Spring3.1.2与Hibernate4.1.8整合

spring 同时被 2 个专栏收录
5 篇文章 0 订阅
2 篇文章 0 订阅

整合Spring3.1.2 与 Hibernate 4.1.8

首先准备整合jar:


Spring3.1.2:

org.springframework.aop-3.1.2.RELEASE.jar
org.springframework.asm-3.1.2.RELEASE.jar
org.springframework.aspects-3.1.2.RELEASE.jar
org.springframework.beans-3.1.2.RELEASE.jar
org.springframework.context.support-3.1.2.RELEASE.jar
org.springframework.context-3.1.2.RELEASE.jar
org.springframework.core-3.1.2.RELEASE.jar
org.springframework.expression-3.1.2.RELEASE.jar(使用表达式${})
org.springframework.jdbc-3.1.2.RELEASE.jar
org.springframework.orm-3.1.2.RELEASE.jar
org.springframework.transaction-3.1.2.RELEASE.jar


Hibernate4.1.8:

--------------required下面---------------
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.8.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
----------------------------

-----proxool-------
proxool-0.9.1.jar
proxool-cglib.jar

其他依赖包
aopalliance-1.0.jar
aspectjrt-1.7.0.jar
aspectjweaver-1.7.0.jar
commons-logging-1.1.1.jar

--数据库
mysql-connector-java-5.1.21.jar


整合示例:

UserModel:

UserModel:

package cn.sh.model;

public class UserModel {
	private int id;
	private String username;
	private String password;
	
	--------getter & setter------
}

user.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="cn.sh.model.UserModel" table="user">
		<id name="id" column="id">
			<generator class="native" />
		</id>
		<property name="username" column="username" />
		<property name="password" column="password" />
	</class>
</hibernate-mapping>

resources/jdbc.properties:
proxool.maxConnCount=10
proxool.minConnCount=5
proxool.statistics=1m,15m,1h,1d
proxool.simultaneousBuildThrottle=30
proxool.trace=false

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh
jdbc.username=root
jdbc.password=admin

resources/applicationContext-common.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: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-3.0.xsd
					       http://www.springframework.org/schema/tx 
					       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
					       http://www.springframework.org/schema/aop 
					       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!-- 引入配置文件 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>
     
     <!-- 数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
	    <property name="targetDataSource">
	        <bean class="org.logicalcobwebs.proxool.ProxoolDataSource">
	            <property name="driver" value="${jdbc.driverClassName}" />
	            <property name="driverUrl" value="${jdbc.url}" />
	            <property name="user" value="${jdbc.username}" />
	            <property name="password" value="${jdbc.password}" />
	            <property name="maximumConnectionCount" value="${proxool.maxConnCount}" />
	            <property name="minimumConnectionCount" value="${proxool.minConnCount}" />
	            <property name="statistics" value="${proxool.statistics}" />
	            <property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
	            <property name="trace" value="${proxool.trace}" />
	        </bean>
	    </property>
	</bean>
	
	<!--  -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<value>cn/sh/model/user.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.HSQLDialect
				hibernate.show_sql=true
			</value>
		</property>
	</bean>
	
	<!-- 声明式事务 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<aop:config>
		<aop:pointcut id="productServiceMethods" expression="execution(* cn.sh.service..*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
	</aop:config>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="increasePrice*" propagation="REQUIRED" />
			<tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW" />
			<tx:method name="*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
	
</beans>

整合测试:

public class SpringHibernateTest {

	private SessionFactory sessionFactory;
	private ApplicationContext ctx;

	@Before
	public void setUp() {
		String[] configLocations = new String[] {"classpath:applicationContext-*.xml"};
		ctx = new ClassPathXmlApplicationContext(configLocations);
		sessionFactory = ctx.getBean("sessionFactory", SessionFactory.class);
	}
	
	@Test
	public void testSessionFactory(){
		System.out.println(sessionFactory);
		System.out.println(ctx.getBean("dataSource"));
		Session session = sessionFactory.openSession(); 
        UserModel model = new UserModel();  
        model.setUsername("wangwu");
        model.setPassword("123456");
		session.save(model);
	}
}























  • 1
    点赞
  • 2
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值