TestA.java: public class TestA extends HibernateDaoSupport{ public void test(){ HibernateTemplate template = this.getHibernateTemplate(); List list = template.find("from User"); for(int i=0;i<list.size();i++){ User u = (User)list.get(i); System.out.println(u.getUserid()+" "+u.getEmail()); } } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); TestA t = (TestA)ctx.getBean("testa"); t.test(); } }
applicationContext.xml: <bean id="testa" class="com.sun.demo.TestA"> <property name="sessionFactory"> <ref bean="mySessionFactory"/> </property> </bean>
public class TestA extends JdbcDaoSupport{ public void test(){ int n = this.getJdbcTemplate().queryForInt("select max(userid) from User"); System.out.println(n); } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); TestA ta = (TestA)ctx.getBean("testa"); ta.test(); } }需要在Spring的配置文件applicationContext.xml中控制反转用到连接数据库中的类是注入DataSource,如下:
<bean id="testa" class="com.sun.demo.TestA"> <property name="dataSource"> <ref bean="myDataSource"/> </property> </bean>
public class TestA extends JdbcDaoSupport{ public void test(){ PlatformTransactionManager ptm = new DataSourceTransactionManager(this.getDataSource()); TransactionTemplate tst = new TransactionTemplate(ptm); tst.execute(new TransactionCallback(){ public Object doInTransaction(TransactionStatus status) { getJdbcTemplate().update("update User set password='1230' where userid='1001'"); getJdbcTemplate().update("update User set email='4444444444444444444444444444444444' where userid='1002'"); getJdbcTemplate().update("update User set phone='phone'"); return null; } }); } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); TestA t = (TestA)ctx.getBean("testa"); t.test(); } }需要在Spring的配置文件applicationContext.xml中控制反转用到连接数据库中的类是注入DataSource,如下:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> ... ... <property name="defaultAutoCommit"> <value>false</value> </property> </bean> <bean id="testa" class="com.sun.demo.TestA"> <property name="dataSource"> <ref bean="myDataSource"/> </property> </bean>
public class TestB extends HibernateDaoSupport{ public void test(){ HibernateTransactionManager hm = new HibernateTransactionManager(this.getSessionFactory()); TransactionTemplate ttm = new TransactionTemplate(hm); ttm.execute(new TransactionCallback(){ public Object doInTransaction(TransactionStatus status) { User u1 = new User(); u1.setUserid("3001"); u1.setPassword("3001"); getHibernateTemplate().save(u1); User u2 = new User(); u2.setUserid("3001"); u2.setPassword("3002"); getHibernateTemplate().save(u2); return null; } }); } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); TestB t = (TestB)ctx.getBean("testb"); t.test(); } }需要在Spring的配置文件applicationContext.xml中控制反转用到连接数据库中的类是注入SessionsFactory,如下:
applicationContext.xml: <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> ... ... <property name="defaultAutoCommit"> <value>false</value> </property> </bean> <bean id="testb" class="com.sun.demo.TestB"> <property name="sessionFactory"> <ref bean="mySessionFactory"/> </property> </bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> ... .... <property name="defaultAutoCommit"> <value>false</value> </property> </bean>
<!-- 事务管理配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="myDataSource"/> </property> </bean> <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager"/> <!-- 事务管理属性 --> <property name="transactionAttributes"> <props> <prop key="updateUser">PROPAGATION_REQUIRED</prop> </props> <!-- 事务管理目标bean --> </property> <property name="target" ref="userdao"></property> </bean> <bean id="userdao" class="com.sun.demo.UserDao"> <property name="dataSource"> <ref bean="myDataSource"/> </property> </bean>
IUserDao.java://定义接口 public interface IUserDao { public void updateUser(); } UserDao.java: public class UserDao extends JdbcDaoSupport implements IUserDao{ public void updateUser(){ this.getJdbcTemplate().update("update User set password='password1' where userid='1001'"); this.getJdbcTemplate().update("update User set password='password9999999999999999999999999999999' where userid='1002'"); } }
public class{ public void test(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserDao u = (IUserDao)ctx.getBean("baseTransactionProxy"); u.updateUser(); } public static void main(String[] args) { TestA t = new TestA(); t.test(); } }
public interface IUserDao { public void updateUser(); } UserDao.java: public class UserDao extends HibernateDaoSupport implements IUserDao{ public void updateUser(){ User u1 = new User(); u1.setUserid("1001"); u1.setPassword("5001"); this.getHibernateTemplate().update(u1); User u2 = new User(); u2.setUserid("1003"); u2.setPassword("20015555555555555555555555555555555555555555555"); this.getHibernateTemplate().update(u2); } }
public class TestA{ public void test(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserDao userdao= (IUserDao)ctx.getBean("userproxy"); userdao.updateUser(); } public static void main(String[] args) { TestA t = new TestA(); t.test(); } }
<!-- 事务管理配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="mySessionFactory"/> </property> </bean> <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"/> <!-- 事务管理属性 --> <property name="transactionAttributes"> <props> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> </props> <!-- 事务管理目标bean --> </property> </bean> <bean id="userdao" class="com.sun.demo.UserDao"> <property name="sessionFactory"> <ref bean="mySessionFactory"/> </property> </bean> <bean id="userproxy" parent="baseTransactionProxy"> <property name="target" ref="userdao"></property> </bean>
以上这种声明事务的方式配置较多,需要为每个类配置代理对象,还有一种配置事务的方式使用了Spring的aop方式,自动生成代理对象,可以减少配置:
<!-- 事务管理配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 哪类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allServiceMethod" expression="execution(* com.spring.tran.service.*.*(..))"/> <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/> </aop:config>