整合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:
05 | public class UserModel { |
07 | private String username; |
08 | private String password; |
10 | --------getter & setter------ |
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"> |
06 | < class name = "cn.sh.model.UserModel" table = "user" > |
07 | < id name = "id" column = "id" > |
08 | < generator class = "native" /> |
10 | < property name = "username" column = "username" /> |
11 | < property name = "password" column = "password" /> |
resources/jdbc.properties:
01 | proxool.maxConnCount= 10 |
03 | proxool.statistics=1m,15m,1h,1d |
04 | proxool.simultaneousBuildThrottle= 30 |
07 | jdbc.driverClassName=com.mysql.jdbc.Driver |
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"> |
14 | < bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > |
15 | < property name = "locations" > |
17 | < value >classpath:jdbc.properties</ value > |
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}" /> |
40 | < bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean" > |
41 | < property name = "dataSource" ref = "dataSource" /> |
42 | < property name = "mappingResources" > |
44 | < value >cn/sh/model/user.hbm.xml</ value > |
47 | < property name = "hibernateProperties" > |
49 | hibernate.dialect=org.hibernate.dialect.HSQLDialect |
50 | hibernate.show_sql=true |
56 | < bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager" > |
57 | < property name = "sessionFactory" ref = "sessionFactory" /> |
61 | < aop:pointcut id = "productServiceMethods" expression = "execution(* cn.sh.service..*.*(..))" /> |
62 | < aop:advisor advice-ref = "txAdvice" pointcut-ref = "productServiceMethods" /> |
65 | < tx:advice id = "txAdvice" transaction-manager = "transactionManager" > |
67 | < tx:method name = "increasePrice*" propagation = "REQUIRED" /> |
68 | < tx:method name = "someOtherBusinessMethod" propagation = "REQUIRES_NEW" /> |
69 | < tx:method name = "*" propagation = "SUPPORTS" read-only = "true" /> |
整合测试:
01 | public class SpringHibernateTest { |
03 | private SessionFactory sessionFactory; |
04 | private ApplicationContext ctx; |
08 | String[] configLocations = new String[] { "classpath:applicationContext-*.xml" }; |
09 | ctx = new ClassPathXmlApplicationContext(configLocations); |
10 | sessionFactory = ctx.getBean( "sessionFactory" , SessionFactory. class ); |
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" ); |
来自:http://blog.csdn.net/cjj3930337/article/details/8217135