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:
- 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);
- }
- }
Spring3.1.2与Hibernate4.1.8整合的更多相关文章
- Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合
java教程|Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合教程并测试成功一.创建项目二.搭建struts-2.3.4.11.struts2必须的Jar包(放到W ...
- springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能
转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包 数据库表 数据库表就不用教大家了,一张表,很简 ...
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)
Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...
- Struts2+Hibernate4+Spring4框架整合搭建Java项目原型
收藏 http://www.cnblogs.com/mageguoshi/p/5850956.html Struts2+Hibernate4+Spring4框架整合搭建Java项目原型
- Struts2.3.4.1 + Spring3.1.2 + Hibernate4.1.6整合
1. Jar包 2. web.xml配置 3. struts.xml配置 4. hibernate.cfg.xml配置 5. applicationContext.xml配置 6. log4j.pro ...
- 最新版本的Struts2+Spring4+Hibernate4三大框架整合(截止2014-10-15,提供源码下载)
一. 项目名称:S2316S411H436 项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 + Quartz2.2.1 源代码下载地址: 基本版:ht ...
- spring4+hibernate4+struts2项目整合的步骤及注意事项
首先,在整合框架之前,我们需要知道Spring框架在普通Java project和Web project中是略有不同的. 这个不同地方就在于创建IOC容器实例的方式不同,在普通java工程中,可以在m ...
- hibernate4.3.8整合struts2过程中遇到的问题
1.遇到的异常: Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to ...
- spring3.1的BeanFactory与Quartz1.8整合
spring的applicationContext.xml配置文件: 加入 <bean id="myJob" class="org.springframework. ...
随机推荐
- 程序设计实习MOOC / 程序设计与算法(三)第一周测验
作业题: 7. 填空(2分)简单的swap 通过码是 ( 请参考公告中的“关于编程作业的说明”完成编程作业(请注意,编程题都要求提交通过码,在openjudge上提交了程序并且通过以后,就可以下载到通 ...
- BZOJ 1861: [Zjoi2006]Book 书架 (splay)
1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1453 Solved: 822[Submit][Stat ...
- CentOS7.6安装rime輸入法
# solve dependencyyum install -y gcc gcc-c++ boost boost-devel cmake make cmake3yum install glog glo ...
- 学习Nodejs的第一步
最近看了几本关于Node.js的书,本来个人技术分享网站http://yuanbo88.com/是打算用Node.js作为服务器端语言来处理后台的,后来又改成了PHP(也是自己研究,毕竟网上DEMO多 ...
- Django实战(17):ajax !
现在让我们来通过ajax请求后台服务.当然首选要实现后台服务.关于“加入购物车”,我们需要的服务是这样定义的: url: http://localhost:8000/depotapp/API/c ...
- redhat重置密码
当忘记虚拟机密码时怎么办? 1.启动虚拟机,当虚拟机显示输入enter启动时输入e 2.再次用上下键选中你平时启动linux的那一项(类似于kernel /boot/vmlinuz-2.4.18-14 ...
- 这套完美的Java环境安装教程,完整,详细,清晰可观,让你一目了然,简单易懂。⊙﹏⊙
JDK下载与安装教程 2017年06月18日 22:53:16 Danishlyy1995 阅读数:349980 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- RMQ_第一弹_Sparse Table
title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 ...
- python学习笔记 - for循环: 遍历字典, 分别打印key, value, key:value
#遍历字典, 分别打印key, value, key:value emp = {'name':'Tom', 'age':20, 'salary' : 8800.00} for k in emp.key ...
- anaconda安装tensorflow后pip安装jieba出错的问题
安装jieba出错,参考https://www.cnblogs.com/minsons/p/7872647.html TypeError: parse() got an unexpected keyw ...