Spring&Hibernate实现声明式事务

通过Spring整合Hibernate并实现Spring的声明使事物管理

使用Hibernate时需要使用org.springframework.orm.hibernate3.HibernateTransactionManager来作为事务管理器

代码如下:

ORM

  1. <?xmlversion="1.0"?>
  2. <!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping>
  5. <classname="com.customer.Customer"table="customer"schema="dbo">
  6. <idname="username"type="java.lang.String">
  7. <columnname="username"length="50"/>
  8. </id>
  9. <propertyname="password"type="java.lang.String">
  10. <columnname="password"length="20"/>
  11. </property>
  12. </class>
  13. </hibernate-mapping>

  1. <?xmlversion="1.0"?>
  2. <!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping>
  5. <classname="com.account.Account"table="account"schema="dbo">
  6. <idname="ca_id"type="int">
  7. <columnname="ca_id"length="4"/>
  8. <generatorclass="identity"/>
  9. </id>
  10. <propertyname="username"type="java.lang.String">
  11. <columnname="username"length="50"/>
  12. </property>
  13. <propertyname="deposit"type="double">
  14. <columnname="deposit"length="20"/>
  15. </property>
  16. </class>
  17. </hibernate-mapping>

  1. <?xmlversion="1.0"?>
  2. <!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping>
  5. <classname="com.blackList.BlackList"table="blackList"schema="dbo">
  6. <idname="id"type="int">
  7. <columnname="id"length="4"/>
  8. <generatorclass="identity"/>
  9. </id>
  10. <propertyname="name"type="java.lang.String">
  11. <columnname="name"length="20"/>
  12. </property>
  13. </class>
  14. </hibernate-mapping>

BO:

  1. packagecom.base;
  2. publicinterfaceIBaseBO{
  3. }
  1. packagecom.customer;
  2. importcom.base.IBaseBO;
  3. publicclassCustomerimplementsIBaseBO{
  4. privateStringusername;
  5. privateStringpassword;
  6. publicStringgetUsername(){
  7. returnusername;
  8. }
  9. publicvoidsetUsername(Stringusername){
  10. this.username=username;
  11. }
  12. publicStringgetPassword(){
  13. returnpassword;
  14. }
  15. publicvoidsetPassword(Stringpassword){
  16. this.password=password;
  17. }
  18. }

  1. packagecom.account;
  2. importcom.base.IBaseBO;
  3. publicclassAccountimplementsIBaseBO{
  4. privateintca_id;
  5. privateStringusername;
  6. privatedoubledeposit;
  7. publicintgetCa_id(){
  8. returnca_id;
  9. }
  10. publicvoidsetCa_id(intca_id){
  11. this.ca_id=ca_id;
  12. }
  13. publicStringgetUsername(){
  14. returnusername;
  15. }
  16. publicvoidsetUsername(Stringusername){
  17. this.username=username;
  18. }
  19. publicdoublegetDeposit(){
  20. returndeposit;
  21. }
  22. publicvoidsetDeposit(doubledeposit){
  23. this.deposit=deposit;
  24. }
  25. }

  1. packagecom.blackList;
  2. importcom.base.IBaseBO;
  3. publicclassBlackListimplementsIBaseBO{
  4. privateintid;
  5. privateStringname;
  6. publicintgetId(){
  7. returnid;
  8. }
  9. publicvoidsetId(intid){
  10. this.id=id;
  11. }
  12. publicStringgetName(){
  13. returnname;
  14. }
  15. publicvoidsetName(Stringname){
  16. this.name=name;
  17. }
  18. }

DAO:

  1. packagecom.base;
  2. publicinterfaceIBaseDao{
  3. publicvoidinsert(IBaseBObo);
  4. }

BaseDao在这里使用HibernateTemplate

  1. packagecom.base;
  2. importorg.springframework.orm.hibernate3.HibernateTemplate;
  3. publicclassBaseDaoextendsHibernateTemplateimplementsIBaseDao{
  4. publicvoidinsert(IBaseBObo){
  5. }
  6. }

Dao的代码变得非常简单...

  1. packagecom.customer;
  2. importcom.base.BaseDao;
  3. importcom.base.IBaseBO;
  4. publicclassCustomerDaoextendsBaseDao{
  5. publicvoidinsert(IBaseBObo){
  6. Customercustomer=(Customer)bo;
  7. this.save(customer);
  8. }
  9. }
  1. packagecom.account;
  2. importcom.base.BaseDao;
  3. importcom.base.IBaseBO;
  4. publicclassAccountDaoextendsBaseDao{
  5. publicvoidinsert(IBaseBObo){
  6. Accountaccount=(Account)bo;
  7. this.save(account);
  8. }
  9. }

  1. packagecom.blackList;
  2. importjava.util.List;
  3. importcom.base.BaseDao;
  4. importcom.base.IBaseBO;
  5. publicclassBlackListDaoextendsBaseDao{
  6. publicListquery(){
  7. Listlist=null;
  8. list=this.find("fromBlackList");
  9. returnlist;
  10. }
  11. publicvoidinsert(IBaseBObo){
  12. }
  13. }

