Spring3.1.2与Hibernate4.1.8整合
来源:互联网 发布:标识符c语言 编辑:程序博客网 时间:2024/05/17 09:21
整合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=10proxool.minConnCount=5proxool.statistics=1m,15m,1h,1dproxool.simultaneousBuildThrottle=30proxool.trace=falsejdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/sshjdbc.username=rootjdbc.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.HSQLDialecthibernate.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;@Beforepublic void setUp() {String[] configLocations = new String[] {"classpath:applicationContext-*.xml"};ctx = new ClassPathXmlApplicationContext(configLocations);sessionFactory = ctx.getBean("sessionFactory", SessionFactory.class);}@Testpublic 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);}}
- Spring3.1.2与Hibernate4.1.8整合
- Spring3.1.2与Hibernate4.1.8整合
- Spring3.1.2与Hibernate4.1.8整合
- Spring3.1.2与Hibernate4.1.8整合
- spring3.1与hibernate4整合
- spring3.1与hibernate4整合
- spring3与hibernate4的整合
- Spring3与Hibernate4整合配置
- spring3.29与hibernate4.45整合出错
- struts2+spring3+hibernate4整合
- hibernate4 spring3 整合
- hibernate4 spring3 整合
- spring3+Hibernate4+struts2整合
- Spring3整合Hibernate4
- spring3、hibernate4、JPA整合
- Spring3+hibernate4整合配置
- hibernate4 spring3 整合
- springmvc+hibernate4+spring3整合
- static变量 static代码块 static方法 main()中的static 变量
- C++ pair
- Freemarker中遍历List实例
- 将Web应用打包成WAR文件的方法
- 发布iOS应用程序(Application Loader)
- Spring3.1.2与Hibernate4.1.8整合
- 读源码系列----MBProgressHUD(加载等待框)
- 新建网站与新建Asp.Net Web 应用程序的区别
- 我眼中的HBase (入门理解)
- ECHO命令的使用
- jquery表单验证插件 jquery.form.js
- 简约主义—Thunderbird邮件处理
- Android onTouchEvent, onClick及onLongClick的调用机制
- 减少reflow让页面变得更快一点