Spring(三):Spring整合Hibernate
背景:
本文主要介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5.2.9.Final项目整合搭建的过程。
开发环境简介:
1)、jdk 1.8
2)、spring-framework-4.3.8.RELEASE、hibernate-release-5.2.9.Final
引入Spring到新建项目My-SSH中
1)导入Spring的required包到My-SSH项目
新建java的dynamic web 项目,之后把spring-framework-4.3.8.RELEASE\libs下的发布包(*-4.3.8.RELEASE.jar,只拷贝这一种即可)拷贝到WebContent\WEB-INF\lib下。
除了spring的开发包外,在运行spring时,必须依赖commons-logging包,因此从http://commons.apache.org/proper/commons-logging/download_logging.cgi下载最新的commons-logging包,解压后把包commons-logging-1.2-bin\commons-logging-1.2\commons-logging-1.2.jar拷贝到WebContent\WEB-INF\lib下。
2)配置WebContent\WEB-INF\web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
version="3.0">
<display-name>My-SSH</display-name> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index</welcome-file>
</welcome-file-list> <!-- 配置启动IOC容器的Listener -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3)在My-SSH项目根路径下创建conf(Source Folder)文件夹,并添加spring配置文件applicationContext.xml
注意:
在新建该文件时,需要设定namespace包括:aop,beans,context,tx。
整合Hibernate到My-SSH项目,并与Spring整合
1)导入Hibernate的required包到My-SSH项目
把Hibernate开发包解压路径hibernate-release-5.2.9.Final\lib\required\下的所有包拷贝到WebContent\WEB-INF\lib下;
把mysql-connector-java-5.1.2-beta-bin.jar开发包拷贝到WebContent\WEB-INF\lib下;
引入C3P0开发包,把hibernate-release-5.2.9.Final\lib\optional\c3p0\下的所有包拷贝到WebContent\WEB-INF\lib下。
2)在My-SSH项目根目录下的conf文件中,新建hibernate配置文件hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--
把一下配置信息转移到jdbc.properties中。
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/my_ssh</property>
--> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
3)在My-SSH项目根目录下的conf文件中,新建jdbc.properties文件
#-----------------------------------------------------
# \u6570\u636E\u5E93\u914D\u7F6E
#-----------------------------------------------------
#\u670D\u52A1\u5668\u5730\u5740
host=127.0.0.1
dbName=my_ssh
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://${host}:3306/${dbName}
jdbc.username=root
jdbc.password=123456 #-----------------------------------------------------
# \u9002\u7528\u4E8Ec3p0\u7684\u914D\u7F6E
#-----------------------------------------------------
#-----------------------------------------------------
# c3p0\u53CD\u7A7A\u95F2\u8BBE\u7F6E\uFF0C\u9632\u6B628\u5C0F\u65F6\u5931\u6548\u95EE\u989828800
#-----------------------------------------------------
#idleConnectionTestPeriod\u8981\u5C0F\u4E8EMySQL\u7684wait_timeout
jdbc.c3p0.testConnectionOnCheckout=false
jdbc.c3p0.testConnectionOnCheckin=true
jdbc.c3p0.idleConnectionTestPeriod=3600
#-----------------------------------------------------
# c3p0\u8FDE\u63A5\u6C60\u914D\u7F6E
#-----------------------------------------------------
#initialPoolSize, minPoolSize, maxPoolSize define the number of Connections that will be pooled.
#Please ensure that minPoolSize <= maxPoolSize.
#Unreasonable values of initialPoolSize will be ignored, and minPoolSize will be used instead.
jdbc.c3p0.initialPoolSize=10
jdbc.c3p0.minPoolSize=10
jdbc.c3p0.maxPoolSize=100
#maxIdleTime defines how many seconds a Connection should be permitted to go unused before being culled from the pool.
jdbc.c3p0.maxIdleTime=3600
#-----------------------------------------------------
# hibernate\u8FDE\u63A5\u6C60\u914D\u7F6E
#-----------------------------------------------------
hibernate.connection.driverClass=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://${host}:3306/${dbName}
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
4)通过配置My-SSH项目根目录下的conf下的applicationContext.xml文件,将hibernate集成到spring中
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" file-encoding="utf-8" ignore-unresolvable="true" /> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="testConnectionOnCheckout" value="${jdbc.c3p0.testConnectionOnCheckout}"></property>
<property name="testConnectionOnCheckin" value="${jdbc.c3p0.testConnectionOnCheckin}"></property>
<property name="idleConnectionTestPeriod" value="${jdbc.c3p0.idleConnectionTestPeriod}"></property>
<property name="initialPoolSize" value="${jdbc.c3p0.initialPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.c3p0.minPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.c3p0.maxPoolSize}"></property>
<property name="maxIdleTime" value="${jdbc.c3p0.maxIdleTime}"></property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 数据源dataSource -->
<property name="dataSource" ref="dataSource" /> <!-- hibernate的配置方案一 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- hibernate的配置方案二 -->
<!--
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
</props>
</property>
--> <!-- 持久化类的位置方案一,通过包进行扫描 -->
<property name="mappingLocations" value="classpath:com/dx/ssh/entities/*.hbm.xml"></property>
<!-- spring的spring.jar的jar包内,在org.springframework.orm.hibernate3.annotation下,
有一个AnnotationSessionFactoryBean类,其中有一个属性叫做"packagesToScan", 有个方法叫setpackagesToScan(),
也就是说我可以再spring里面将这个属性给设定上。
packagesToScan是"包扫描"的意思,哪些包spring可以给我们扫描一下,看看有哪些实体类,
这一项在我们在配置文件中配置hibernate的实体类的时候可以这么配,只要给出具体的扫描范围就可以了,
不需要将实体类一个一个的写出来
-->
<!-- 持久化类的位置方案二,通过包进行扫描 -->
<!--
<property name="packagesToScan">
<list>
<value>com.dx.ssh.entities</value>
</list>
</property>
-->
<!-- 持久化类的位置方案三,通过类进行扫描 -->
<!--
<property name="annotatedClasses">
<list>
<value>com.dx.ssh.entities.Member</value>
<value>com.dx.ssh.entities.Log</value>
</list>
</property>
-->
</bean>
<!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 配置事务异常封装 -->
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <!-- 基于数据源的事务管理器 -->
<!--
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
--> <!-- 第一种方式: 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
测试是否集成成功
在My-SSH项目的src下新建包com.dx.ssh.entities,在包内添加Member及Member.hbm.xml
Member.java
package com.dx.ssh.entities;
public class Member {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Member.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-5 23:51:42 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.ssh.entities.Member" table="MEMBER">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>
运行项目,如果项目启动没有错误,并且在mysql新建的数据my_ssh库中包含表MEMBE,就说明集成成功。
Spring(三):Spring整合Hibernate的更多相关文章
- Spring学习7-Spring整合Hibernate
一.Springl为什么要整合Hibernate 二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...
- Spring Data初步--整合Hibernate
Spring Data课程中的技术介绍 Hibernate: Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 pojo 与数据库表建立映射关系 ...
- Spring再接触 整合Hibernate
首先更改配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...
- spring(三) spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- Spring(三) Spring IOC
Spring 核心之 IOC 容器 再谈 IOC 与 DI IOC(Inversion of Control)控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创 建.依赖的代码,反转给容器 ...
- Spring(三) Spring IOC 初体验
Web IOC 容器初体验 我们还是从大家最熟悉的 DispatcherServlet 开始,我们最先想到的还是 DispatcherServlet 的 init() 方法.我们发现在 Dispath ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能
1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...
随机推荐
- 手机端原生js实现下拉刷新数据
HTML结构如下: <div class="outerScroller comment"> <div class='scroll comment'> < ...
- 使用async和wait进行异步编程
本文来源于博客园-钱智慧,转载请注明出处 代码示例 // 要让一个方法成为异步方法: // - async修饰符. // - 返回类型是 Task 或者 Task<T>. 具体来说,如果函 ...
- 1-5 hibernate学习笔记(11-14章)
一,概念详解 1.持久化persistent 是指将内存中的数据保存到磁盘.数据库等存储设备中. 2.持久化对象:已经储存到磁盘或者数据库中的业务对象. 3.在java中对对象的持久化有三种方法: 1 ...
- Java爬虫爬取网站电影下载链接
之前有看过一段时间爬虫,了解了爬虫的原理,以及一些实现的方法,本项目完成于半年前,一直放在那里,现在和大家分享出来. 网络爬虫简单的原理就是把程序想象成为一个小虫子,一旦进去了一个大门,这个小虫子就像 ...
- Day3---------Linux操作系统
---恢复内容开始--- 网络基础和DOS命令 一.网络分类 1.地理位置 1).局域网(LAN) 2).城域网(MAN) 3).广域网(WAN) 2.传输介质 1).有线网 2).光纤网 3).无线 ...
- Python OJ 从入门到入门基础练习 10 题
1.天天向上的力量: 一年365天,以第1天的能力值为基数,记为1.0.当好好学习时,能力值相比前一天提高N‰:当没有学习时,由于遗忘等原因能力值相比前一天下降N‰.每天努力或放任,一年下来的能力值相 ...
- linux --> Linux 的启动流程
Linux 的启动流程 操作系统接管硬件以后发生的事情,也就是操作系统的启动流程. 因为在BIOS阶段,计算机的行为基本上被写死了,程序员可以做的事情并不多:但一旦进入操作系统,程序员几乎可以定制所有 ...
- iOS scrollView中嵌套多个tabeleView处理方案
项目中经常会有这样的需求,scrollView有个头部,当scrollView滚动的时候头部也跟着滚动,同时头部还有一个tab会锁定在某个位置,scrollView中可以放很多不同的view,这些vi ...
- 听翁恺老师mooc笔记(10)--结构
定义结构: 在程序里,如果想要表达一个数据就需要一个变量,而每个变量又都需要一个类型,之前学过C语言中有int.double.float.char等这些基础类型,还有指针.数组等.如果你要表达的数据比 ...
- JAVA设计模式之【装饰者模式】
JAVA设计模式之[装饰者模式] 装饰模式 对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮.更温馨.更实用. 在软件设计中,对已有对象(新房)的功能进行扩展(装修). 把通用功能封装在装 ...