这里将用到以下几个包:
引用
aopalliance-1.0.jar
commons-collections.jar
commons-dbcp.jar
commons-logging-1.1.1.jar
commons-pool.jar
jstl.jar
log4j-1.2.15.jar
mysql-connector-java-5.1.6-bin.jar
spring-aop-2.5.6.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-context-support-2.5.6.jar
spring-core-2.5.6.jar
spring-jdbc-2.5.6.jar
spring-tx-2.5.6.jar
spring-web-2.5.6.jar
spring-webmvc-2.5.6.jar
standard.jar
主要增加了spring-aop-2.5.6.jar的AOP支持包!之前我们在AccountService中加入了注解@Transactional标签,但是要想要真正发挥事务作用,还需要一些配置。主要需要调整dao.xml文件dao.xml-事务管理
-
<bean
-
id="transactionManager"
-
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
-
p:dataSource-ref="dataSource"/>
-
<tx:annotation-driven
-
transaction-manager="transactionManager"/>
细化一下AccountService接口方法AccountService.java
-
-
-
-
packageorg.zlex.spring.service;
-
-
importorg.springframework.dao.DataAccessException;
-
importorg.springframework.transaction.annotation.Transactional;
-
importorg.zlex.spring.domain.Account;
-
-
-
-
-
-
-
-
-
publicinterfaceAccountService{
-
-
-
-
-
-
-
-
-
@Transactional(readOnly=true)
-
Accountread(Stringusername,Stringpassword);
-
-
-
-
-
-
-
-
@Transactional(readOnly=true)
-
Accountread(intid);
-
-
-
-
-
-
-
-
@Transactional(readOnly=false,rollbackFor=DataAccessException.class)
-
Accountregister(Accountaccount);
-
}
这里我把注解@Transactional调整到了具体的方法上,也就是说这样写的话,凡是加入注解的标注的方法都属于事务配置!Account register(Account account);用做用户注册作用!
@Transactional(readOnly = true)只读属性@Transactional(readOnly = false, rollbackFor = DataAccessException.class)只读关闭,遇到DataAccessException异常回滚!如果不对异常进行处理,该异常将一直向上层抛出,直至抛出到页面!如果你的Eclipse集成了SpringIDE,你可以观察一下这时的xml配置文件和AccoutServiceImpl.java的变化!

