首页 > 代码库 > spring和hibernate的整合

spring和hibernate的整合

阅读目录

一、概述

二、整合步骤

  1.大致步骤

  2.具体分析

 

 

一、概述 

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代码如下:

技术分享
 1 package com.yanwu.www.spring_hibernate.domain;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Classes implements Serializable{
 6     private Long cid;
 7     private String cname;
 8     public Long getCid() {
 9         return cid;
10     }
11 
12     public void setCid(Long cid) {
13         this.cid = cid;
14     }
15 
16     public String getCname() {
17         return cname;
18     }
19 
20     public void setCname(String cname) {
21         this.cname = cname;
22     }
23 
24     public String getDescription() {
25         return description;
26     }
27 
28     public void setDescription(String description) {
29         this.description = description;
30     }
31 
32     private String description;
33 }
View Code

 

映射文件Classes.hbm.xml代码如下:

技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="com.yanwu.www.spring_hibernate.domain.Classes">
 6         <id name="cid" type="java.lang.Long" length="5">
 7             <column name="cid"></column>
 8             <generator class="increment"></generator>
 9         </id>
10         <property name="cname" length="20"></property>
11         <property name="description" length="50"></property>
12     </class>
13 </hibernate-mapping>
View Code

 

b).编写dao类

技术分享

 

 ClassesDao.java代码如下:

技术分享
1 package com.yanwu.www.spring_hibernate.dao;
2 
3 import com.yanwu.www.spring_hibernate.domain.Classes;
4 
5 public interface ClassesDao {
6     public void saveClasses(Classes classes);
7 }
View Code

 

c).编写dao实现类Impl

技术分享

 

ClassesDaoImpl.java代码如下:

技术分享
 1 package com.yanwu.www.spring_hibernate.dao.impl;
 2 
 3 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 4 
 5 import com.yanwu.www.spring_hibernate.dao.ClassesDao;
 6 import com.yanwu.www.spring_hibernate.domain.Classes;
 7 
 8 public class ClassesDaoImpl extends HibernateDaoSupport implements ClassesDao{
 9 
10     @Override
11     public void saveClasses(Classes classes) {
12         this.getHibernateTemplate().save(classes);
13     }
14     
15 }
View Code

 

 

d).编写service类(业务层接口)

技术分享

 

ClassesService.java代码如下:

技术分享
1 package com.yanwu.www.spring_hibernate.service;
2 
3 import com.yanwu.www.spring_hibernate.domain.Classes;
4 
5 public interface ClassesService {
6     public void saveClasses(Classes classes);
7 }
View Code

 

 

 

e).编写service实现类Impl(业务层接口的实现)

技术分享

 

ClassesServiceImpl.java代码如下:

技术分享
 1 package com.yanwu.www.spring_hibernate.service.impl;
 2 import com.yanwu.www.spring_hibernate.dao.ClassesDao;
 3 import com.yanwu.www.spring_hibernate.domain.Classes;
 4 import com.yanwu.www.spring_hibernate.service.ClassesService;
 5 
 6 public class ClassesServiceImpl implements ClassesService{
 7     private ClassesDao classesDao;
 8     public ClassesDao getClassesDao() {
 9         return classesDao;
10     }
11     public void setClassesDao(ClassesDao classesDao) {
12         this.classesDao = classesDao;
13     }
14     public void saveClasses(Classes classes){
15         this.classesDao.saveClasses(classes);
16     }
17 }
View Code

 

 

 

f).spring的配置

技术分享

 

applicationContext.xml的代码如下:

 

说明:配置提供两种方式来得到session工厂

