写文章

spring整合hibernate

2018-11-27 10:43:44

1329 | 0 | 0

个人感觉比较好的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);
	}
}


0

收藏
分享