您现在的位置是:首页 > 文章详情

spring整合hibernate

日期:2018-11-27点击:670

个人感觉比较好的spring 整合hibernate的方式

一、添加依赖

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> </dependency>

二、在spring的core配置文件中配置

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.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"> <!-- 引入配置文件 --> <context:property-placeholder location="classpath:conf.properties" /> <!-- c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 基本配置 --> <property name="driverClass" value="${c3p0.driverClass}" /> <property name="jdbcUrl" value="${c3p0.jdbcUrl}" /> <property name="user" value="${c3p0.user}" /> <property name="password" value="${c3p0.password}" /> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}" /> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="${c3p0.maxIdleTime}" /> <!--连接池中保留的最小连接数。 --> <property name="minPoolSize" value="${c3p0.minPoolSize}" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="${c3p0.maxPoolSize}" /> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="${c3p0.initialPoolSize}" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="${c3p0.acquireIncrement}" /> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" /> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <!-- 自动创建|更新|验证数据库表结构。如果不是此方面的需求建议set value="none" --> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!-- 方言 --> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <!-- 打印sql --> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!-- 格式化sql --> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> <!-- 注解方式配置 --> <property name="packagesToScan"> <list> <value>com.web.common.bean.entity</value> </list> </property> </bean> <bean id="myTxManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="mySessionFactory" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> <!-- 1.注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>

第三步、测试

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:/spring/spring-core.xml") public class UserTest { @Autowired private SessionFactory sessionFactory; @Test public void test(){ Session session = sessionFactory.openSession(); User user = new User(); user.setCreateTime(new Date()); user.setEditTime(user.getCreateTime()); user.setNickName("4444"); user.setSex("1"); user.setUserNo("232"); user.setPassword("333"); user.setStatusId("Y"); session.save(user); } }


原文链接:https://blog.roncoo.com/article/127583
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章