依据前一篇《Spring + Hibernate整合》:https://blog.youkuaiyun.com/wenwenliuyunabcd/article/details/100703434,进行修改
spring容器配置beans.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: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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:db.properties" />
<!-- 扫描service,respositroy组件 -->
<context:component-scan base-package="com.liuyun"></context:component-scan>
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- sessionFactory (Spring接管) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置Hibernate相关属性 -->
<property name="hibernateProperties">
<props>
<!-- 配置方言,打印语句,格式化sql,自动建表 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 指定映射文件 -->
<!-- <property name="mappingResources">
<list>
<value>/com/liuyun/domain/User.hbm.xml</value>
</list>
</property> -->
<!-- 指定扫描的包:扫描还有注解配置映射关系的实体 -->
<property name="packagesToScan">
<list>
<value>com.liuyun.domain</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--a. 事务管理类 -->
<bean id="trans" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启注解事务,在业务代码中加入注解进行事务控制 -->
<tx:annotation-driven transaction-manager="trans" />
</beans>
事务的注解驱动修改业务类UserServiceImpl :
package com.liuyun.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.liuyun.dao.IUserDao;
import com.liuyun.domain.User;
import com.liuyun.service.IUserService;
@Service("userService")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class UserServiceImpl implements IUserService {
@Autowired
IUserDao userDao;
@Override
public int save(User user) {
return userDao.save(user);
}
@Override
public void update(Long id, User user) {
user.setId(id);
userDao.update(user);
}
@Override
public void deleteById(Long id) {
userDao.deleteById(id);
}
@Override
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public List<User> findAll() {
return userDao.findAll();
}
@Override
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public User findById(Long id) {
return userDao.findById(id);
}
@Override
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public User findByName(String name) {
return userDao.findByName(name);
}
}

本文详细介绍了如何在Spring框架中整合Hibernate,实现数据库操作。通过配置beans.xml文件,设置事务注解驱动,以及修改业务类UserServiceImpl,展示了完整的整合过程。
525

被折叠的 条评论
为什么被折叠?



