Spring DAO 实现声明式事务

实现注册新用户的功能,通过Spring的AOP来管理事务。

通过事务保证:

1.customer(用户信息),account(资金账户信息)同时创建成功,否则回滚。

2.用户名唯一,否则回滚。

3.用户没有存在BlackList(黑名单中),否则回滚。

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. }
  1. packagecom.customer;
  2. importorg.springframework.jdbc.core.support.JdbcDaoSupport;
  3. importorg.springframework.jdbc.core.JdbcTemplate;
  4. importcom.base.IBaseDao;
  5. importcom.base.IbaseBO;
  6. publicclassCustomerDaoextendsJdbcDaoSupportimplementsIBaseDao{
  7. publicvoidinsert(IbaseBObo){
  8. Customercustomer=(Customer)bo;
  9. JdbcTemplatejt=this.getJdbcTemplate();
  10. jt.execute("insertintocustomervalues('"+customer.getUsername()+"','"+customer.getPassword()+"')");
  11. }
  12. }

  1. packagecom.account;
  2. importorg.springframework.jdbc.core.support.JdbcDaoSupport;
  3. importorg.springframework.jdbc.core.JdbcTemplate;
  4. importcom.base.IBaseDao;
  5. importcom.base.IbaseBO;
  6. publicclassAccountDaoextendsJdbcDaoSupportimplementsIBaseDao{
  7. publicvoidinsert(IbaseBObo){
  8. Accountaccount=(Account)bo;
  9. JdbcTemplatejt=this.getJdbcTemplate();
  10. jt.execute("insertintoaccountvalues('"+account.getUsername()+"',"+account.getDeposit()+")");
  11. }
  12. }

  1. packagecom.blackList;
  2. importjava.util.List;
  3. importorg.springframework.jdbc.core.support.JdbcDaoSupport;
  4. importorg.springframework.jdbc.core.JdbcTemplate;
  5. importorg.springframework.jdbc.support.rowset.SqlRowSet;
  6. importcom.base.IBaseDao;
  7. importcom.base.IbaseBO;
  8. publicclassBlackListDaoextendsJdbcDaoSupportimplementsIBaseDao{
  9. publicListquery(){
  10. Listlist=null;
  11. JdbcTemplatejt=this.getJdbcTemplate();
  12. list=jt.queryForList("select*fromblackList");
  13. returnlist;
  14. }
  15. publicvoidinsert(IbaseBObo){
  16. }
  17. }

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.BlackListDao;
  9. importcom.customer.Customer;
  10. importcom.customer.CustomerDao;
  11. publicclassCustomerManagerServiceImpimplementsCustomerManagerService{
  12. privateCustomerDaocustomerDao;
  13. privateAccountDaoaccountDao;
  14. privateBlackListDaoblackListDao;
  15. publicvoidinsertCustomer(IbaseBO_customer,IbaseBO_account)throwsException{
  16. Customercustomer=(Customer)_customer;
  17. Listlist=blackListDao.query();
  18. for(inti=0;i<list.size();i++){
  19. LinkedHashMapmap=(LinkedHashMap)list.get(i);
  20. if(map.get("name").equals(customer.getUsername())){
  21. thrownewException("注册的用户名被检查出存在于黑名单中。");
  22. }
  23. }
  24. customerDao.insert(customer);
  25. accountDao.insert(_account);
  26. }
  27. publicvoidsetCustomerDao(CustomerDaocustomerDao){
  28. this.customerDao=customerDao;
  29. }
  30. publicvoidsetAccountDao(AccountDaoaccountDao){
  31. this.accountDao=accountDao;
  32. }
  33. publicvoidsetBlackListDao(BlackListDaoblackListDao){
  34. this.blackListDao=blackListDao;
  35. }
  36. }

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="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  20. <propertyname="dataSource"ref="dataSource"/>
  21. </bean>
  22. <beanid="customerDao"class="com.customer.CustomerDao">
  23. <propertyname="dataSource"ref="dataSource"/>
  24. </bean>
  25. <beanid="accountDao"class="com.account.AccountDao">
  26. <propertyname="dataSource"ref="dataSource"/>
  27. </bean>
  28. <beanid="blackListDao"class="com.blackList.BlackListDao">
  29. <propertyname="dataSource"ref="dataSource"/>
  30. </bean>
  31. <beanid="baseTxProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"lazy-init="true">
  32. <propertyname="transactionManager">
  33. <refbean="transactionManager"/>
  34. </property>
  35. <propertyname="transactionAttributes">
  36. <props>
  37. <propkey="*">PROPAGATION_REQUIRED</prop>
  38. </props>
  39. </property>
  40. </bean>
  41. <beanid="customerServiceManager"parent="baseTxProxy">
  42. <propertyname="target">
  43. <beanid="userManagerServiceImp"class="com.customer.managerService.CustomerManagerServiceImp">
  44. <propertyname="customerDao"ref="customerDao"/>
  45. <propertyname="accountDao"ref="accountDao"/>
  46. <propertyname="blackListDao"ref="blackListDao"/>
  47. </bean>
  48. </property>
  49. </bean>
  50. </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("ffg");
  14. c.setPassword("12345");
  15. Accounta=newAccount();
  16. a.setDeposit(0);
  17. a.setUsername("admin");
  18. try{
  19. ms.insertCustomer(c,a);
  20. }catch(Exceptione){
  21. e.printStackTrace();
  22. }
  23. }
  24. }

你可能感兴趣的:(spring)