當前位置:
首頁 > 最新 > Spring整合Hibernate.Final

Spring整合Hibernate.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

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 千鋒JAVA開發學院 的精彩文章:

TAG:千鋒JAVA開發學院 |