Spring3.1.2与Hibernate4.1.8整合

整合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:
01UserModel: 
02   
03package cn.sh.model; 
04   
05public class UserModel { 
06    private int id; 
07    private String username; 
08    private String password; 
09       
10    --------getter & setter------ 
11}
user.hbm.xml
01<?xml version="1.0" encoding="UTF-8"?>   
02<!DOCTYPE hibernate-mapping PUBLIC   
03        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
04        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
05<hibernate-mapping
06    <class name="cn.sh.model.UserModel" table="user"
07        <id name="id" column="id"
08            <generator class="native" /> 
09        </id
10        <property name="username" column="username" /> 
11        <property name="password" column="password" /> 
12    </class
13</hibernate-mapping>
resources/jdbc.properties:
01proxool.maxConnCount=10 
02proxool.minConnCount=5 
03proxool.statistics=1m,15m,1h,1d 
04proxool.simultaneousBuildThrottle=30 
05proxool.trace=false 
06   
07jdbc.driverClassName=com.mysql.jdbc.Driver 
08jdbc.url=jdbc:mysql://localhost:3306/ssh 
09jdbc.username=root 
10jdbc.password=admin
resources/applicationContext-common.xml:
01<?xml version="1.0" encoding="UTF-8"?> 
02<beans xmlns="http://www.springframework.org/schema/beans" 
03       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
04       xmlns:aop="http://www.springframework.org/schema/aop" 
05       xmlns:tx="http://www.springframework.org/schema/tx" 
06       xsi:schemaLocation="http://www.springframework.org/schema/beans  
07                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
08                           http://www.springframework.org/schema/tx  
09                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
10                           http://www.springframework.org/schema/aop  
11                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
12       
13    <!-- 引入配置文件 --> 
14    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
15        <property name="locations"
16            <list
17                <value>classpath:jdbc.properties</value
18            </list
19        </property
20    </bean
21        
22     <!-- 数据源 --> 
23    <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
24        <property name="targetDataSource"
25            <bean class="org.logicalcobwebs.proxool.ProxoolDataSource"
26                <property name="driver" value="${jdbc.driverClassName}" /> 
27                <property name="driverUrl" value="${jdbc.url}" /> 
28                <property name="user" value="${jdbc.username}" /> 
29                <property name="password" value="${jdbc.password}" /> 
30                <property name="maximumConnectionCount" value="${proxool.maxConnCount}" /> 
31                <property name="minimumConnectionCount" value="${proxool.minConnCount}" /> 
32                <property name="statistics" value="${proxool.statistics}" /> 
33                <property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" /> 
34                <property name="trace" value="${proxool.trace}" /> 
35            </bean
36        </property
37    </bean
38       
39    <!--  --> 
40    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
41        <property name="dataSource" ref="dataSource" /> 
42        <property name="mappingResources"
43            <list
44                <value>cn/sh/model/user.hbm.xml</value
45            </list
46        </property
47        <property name="hibernateProperties"
48            <value
49                hibernate.dialect=org.hibernate.dialect.HSQLDialect 
50                hibernate.show_sql=true 
51            </value
52        </property
53    </bean
54       
55    <!-- 声明式事务 --> 
56    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
57        <property name="sessionFactory" ref="sessionFactory" /> 
58    </bean
59       
60    <aop:config
61        <aop:pointcut id="productServiceMethods" expression="execution(* cn.sh.service..*.*(..))" /> 
62        <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> 
63    </aop:config
64       
65    <tx:advice id="txAdvice" transaction-manager="transactionManager"
66        <tx:attributes
67            <tx:method name="increasePrice*" propagation="REQUIRED" /> 
68            <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW" /> 
69            <tx:method name="*" propagation="SUPPORTS" read-only="true" /> 
70        </tx:attributes
71    </tx:advice
72       
73       
74</beans>
整合测试:
01public class SpringHibernateTest { 
02   
03    private SessionFactory sessionFactory; 
04    private ApplicationContext ctx; 
05   
06    @Before 
07    public void setUp() { 
08        String[] configLocations = new String[] {"classpath:applicationContext-*.xml"}; 
09        ctx = new ClassPathXmlApplicationContext(configLocations); 
10        sessionFactory = ctx.getBean("sessionFactory", SessionFactory.class); 
11    
12       
13    @Test 
14    public void testSessionFactory(){ 
15        System.out.println(sessionFactory); 
16        System.out.println(ctx.getBean("dataSource")); 
17        Session session = sessionFactory.openSession();  
18        UserModel model = new UserModel();   
19        model.setUsername("wangwu"); 
20        model.setPassword("123456"); 
21        session.save(model); 
22    
23}
来自:http://blog.csdn.net/cjj3930337/article/details/8217135
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值