步骤:
1.利用MyEclipse顺序配置好Spring和Hibernate,mysql数据库中有一张表user,它里边有字段username(PK)和password;
2.目录结构如下:
3.代码如下(User作为bean,由reverse engineering自动生成,这里不给出.):
IUserDAO:
public interface IUserDAO {
void addUser(User user);
}
UserDAOImp:
@Component
public class UserDAOImp implements IUserDAO {
private SessionFactory sessionFactory;
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
Session session = sessionFactory.getCurrentSession();
session.save(user);
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
UserService:
@Component
public class UserService {
private UserDAOImp userDAOImp;
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.addUser(new User("user4", "123"));
}
@Transactional
public void addUser(User user) {
userDAOImp.addUser(user);
}
public UserDAOImp getUserDAOImp() {
return userDAOImp;
}
@Resource
public void setUserDAOImp(UserDAOImp userDAOImp) {
this.userDAOImp = userDAOImp;
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="user"></property>
<property name="password" value="1234"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>glut.bean.User</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="glut"></context:component-scan>
</beans>
4.运行UserService中的单元测试即可.
5.注意几个小问题:
5.1 单元测试中,ctx.getBean()的参数不要写成"UserService",component默认第一个字母是小写.
5.2 Transactional注解应放在Service上,而不是DAO上,若添加完User后,还要有别的DAO操作(比如log),那么,把Transactional放在DAO上就不合适了.所以应该放在service上.
总结:
事务注解很方便,再也不用写try{}catch来手动进行事务回滚了....