这次,来个用户注册功能演示,故意在某个位置制造一个异常,看看是否正常回滚!
先看注册控制器RegisterController.java
-
-
-
-
packageorg.zlex.spring.controller;
-
-
importjava.text.DateFormat;
-
importjava.text.SimpleDateFormat;
-
importjava.util.Date;
-
-
importorg.springframework.beans.factory.annotation.Autowired;
-
importorg.springframework.beans.propertyeditors.CustomDateEditor;
-
importorg.springframework.stereotype.Controller;
-
importorg.springframework.ui.ModelMap;
-
importorg.springframework.web.bind.WebDataBinder;
-
importorg.springframework.web.bind.annotation.InitBinder;
-
importorg.springframework.web.bind.annotation.ModelAttribute;
-
importorg.springframework.web.bind.annotation.RequestMapping;
-
importorg.springframework.web.bind.annotation.RequestMethod;
-
importorg.zlex.spring.domain.Account;
-
importorg.zlex.spring.service.AccountService;
-
-
-
-
-
-
-
-
-
@Controller
-
@RequestMapping(value="/register.do")
-
publicclassRegisterController{
-
-
@Autowired
-
privateAccountServiceaccountService;
-
-
@InitBinder
-
publicvoidinitBinder(WebDataBinderbinder){
-
-
-
-
DateFormatformat=newSimpleDateFormat("yyyy-MM-dd");
-
binder.registerCustomEditor(Date.class,"birthday",
-
newCustomDateEditor(format,true));
-
}
-
-
@RequestMapping(method=RequestMethod.GET)
-
publicStringinitForm(ModelMapmodel){
-
Accountaccount=newAccount();
-
model.addAttribute("account",account);
-
-
return"account/register";
-
}
-
-
@RequestMapping(method=RequestMethod.POST)
-
protectedStringsubmit(@ModelAttribute("account")Accountaccount){
-
intid=accountService.register(account).getId();
-
-
return"redirect:profile.do?id="+id;
-
}
-
}
@InitBinder用于表单自定义属性绑定。这里我们要求输入一个日期格式的生日。@RequestMapping(method = RequestMethod.GET)用于初始化页面。@RequestMapping(method = RequestMethod.POST)用于提交页面。再看注册页面register.jsp
-
<html>
-
<head>
-
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
-
<title>注册</title>
-
<linkrel="stylesheet"type="text/css"href="css/style.css"/>
-
<scripttype="text/javascript"src="js/calendar.js"></script>
-
</head>
-
<body>
-
<fieldset><legend>用户注册</legend><form:form
-
commandName="account">
-
<ul>
-
<li><form:labelpath="username">用户名:</form:label><form:input
-
path="username"/></li>
-
<li><form:labelpath="password">密码:</form:label><form:password
-
path="password"/></li>
-
<li><form:labelpath="birthday">生日:</form:label><form:input
-
path="birthday"onfocus="showDate(this);"/></li>
-
<li><form:labelpath="email">Email:</form:label><form:input
-
path="email"/></li>
-
<li>
-
<buttontype="submit">注册</button>
-
<buttontype="reset">重置</button>
-
</li>
-
</ul>
-
</form:form></fieldset>
-
</body>
-
</html>
这里我用了一个JavaScript日期控制标签:
-
<scripttype="text/javascript"src="js/calendar.js"></script>
使用起来就像是这样:
非常好用!!!
当然,你完全可以使用JE上的那个JS控件!接下来稍微调整一下AccountService接口及其实现AccountServiceImplAccountService.java
-
publicinterfaceAccountService{
-
-
-
-
-
-
-
-
@Transactional(readOnly=false,rollbackFor=DataAccessException.class)
-
Accountregister(Accountaccount);
-
-
}
-
@Service
-
publicclassAccountServiceImplimplementsAccountService{
-
-
@Autowired
-
privateAccountDaoaccountDao;
-
-
-
-
@Override
-
publicAccountregister(Accountaccount){
-
accountDao.create(account);
-
returnaccountDao.read(account.getUsername());
-
}
-
}
为了在插入一条记录后获得当前用户的主键,我们还得这么玩!
的确有点雷人~从架构考虑,这是符合业务要求的实现!如果用iBatis或者Hibernate,这个问题就有数据库一次IO处理完成了!
再看看AccountDao接口及其实现AccountDaoImplAccountDao.java
-
publicinterfaceAccountDao{
-
-
-
-
-
-
-
-
voidcreate(Accountaccount);
-
}
AccountDaoImpl.java
-
@Repository
-
publicclassAccountDaoImplimplementsAccountDao{
-
-
-
@Override
-
publicvoidcreate(Accountaccount){
-
Stringsql="INSERTINTOaccount(username,password,birthday,email)VALUES(?,?,?,?)";
-
-
jdbcTemplate.update(sql,newObject[]{account.getUsername(),
-
account.getPassword(),account.getBirthday(),
-
account.getEmail()});
-
}
-
}
来个注册演示!
注册:
信息展示:
来制造一起事故!
先看看数据库目前的状况!
在AccountDaoImpl中来个破坏!
-
@Override
-
publicvoidcreate(Accountaccount){
-
Stringsql="INSERTINTOaccount(username,password,birthday,email)VALUES(?,?,?,?)";
-
-
jdbcTemplate.update(sql,newObject[]{account.getUsername(),
-
account.getPassword(),account.getBirthday(),
-
account.getEmail()});
-
-
thrownewRecoverableDataAccessException("TEST");
-
}
我们强行在执行完Insert语句后抛出DataAccessException异常(RecoverableDataAccessException)!
来个注册试试!
点击提交看看返回的异常!
异常回滚生效!
数据库中当然是什么都没有,我就不废话了!
分享到:
相关推荐
在本篇《Spring注解学习手札(四)持久层浅析》中,我们将深入探讨Spring框架在持久层的应用,特别是如何通过注解简化数据库操作。Spring作为一个强大的轻量级框架,提供了丰富的功能来处理数据访问,使得开发者可以...
在本篇《Spring注解学习手札(一)构建简单Web应用》中,我们将深入探讨如何使用Spring框架的注解来构建一个基本的Web应用程序。Spring框架是Java开发中的核心工具,尤其在企业级应用中广泛应用。它简化了依赖注入、...
【Spring注解学习手札】 在现代Java Web开发中,Spring框架因其强大的功能和灵活性而备受推崇。Spring注解的引入极大地简化了配置文件,提高了开发效率。本篇将聚焦于Spring MVC中的注解,通过构建一个简单的Web...
以上内容基于Snowolf的博客文章《Spring注解手札》系列,该系列文章详尽地介绍了Spring注解的使用,从构建简单的Web应用到控制层、表单处理、持久层以及事务管理和测试,覆盖了Spring注解的多个方面。通过这些实例,...
- "spring-reference.pdf" 和 "Spring注解手札.pdf" 可能是详细的 Spring 参考文档和注解指南,对于深入学习 Spring 极为有用。 以上内容只是 Spring 框架的冰山一角,想要精通 Spring,还需要通过阅读文档、实践...