`
v5qqbrowser
  • 浏览: 356727 次
文章分类
社区版块
存档分类
最新评论

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编程式和声明式事务管理

    spring编程式和声明式事务管理,事务放在dao,存演示版本!懂!

    spring 声明式事务

    1.本例子的使用了 ssh 框架 2.本例子DAO层 使用了 getHibernateTemplate 来实现数据的新增修改和删除 3.本例子使用了声明式...4.本例子提供了详细的使用方法,可以根据 readme.txt 来逐步的验证声明式事务是否起作用

    Spring配置事务在DAO层和业务逻辑层

    Spring通过AOP实现声明式事务管理。通常通过TransactionProxyFactoryBean设置Spring事务代理。

    Spring3.x_Struts2.x_Hibernate3.x整合之声明式事务配置

    事务声明在Dao中,但是通常都会在Service中来处理多个业务逻辑的关系,如:删除,更新等,此时如果在执行了一个步骤之后抛出抛出异常就会导致数据部完整,所以事务不应该在Dao中处理,而应该在Service...声明式的事务。

    Spring2.5和Hibernate3集成--学习spring aop ioc

    采用声明式事务 1.声明式事务的配置 * 配置sessionFactory * 配置事务管理器 * 配置事务的传播特性 * 配置哪些类哪些方法使用事务 2.编写业务逻辑方法 * 继承HibernateDaoSupport类,使用this....

    SSM框架教程Spring+SpringMVC+MyBatis全覆盖_Java热门框架视频教程

    7、Spring的声明式事务控制 8、SpringMVC的介绍及其快速入门 9、SpringMVC的数据请求和响应 10、SpringMVC的异常处理及其拦截器 11、MyBatis简介及其入门 12、MyBatis的配置文件详解 13、MyBatis的多表操作 适用...

    Spring 2.0 开发参考手册

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 ...

    spring4.3.9相关jar包

    spring-core.jar(必须):这个jar 文件包含Spring 框架...spring-tx.jar:为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。 spring-webmvc-portlet.jar:Spring MVC的增强 spring-websocket.jar:

    Spring中文帮助文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    Spring API

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    spring chm文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...

    Spring.3.x企业应用开发实战(完整版).part2

    9.6 使用注解配置声明式事务 9.6.1 使用@Transactional注解 9.6.2 通过AspectJ LTW引入事务切面 9.7 集成特定的应用服务器 9.7.1 BEA WebLogic 9.7.2 BEA WebLogic 9.8 小结 第10章 Spring的事务管理难点剖析 10.1 ...

    spring-framework-3.1.0.RELEASE.zip

    在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。Spring也提供了很多基础功能(事务管理、持久化框架集成等等),将应用逻辑的开发留给了你。 所有Spring的这些特征使你能够编写更干净、更可管理、...

    spring-net:.NET的Spring框架

    DAO支持类以及与Spring的声明式事务管理功能的集成 可移植服务抽象通过.NET Remoting,Web Service或.NET Serviced Component导出纯.NET对象,并基于终结点URL和服务接口创建客户端代理。 NHibernate整合...

    第24次课-1 Spring与Hibernate的整合

    方便的事务管理:Spring提供的声明式事务处理可以全面有效地处理事务。 异常包装:Spring能够包装Hibernate的异常,使开发者可以选择恰当的层来处理异常。 24.2 管理SessionFactory Hibernate的SessionFactory,是...

    spring in action英文版

     5.3 声明式事务  5.3.1 理解事务属性  5.3.2 声明一个简单的事务策略  5.4 通过方法名声明事务  5.4.1 使用NameMatchTransactionAttributeSource  5.4.2 名称匹配事务的捷径  5.5 用元数据声明...

    Struts+Spring+Hibernate人力管理系统(part1)

    模拟简单的工作流,使用轻量级J2EE架构。技术包括:Struts,Spring,Hibernate,Quartz,整个应用使用Spring提供的DAO支持操作数据库,同时利用Spring的声明式事务。

    Spring3.x企业应用开发实战(完整版) part1

    9.6 使用注解配置声明式事务 9.6.1 使用@Transactional注解 9.6.2 通过AspectJ LTW引入事务切面 9.7 集成特定的应用服务器 9.7.1 BEA WebLogic 9.7.2 BEA WebLogic 9.8 小结 第10章 Spring的事务管理难点剖析 10.1 ...

Global site tag (gtag.js) - Google Analytics