阅读目录
一、概述
Spring整合Hibernate有什么好处?
1、由IOC容器来管理Hibernate的SessionFactory
2、让Hibernate使用上Spring的声明式事务
二、整合步骤
1.大致步骤
1)、加入Hibernate
- 加入hibernate jar包
- 添加Hibernate的配置文件:hibernate.cfg.xml
2)、加入Spring
- 加入spring jar包
- 加入Spring配置文件
3)、编写测试类
下图为项目建立的目录结构:
2.具体分析:
1).导入jar包(若是maven工程则可以更加简单)
2).写类及配置文件
a).编写domain类
Classes.java代码如下:
package com.yanwu.www.spring_hibernate.domain; import java.io.Serializable; public class Classes implements Serializable{
private Long cid;
private String cname;
public Long getCid() {
return cid;
} public void setCid(Long cid) {
this.cid = cid;
} public String getCname() {
return cname;
} public void setCname(String cname) {
this.cname = cname;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} private String description;
}
映射文件Classes.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="com.yanwu.www.spring_hibernate.domain.Classes">
<id name="cid" type="java.lang.Long" length="5">
<column name="cid"></column>
<generator class="increment"></generator>
</id>
<property name="cname" length="20"></property>
<property name="description" length="50"></property>
</class>
</hibernate-mapping>
b).编写dao类
ClassesDao.java代码如下:
package com.yanwu.www.spring_hibernate.dao; import com.yanwu.www.spring_hibernate.domain.Classes; public interface ClassesDao {
public void saveClasses(Classes classes);
}
c).编写dao实现类Impl
ClassesDaoImpl.java代码如下:
package com.yanwu.www.spring_hibernate.dao.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.yanwu.www.spring_hibernate.dao.ClassesDao;
import com.yanwu.www.spring_hibernate.domain.Classes; public class ClassesDaoImpl extends HibernateDaoSupport implements ClassesDao{ @Override
public void saveClasses(Classes classes) {
this.getHibernateTemplate().save(classes);
} }
d).编写service类(业务层接口)
ClassesService.java代码如下:
package com.yanwu.www.spring_hibernate.service; import com.yanwu.www.spring_hibernate.domain.Classes; public interface ClassesService {
public void saveClasses(Classes classes);
}
e).编写service实现类Impl(业务层接口的实现)
ClassesServiceImpl.java代码如下:
package com.yanwu.www.spring_hibernate.service.impl;
import com.yanwu.www.spring_hibernate.dao.ClassesDao;
import com.yanwu.www.spring_hibernate.domain.Classes;
import com.yanwu.www.spring_hibernate.service.ClassesService; public class ClassesServiceImpl implements ClassesService{
private ClassesDao classesDao;
public ClassesDao getClassesDao() {
return classesDao;
}
public void setClassesDao(ClassesDao classesDao) {
this.classesDao = classesDao;
}
public void saveClasses(Classes classes){
this.classesDao.saveClasses(classes);
}
}
f).spring的配置
applicationContext.xml的代码如下:
说明:利用配置来得到session工厂
<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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--
Session 工厂配置
LocalSessionFactoryBean这个类实例化我们可以得到sessionfactory。
该类中有个属性configLocation通过这个属性我们就可以hibernate.cfg.xml建立联系了。
id="sessionFactory"的配置需要与hibernate.cfg.xml建立联系
--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean> <!-- 加载配置文件jdbc.properties ,在其中保存数据库信息,方便日后维护-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean> <!-- 配置数据源 -->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 引用数据源 -->
<property name="dataSource" ref="dataSource"/> <!-- 加载实体类的映射文件位置及名称,可以使用通配符 -->
<property name="mappingResources">
<list>
<value>com/yanwu/www/spring_hibernate/domain/Classes.hbm.xml</value>
</list>
</property> <!-- 配置数据库方言,这里是MySQL -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property> </bean> <bean id="classesDao" class="com.yanwu.www.spring_hibernate.dao.impl.ClassesDaoImpl"> <property name="sessionFactory">
<ref bean="sessionFactory1"/>
</property>
</bean> <bean id="classesService" class="com.yanwu.www.spring_hibernate.service.impl.ClassesServiceImpl" >
<property name="classesDao">
<ref bean="classesDao"/>
</property>
</bean> <!-- 配置Spring声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory1"/>
</property>
</bean> <!-- 配置事务事务属性 --> <tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!-- 方法对应的传播属性 -->
<tx:method name="save*" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- 配置事务切点,并把切点和事务属性关联起来,事务控制位置,一般在业务实现层 -->
<aop:config>
<aop:pointcut expression="execution(* com.yanwu.www.spring_hibernate.service.impl.ClassesServiceImpl.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
</beans>
g).hibernate和数据库信息的配置(可选)
hibernate.cfg.xml代码如下:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--
数据库的用户名
-->
<property name="connection.username">root</property>
<!--
密码
-->
<property name="connection.password">root</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<!--
方言
告诉hibernate,要操作的数据库是mysql
-->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!--
导入驱动
-->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!--
validate
只检查结构
update
检查结构,更新或者创建表
create
每次启动hibernate时,都要创建表
create-drop
启动hibernate时创建表,当hibernate关闭时,删除表
-->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/yanwu/www/spring_hibernate/domain/Classes.hbm.xml" /> </session-factory>
</hibernate-configuration>
jdbc.properties代码如下:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/test
jdbc.username=root
jdbc.password=root
3).写测试类
ClassesServiceTest.java代码如下:
package com.yanwu.www.spring_hibernate.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yanwu.www.spring_hibernate.domain.Classes;
import com.yanwu.www.spring_hibernate.service.ClassesService; public class ClassesServiceTest {
@Test
public void testSaveClasses(){
ApplicationContext context = new ClassPathXmlApplicationContext("com/yanwu/www/spring_hibernate/config/applicationContext.xml");
ClassesService classesService = (ClassesService) context.getBean("classesService");
Classes classes = new Classes();
classes.setCname("aa");
classesService.saveClasses(classes);
}
}
至此,完成了spring和hibernate的整合,第一次写博客,请大家多多指导。提供项目下载地址:https://pan.baidu.com/s/1kUCohYf