• 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"的配置需要与hibernate.cfg.xml建立联系    
    17      -->
    18     
    19     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    20         <property name="configLocation">
    21             <value>classpath:hibernate.cfg.xml</value>
    22         </property>
    23     </bean>  
    24 
    25     
    26     
    27     
    28     <!-- 加载配置文件jdbc.properties ,在其中保存数据库信息,方便日后维护-->
    29     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    30         <property name="locations">
    31             <value>classpath:jdbc.properties</value>
    32         </property>
    33     </bean>
    34     
    35     <!-- 配置数据源 -->
    36     <bean id="dataSource" destroy-method="close"
    37         class="org.apache.commons.dbcp.BasicDataSource">
    38         <property name="driverClassName" value="${jdbc.driverClassName}" />
    39         <property name="url" value="${jdbc.url}" />
    40         <property name="username" value="${jdbc.username}" />
    41         <property name="password" value="${jdbc.password}" />
    42     </bean>
    43     
    44     <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    45     
    46         <!-- 引用数据源 -->
    47         <property name="dataSource" ref="dataSource"/>
    48         
    49         <!-- 加载实体类的映射文件位置及名称,可以使用通配符 -->
    50         <property name="mappingResources">
    51           <list>
    52             <value>com/yanwu/www/spring_hibernate/domain/Classes.hbm.xml</value>
    53           </list>
    54         </property>
    55         
    56         <!-- 配置数据库方言,这里是MySQL -->
    57         <property name="hibernateProperties">
    58           <value>
    59             hibernate.dialect=org.hibernate.dialect.MySQLDialect
    60           </value>
    61         </property>
    62 
    63     </bean>
    64     
    65     <bean id="classesDao" class="com.yanwu.www.spring_hibernate.dao.impl.ClassesDaoImpl">
    66         
    67         <property name="sessionFactory">
    68             <ref bean="sessionFactory1"/>
    69         </property>
    70     </bean>
    71     
    72     <bean id="classesService" class="com.yanwu.www.spring_hibernate.service.impl.ClassesServiceImpl" >
    73         <property name="classesDao">
    74             <ref bean="classesDao"/>
    75         </property>
    76     </bean>
    77     
    78      <!-- 配置Spring声明式事务 -->
    79     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    80         <property name="sessionFactory">
    81             <ref bean="sessionFactory1"/>
    82         </property>
    83     </bean>
    84     
    85      <!-- 配置事务事务属性 -->
    86     
    87     <tx:advice id="tx" transaction-manager="transactionManager">
    88         <tx:attributes>
    89             <!-- 方法对应的传播属性 -->    
    90             <tx:method name="save*" read-only="false"/>
    91         </tx:attributes>
    92     </tx:advice>
    93     
    94     <!-- 配置事务切点,并把切点和事务属性关联起来,事务控制位置,一般在业务实现层 -->
    95     <aop:config>
    96         <aop:pointcut expression="execution(* com.yanwu.www.spring_hibernate.service.impl.ClassesServiceImpl.*(..))" id="perform"/>
    97         <aop:advisor advice-ref="tx" pointcut-ref="perform"/>
    98     </aop:config>
    99 </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

  • 相关阅读:
    【基于初学者】SSH+Maven实现Excel导出功能
    【基于初学者】IDEA中Git的使用
    【基于初学者】通过eclipse工具如何创建Spring Boot工程
    【基于初学者】基于ssm框架实现不同用户显示不同的菜单模块
    【基于初学者】Struts框架
    【基于初学者】Maven相关配置和创建
    树莓派2+无线网卡=钓鱼热点
    mockjs 项目实战踩坑
    上传文件 上传图片 源码跟思路
    css 浮动及清除浮动 详细讲解
  • 原文地址:https://www.cnblogs.com/harvey2017/p/6843594.html
Copyright © 2020-2023  润新知