前面写了这么久的持久层框架存储数据,今天来记录下 Spring 框架对于事物管理的配置,顺带记录下 Spring 框架与 Hibernate 和 Mybatis 这些持久层框架的整合。
Spring 为事务管理提供了一致的编程模版,在高层次建立了统一的事务抽象,不管用户选择 Spring JDBC、Hibernate、JPA还是选择 MyBatis,Spring 都可以让用户用统一的编程模型进行事务管理。这种统一处理的方式带来的好处是不可估量的,用户完全可以抛开事务管理的问题编写程序,并在 Spring 中通过配置完成事务的管理工作。
一、Spring事务管理实现类
Spring 将事务管理委托给底层具体的持久化框架来完成。因此,Spring 为不同的持久化框架提供了不同的 PlantformTransactionManager
接口的实现类。
事务类 | 说明 |
---|---|
org.springframework.jdbc.datasource.DataSourceTransactionManager | 使用Spring JDBC 或者 MyBatis 等基于 DataSource 数据源的持久化技术时,使用该事务管理器 |
org.springframework.orm.hibernateX.HibernateTemplate | 使用Hibernate X.0版本进行持久化时,使用该事务管理器 |
org.springframework.orm.jpa.JpaTransactionManager | 使用JPA进行持久化时候,使用该事务管理器 |
二、Spring JDBC 事务管理配置
Dao 层实现
public class ContactDaoImpl implements ContactDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public Contact findObjectById(int id) throws Exception {
String sql = "select * from contact where id=?";
return jdbcTemplate.queryForObject(sql, new RowMapper() {
@Override
public Contact mapRow(ResultSet rs, int rowNum) throws SQLException {
Contact contact = new Contact();
contact.setId(rs.getInt("id"));
contact.setName(rs.getString("name"));
contact.setBirthday(rs.getDate("birthday"));
return contact;
}
}, id);
}
@Override
public List findAll() throws Exception {
String sql = "select * from contact";
return jdbcTemplate.query(sql, new RowMapper() {
@Override
public Contact mapRow(ResultSet rs, int rowNum) throws SQLException {
Contact contact = new Contact();
contact.setId(rs.getInt("id"));
contact.setName(rs.getString("name"));
contact.setBirthday(rs.getDate("birthday"));
return contact;
}
});
}
@Override
public int insertObject(Contact contact) throws Exception {
String sql = "insert into contact (name, birthday) values (?, ?)";
return jdbcTemplate.update(sql, contact.getName(), contact.getBirthday());
}
@Override
public int updateObject(Contact contact) throws Exception {
String sql = "update contact set name = ?, birthday = ? where id = ?";
return jdbcTemplate.update(sql, contact.getName(), contact.getBirthday(), contact.getId());
}
@Override
public int deleteObjectById(int id) throws Exception {
String sql = "delete from contact where id=?";
return jdbcTemplate.update(sql, id);
}
}
Service 层实现
public class ContactServiceImpl implements ContactService {
private ContactDao contactDao;
public void setContactDao(ContactDao contactDao) {
this.contactDao = contactDao;
}
@Override
public Contact findObjectById(int id) throws Exception {
return contactDao.findObjectById(id);
}
@Override
public List findAll() throws Exception {
return contactDao.findAll();
}
@Override
public int insertObject(Contact contact) throws Exception {
int count = contactDao.insertObject(contact);
return count;
}
@Override
public int updateObject(Contact contact) throws Exception {
return contactDao.updateObject(contact);
}
@Override
public int deleteObjectById(int id) throws Exception {
return contactDao.deleteObjectById(id);
}
}
注意: 只要将 jdbcTemplate
注入到 Dao 的实现层就能实现接下来的JDBC操作,同时需要注意的是,事务管理配置在 Service 层,所以不管是 Dao 层还是 Service 层,都要将异常往上抛,这样才能达到事务管理效果,如果在 Service 层或者下层进行错误抛出,则到不到事务管理效果。
三、Spring 与 MyBatis整合 事务管理配置
Dao 层接口
public interface ContactMapper {
public List findAll() throws Exception;
public Contact findObjectById(int id) throws Exception;
public int insertObject(Contact contact) throws Exception;
public int updateObject(@Param("contact") Contact contact) throws Exception;
public int deleteObjectById(int id) throws Exception;
}
Mapper XML文件
insert into contact (name, birthday) values (#{name}, #{birthday})
update contact
name = #{contact.name},
birthday = #{contact.birthday}
where id = #{contact.id}
delete from contact where id = #{id}
Service 层实现
public class ContactServiceImpl implements ContactService {
private ContactMapper contactMapper;
public void setContactMapper(ContactMapper contactMapper) {
this.contactMapper = contactMapper;
}
@Override
public List findAll() throws Exception {
return contactMapper.findAll();
}
@Override
public Contact findObjectById(int id) throws Exception {
return contactMapper.findObjectById(id);
}
@Override
public int insertObject(Contact contact) throws Exception {
int count = contactMapper.insertObject(contact);
return count;
}
@Override
public int updateObject(Contact contact) throws Exception {
return contactMapper.updateObject(contact);
}
@Override
public int deleteObjectById(int id) throws Exception {
return contactMapper.deleteObjectById(id);
}
}
四、Spring 与 Hibernate整合 事务管理配置
Dao 层实现类
public class ContactDaoImpl implements ContactDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getSession() {
return sessionFactory.getCurrentSession();
}
@Override
public List findAll() throws Exception {
String sql = "from " + Contact.class.getSimpleName();
List list = getSession().createQuery(sql).getResultList();
return list;
}
@Override
public Contact findById(int id) throws Exception {
return getSession().get(Contact.class, id);
}
@Override
public Serializable insertObject(Contact contact) throws Exception {
return getSession().save(contact);
}
@Override
public void updateObject(Contact contact) throws Exception {
getSession().update(contact);
}
@Override
public void deleteObject(Contact contact) throws Exception {
getSession().delete(contact);
}
}
Service 层实现
public class ContactServiceImpl implements ContactService {
private ContactDao contactDao;
public void setContactDao(ContactDao contactDao) {
this.contactDao = contactDao;
}
public List findAll() throws Exception {
return contactDao.findAll();
}
public Contact findById(int id) throws Exception {
return contactDao.findById(id);
}
public Serializable insertObject(Contact contact) throws Exception {
Serializable serializable = contactDao.insertObject(contact);
return serializable;
}
public void updateObject(Contact contact) throws Exception {
contactDao.updateObject(contact);
}
public void deleteObject(Contact contact) throws Exception {
contactDao.deleteObject(contact);
}
}
org.hibernate.dialect.MySQL55Dialect
validate
true
false
classpath:com/ogemray/hibernate/entity/*.hbm.xml
另外也可以在 Spring 中注入 HibernateTempate 来完成持久层操作
持久层代码修改
public class ContactDaoImpl implements ContactDao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public List findAll() throws Exception {
String sql = "from " + Contact.class.getSimpleName();
List list = (List) hibernateTemplate.find(sql);
return list;
}
@Override
public Contact findById(int id) throws Exception {
return hibernateTemplate.get(Contact.class, id);
}
@Override
public void updateObject(Contact contact) throws Exception {
hibernateTemplate.update(contact);
}
}