ManagerService:

  1. packagecom.customer.managerService;
  2. importjava.sql.SQLException;
  3. importcom.base.IBaseBO;
  4. publicinterfaceCustomerManagerService{
  5. publicvoidinsertCustomer(IBaseBOcustomer,IBaseBOaccount)throwsException;
  6. }
  1. packagecom.customer.managerService;
  2. importjava.sql.SQLException;
  3. importjava.util.LinkedHashMap;
  4. importjava.util.List;
  5. importorg.springframework.jdbc.support.rowset.SqlRowSet;
  6. importcom.account.AccountDao;
  7. importcom.base.IBaseBO;
  8. importcom.blackList.BlackList;
  9. importcom.blackList.BlackListDao;
  10. importcom.customer.Customer;
  11. importcom.customer.CustomerDao;
  12. publicclassCustomerManagerServiceImpimplementsCustomerManagerService{
  13. privateCustomerDaocustomerDao;
  14. privateAccountDaoaccountDao;
  15. privateBlackListDaoblackListDao;
  16. publicvoidinsertCustomer(IBaseBO_customer,IBaseBO_account)throwsException{
  17. Customercustomer=(Customer)_customer;
  18. Listlist=blackListDao.query();
  19. for(inti=0;i<list.size();i++){
  20. BlackListblackList=(BlackList)list.get(i);
  21. if(blackList.getName().equals(customer.getUsername())){
  22. thrownewSQLException("注册的用户名被检查出存在于黑名单中。");
  23. }
  24. }
  25. customerDao.insert(_customer);
  26. accountDao.insert(_account);
  27. }
  28. publicvoidsetCustomerDao(CustomerDaocustomerDao){
  29. this.customerDao=customerDao;
  30. }
  31. publicvoidsetAccountDao(AccountDaoaccountDao){
  32. this.accountDao=accountDao;
  33. }
  34. publicvoidsetBlackListDao(BlackListDaoblackListDao){
  35. this.blackListDao=blackListDao;
  36. }
  37. }

applicationContext.xml:

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4. <beanid="dataSource"
  5. class="org.apache.commons.dbcp.BasicDataSource">
  6. <propertyname="driverClassName">
  7. <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
  8. </property>
  9. <propertyname="url">
  10. <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testdb</value>
  11. </property>
  12. <propertyname="username">
  13. <value>sa</value>
  14. </property>
  15. <propertyname="password">
  16. <value>123</value>
  17. </property>
  18. </bean>
  19. <beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  20. <propertyname="dataSource">
  21. <reflocal="dataSource"/>
  22. </property>
  23. <propertyname="mappingResources">
  24. <list>
  25. <value>/Customer.hbm.xml</value>
  26. <value>/Account.hbm.xml</value>
  27. <value>/BlackList.hbm.xml</value>
  28. </list>
  29. </property>
  30. <propertyname="hibernateProperties">
  31. <props>
  32. <propkey="hibernate.dialect">
  33. org.hibernate.dialect.SQLServerDialect
  34. </prop>
  35. </props>
  36. </property>
  37. </bean>
  38. <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  39. <propertyname="sessionFactory">
  40. <refbean="sessionFactory"/>
  41. </property>
  42. </bean>
  43. <beanid="customerDao"class="com.customer.CustomerDao">
  44. <propertyname="sessionFactory">
  45. <refbean="sessionFactory"/>
  46. </property>
  47. </bean>
  48. <beanid="accountDao"class="com.account.AccountDao">
  49. <propertyname="sessionFactory">
  50. <refbean="sessionFactory"/>
  51. </property>
  52. </bean>
  53. <beanid="blackListDao"class="com.blackList.BlackListDao">
  54. <propertyname="sessionFactory">
  55. <refbean="sessionFactory"/>
  56. </property>
  57. </bean>
  58. <beanid="baseTxProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"lazy-init="true">
  59. <propertyname="transactionManager">
  60. <refbean="transactionManager"/>
  61. </property>
  62. <propertyname="transactionAttributes">
  63. <props>
  64. <propkey="*">PROPAGATION_REQUIRED</prop>
  65. </props>
  66. </property>
  67. </bean>
  68. <beanid="customerServiceManager"parent="baseTxProxy">
  69. <propertyname="target">
  70. <beanid="userManagerServiceImp"class="com.customer.managerService.CustomerManagerServiceImp">
  71. <propertyname="customerDao"ref="customerDao"/>
  72. <propertyname="accountDao"ref="accountDao"/>
  73. <propertyname="blackListDao"ref="blackListDao"/>
  74. </bean>
  75. </property>
  76. </bean>
  77. </beans>

调用实例:

  1. packagecom;
  2. importjava.sql.SQLException;
  3. importorg.springframework.context.ApplicationContext;
  4. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  5. importcom.account.Account;
  6. importcom.customer.Customer;
  7. importcom.customer.managerService.CustomerManagerService;
  8. publicclassMain{
  9. publicstaticvoidmain(String[]args){
  10. ApplicationContextctx=newClassPathXmlApplicationContext("applicationContext.xml");
  11. CustomerManagerServicems=(CustomerManagerService)ctx.getBean("customerServiceManager");
  12. Customerc=newCustomer();
  13. c.setUsername("DD");
  14. c.setPassword("12345");
  15. Accounta=newAccount();
  16. a.setDeposit(0);
  17. a.setUsername("hehua");
  18. try{
  19. ms.insertCustomer(c,a);
  20. }catch(Exceptione){
  21. e.printStackTrace();
  22. }
  23. }
  24. }

你可能感兴趣的:(Hibernate)