Spring4.3.14.RELEASE整合Hibernate5.2.10.Final



1.USerDao层实现类不要继承HibernateDaoSupport,现在Spring不推荐使用这种方式。
会报错:org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
使用Hibernate5.2.10.Final版本会报错。使用Hibernate5.1.0.Final版本则不会报错,总之推荐使用SessionFactory的方式来获取Session:

代码如下:UserDaoImpl

@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;


@Override
public void add(String username) {
// TODO Auto-generated method stub
//String sql=insert into user(username) values(?);
User user = new User();
user.setUsername(username);
Session session = sessionFactory.getCurrentSession();
session.save(user);
}

}

Spring的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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd

context:component-scan base-package=com.servlet/context:component-scan
context:property-placeholder location=classpath:jdbc.properties/
!-- 配置C3p0连接池对象 --
bean id=dataSource class=com.mchange.v2.c3p0.ComboPooledDataSource 
property name=user value=root /property
property name=password value=root /property
property name=jdbcUrl value=jdbc:mysql:///spring /property
property name=driverClass value=com.mysql.jdbc.Driver /property
property name=maxPoolSize value=200 /property
property name=initialPoolSize value=20 /property 
/bean
!-- 配置SessionFactory --
bean id=sessionFactory class=org.springframework.orm.hibernate5.LocalSessionFactoryBean
!-- 引入数据源 --
property name=dataSource ref=dataSource/property
property name=hibernateProperties
props
prop key=hibernate.show_sqltrue/prop
prop key=hibernate.format_sqltrue/prop
prop key=hibernate.hbm2ddl.autoupdate/prop
prop key=hibernate.connection.isolation4/prop
!-- prop key=current_session_context_classthread/prop
-- /props
/property

property name=packagesToScan
list
valuecom.servlet/value
/list
/property

/bean




!-- 1.配置平台事务管理器
jdbc/Mybatis:DateSourceTransactionManager 
Hibernate:HibernateTransactionManager 
-- 
bean id=transactionManager class=org.springframework.orm.hibernate5.HibernateTransactionManager
property name=sessionFactory ref=sessionFactory/property
/bean

!-- 开启事务注解 -- 

tx:annotation-driven transaction-manager=transactionManager/
/beans


1.USerDao层实现类不要继承HibernateDaoSupport,现在Spring不推荐使用这种方式。
会报错:org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
使用Hibernate5.2.10.Final版本会报错。使用Hibernate5.1.0.Final版本则不会报错,总之推荐使用SessionFactory的方式来获取Session:

代码如下:UserDaoImpl

@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;


@Override
public void add(String username) {
// TODO Auto-generated method stub
//String sql=insert into user(username) values(?);
User user = new User();
user.setUsername(username);
Session session = sessionFactory.getCurrentSession();
session.save(user);
}

}

Spring的xml配置文件

扫描二维码关注公众号,回复: 175442 查看本文章

?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd

context:component-scan base-package=com.servlet/context:component-scan
context:property-placeholder location=classpath:jdbc.properties/
!-- 配置C3p0连接池对象 --
bean id=dataSource class=com.mchange.v2.c3p0.ComboPooledDataSource 
property name=user value=root /property
property name=password value=root /property
property name=jdbcUrl value=jdbc:mysql:///spring /property
property name=driverClass value=com.mysql.jdbc.Driver /property
property name=maxPoolSize value=200 /property
property name=initialPoolSize value=20 /property 
/bean
!-- 配置SessionFactory --
bean id=sessionFactory class=org.springframework.orm.hibernate5.LocalSessionFactoryBean
!-- 引入数据源 --
property name=dataSource ref=dataSource/property
property name=hibernateProperties
props
prop key=hibernate.show_sqltrue/prop
prop key=hibernate.format_sqltrue/prop
prop key=hibernate.hbm2ddl.autoupdate/prop
prop key=hibernate.connection.isolation4/prop
!-- prop key=current_session_context_classthread/prop
-- /props
/property

property name=packagesToScan
list
valuecom.servlet/value
/list
/property

/bean




!-- 1.配置平台事务管理器
jdbc/Mybatis:DateSourceTransactionManager 
Hibernate:HibernateTransactionManager 
-- 
bean id=transactionManager class=org.springframework.orm.hibernate5.HibernateTransactionManager
property name=sessionFactory ref=sessionFactory/property
/bean

!-- 开启事务注解 -- 

tx:annotation-driven transaction-manager=transactionManager/
/beans


猜你喜欢

转载自blog.csdn.net/qianfeng_dashuju/article/details/80063973