技术分享
  1 <beans xmlns="http://www.springframework.org/schema/beans"
  2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3     xmlns:aop="http://www.springframework.org/schema/aop"
  4     xmlns:tx="http://www.springframework.org/schema/tx"
  5     xsi:schemaLocation="
  6         http://www.springframework.org/schema/beans 
  7         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8         http://www.springframework.org/schema/aop 
  9         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
 10         http://www.springframework.org/schema/tx 
 11         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 12     <!-- 
 13         Session 工厂配置 
 14             LocalSessionFactoryBean这个类实例化我们可以得到sessionfactory。
 15             该类中有个属性configLocation通过这个属性我们就可以hibernate.cfg.xml建立联系了。    
 16             若id="sessionFactory"放开则不需要id="sessionFactory1"的配置
 17             提供两种方法来配置Session工厂,
 18             id="sessionFactory"的配置需要与hibernate.cfg.xml建立联系
 19             id="sessionFactory1"的配置则完全不需要hibernate.cfg.xml    
 20      -->
 21     <!-- 
 22     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 23         <property name="configLocation">
 24             <value>classpath:hibernate.cfg.xml</value>
 25         </property>
 26     </bean>  
 27     -->
 28     
 29     
 30     
 31     <!-- 加载配置文件jdbc.properties ,在其中保存数据库信息,方便日后维护-->
 32     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 33         <property name="locations">
 34             <value>classpath:jdbc.properties</value>
 35         </property>
 36     </bean>
 37     
 38     <!-- 配置数据源 -->
 39     <bean id="dataSource" destroy-method="close"
 40         class="org.apache.commons.dbcp.BasicDataSource">
 41         <property name="driverClassName" value="${jdbc.driverClassName}" />
 42         <property name="url" value="${jdbc.url}" />
 43         <property name="username" value="${jdbc.username}" />
 44         <property name="password" value="${jdbc.password}" />
 45     </bean>
 46     
 47     <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 48     
 49         <!-- 引用数据源 -->
 50         <property name="dataSource" ref="dataSource"/>
 51         
 52         <!-- 加载实体类的映射文件位置及名称,可以使用通配符 -->
 53         <property name="mappingResources">
 54           <list>
 55             <value>com/yanwu/www/spring_hibernate/domain/Classes.hbm.xml</value>
 56           </list>
 57         </property>
 58         
 59         <!-- 配置数据库方言,这里是MySQL -->
 60         <property name="hibernateProperties">
 61           <value>
 62             hibernate.dialect=org.hibernate.dialect.MySQLDialect
 63           </value>
 64         </property>
 65 
 66     </bean>
 67     
 68     <bean id="classesDao" class="com.yanwu.www.spring_hibernate.dao.impl.ClassesDaoImpl">
 69         
 70         <property name="sessionFactory">
 71             <ref bean="sessionFactory1"/>
 72         </property>
 73     </bean>
 74     
 75     <bean id="classesService" class="com.yanwu.www.spring_hibernate.service.impl.ClassesServiceImpl" >
 76         <property name="classesDao">
 77             <ref bean="classesDao"/>
 78         </property>
 79     </bean>
 80     
 81      <!-- 配置Spring声明式事务 -->
 82     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 83         <property name="sessionFactory">
 84             <ref bean="sessionFactory1"/>
 85         </property>
 86     </bean>
 87     
 88      <!-- 配置事务事务属性 -->
 89     
 90     <tx:advice id="tx" transaction-manager="transactionManager">
 91         <tx:attributes>
 92             <!-- 方法对应的传播属性 -->    
 93             <tx:method name="save*" read-only="false"/>
 94         </tx:attributes>
 95     </tx:advice>
 96     
 97     <!-- 配置事务切点,并把切点和事务属性关联起来,事务控制位置,一般在业务实现层 -->
 98     <aop:config>
 99         <aop:pointcut expression="execution(* com.yanwu.www.spring_hibernate.service.impl.ClassesServiceImpl.*(..))" id="perform"/>
100         <aop:advisor advice-ref="tx" pointcut-ref="perform"/>
101     </aop:config>
102 </beans>
View Code

 

 

g).hibernate和数据库信息的配置(可选)

技术分享

 

hibernate.cfg.xml代码如下:

技术分享
 1 <?xml version=‘1.0‘ encoding=‘utf-8‘?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6 <session-factory>
 7     <!-- 
 8         数据库的用户名
 9     -->
10     <property name="connection.username">root</property>
11     <!-- 
12         密码
13     -->
14     <property name="connection.password">root</property>
15     <!-- 
16         url
17     -->
18     <property name="connection.url">
19         jdbc:mysql://localhost:3306/test
20     </property>
21     <!-- 
22         方言
23         告诉hibernate,要操作的数据库是mysql
24     -->
25     <property name="dialect">
26         org.hibernate.dialect.MySQLDialect
27     </property>
28     <!-- 
29         导入驱动
30     -->
31     <property name="connection.driver_class">
32         com.mysql.jdbc.Driver
33     </property>
34     <!-- 
35         validate
36         只检查结构
37         update
38         检查结构,更新或者创建表
39         create
40         每次启动hibernate时,都要创建表
41         create-drop
42         启动hibernate时创建表,当hibernate关闭时,删除表
43     -->
44     <property name="hbm2ddl.auto">update</property>
45     <property name="show_sql">true</property>
46     <property name="format_sql">true</property>
47     <mapping resource="com/yanwu/www/spring_hibernate/domain/Classes.hbm.xml" />
48 
49 
50 </session-factory>
51 </hibernate-configuration>
View Code

 

 

jdbc.properties代码如下:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/test
jdbc.username=root
jdbc.password=root

 

 

   3).写测试类

技术分享

 

 ClassesServiceTest.java代码如下:

技术分享
 1 package com.yanwu.www.spring_hibernate.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 import com.yanwu.www.spring_hibernate.domain.Classes;
 7 import com.yanwu.www.spring_hibernate.service.ClassesService;
 8 
 9 public class ClassesServiceTest {
10     @Test
11     public void testSaveClasses(){
12         ApplicationContext context = new ClassPathXmlApplicationContext("com/yanwu/www/spring_hibernate/config/applicationContext.xml");
13         ClassesService classesService = (ClassesService) context.getBean("classesService");
14         Classes classes = new Classes();
15         classes.setCname("aa");
16         classesService.saveClasses(classes);
17     }
18 }
View Code

 

 

 

至此,完成了spring和hibernate的整合,第一次写博客,请大家多多指导。提供项目下载地址:https://pan.baidu.com/s/1kUCohYf

 

spring和hibernate的整合