由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQL语句,直接与对象打交道。 将Hibernate做完映射之后的对象交给Spring来管理是再合适不过的事情了, Spring也同时提供了对Hibernate的SessionFactory的集成功能。所以pring+hibernate整合对我们实际开发是非常有必要的。Spring整合hibernate有多种方式,我用的只是其中的一种,我这种不需要hibernate的配置文件,直接配置我们的beans.xml里了。

经测试需要在原先jar包上,新加入的包。

PS:在Spring中,当您每学习一个新的知识的时候,你可能不知道要加入哪个JAR包,这时不要乱加,你可以不断测试发现少哪个包就加哪个,千万别加多了,以免引起冲突。

示例:

配置文件:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" >
     <context:component-scan base-package="net.zmcheng"/>
     <bean id=" logInterceptor" class="net.zmcheng.aop.LogInterceptor"/>
     <aop:config>
          <aop:pointcut expression="execution(public * net.zmcheng.serviceImpl.UserServiceImpl.add())"  id="servicePointCut"/>
          <aop:aspect id="logAspect"  ref=" logInterceptor">
             <aop:before method="before" pointcut-ref="servicePointCut"/>
             <aop:around method="aroundMethod" pointcut-ref="servicePointCut"/>
          </aop:aspect>
     </aop:config>
     <!-- 设置数据源:提供了一个标准化的取得数据库连接的方式,一看到property我们就应该有一个意识,那就是这个类当中用这个属性的setter方法来进行注入 -->
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
           <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
           <property name="url" value="jdbc:mysql://localhost:3306/BSWS?useUnicode=true&amp;characterEncoding=UTF-8"/>
           <property name="username" value="root"/>
          <property name="password" value="password"/>
        </bean>
      <!-- 整合Hibernate -->
      <bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
           <property name="dataSource" ref="dataSource"/>
           <!-- 使用注解的方式
           <property name="annotatedClasses">
               <list><value>net.zmcheng.model.NlUser</value></list>
           </property>
            -->
           <property name="mappingResources">  
            <list>  
                <value>net/zmcheng/model/NlUser.hbm.xml</value>  
            </list>  
        </property>  
           <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
           </property>
      </bean>
</beans>

PS:一看到property我们就应该有一个意识,那就是这个类当中用这个属性的setter方法来进行注入 。

daoImpl:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package net.zmcheng.daoImpl;
 
import javax.annotation.Resource;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
 
import net.zmcheng.dao.UserDao;
import net.zmcheng.model.NlUser;
@Component
public class UserDaoImpl implements UserDao {
  public SessionFactory getSessionFactory() {
return sessionFactory;
}
  @Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private SessionFactory sessionFactory;
   public void add() {
   try{
  
   Session s = sessionFactory.openSession();
   s.beginTransaction();
   NlUser n = new NlUser();
   n.setName("nihaomahh");
   s.save(n);
   s.getTransaction().commit();
   System.out.println("添加员工成功");
   }
   catch (Exception e){
   e.printStackTrace();
   }
    }
}

model:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package net.zmcheng.model;
 
import java.util.Date;
 
@SuppressWarnings("serial")
public class NlUser implements java.io.Serializable{
 
   private Integer id;
      private String name;
      private Double money=222.4;
      private String number="1000000";
      private String psw="123456";
      public String getRpsw() {
return rpsw;
}
public void setRpsw(String rpsw) {
this.rpsw = rpsw;
}
private String rpsw;
      private Date time=new Date();
      
      public NlUser(){
       super();
      }
      
      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;
   }
   public Double getMoney() {
   return money;
   }
   public void setMoney(Double money) {
   this.money = money;
   }
   public String getNumber() {
   return number;
   }
   public void setNumber(String number) {
   this.number = number;
   }
   public String getPsw() {
   return psw;
   }
   public void setPsw(String psw) {
   this.psw = psw;
   }
   public Date getTime() {
   return time;
   }
   public void setTime(Date time) {
   this.time = time;
   }
    
}

NlUser.hbm.xml:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>
    <class name="net.zmcheng.model.NlUser" table="User" catalog="BSWS">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" length="30" not-null="true" unique="true" />
        </property>
        <property name="psw" type="string">
            <column name="psw" length="20" not-null="true" unique="true"  />
        </property>
        <property name="money" type="double">
            <column name="money" not-null="true" unique="true" />
        </property>
          <property name="time" type="timestamp">
            <column name="time" not-null="true" unique="true" />
        </property>
        <property name="number" type="string">
            <column name="number" not-null="true" unique="true" />
        </property>
      
    </class>
</hibernate-mapping>

测试结果:

成功在数据库中加入数据,其它的接口与类在之前篇博客中都贴了,本篇只贴了代码的类。

未经允许不得转载:程序没有猿 » Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能

Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能的更多相关文章

  1. 死磕Spring之IoC篇 - Spring 应用上下文 ApplicationContext

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  2. 死磕Spring之AOP篇 - Spring AOP常见面试题

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  3. 死磕Spring之AOP篇 - Spring AOP总览

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  4. 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  5. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  6. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  7. 死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  8. 死磕Spring之AOP篇 - Spring AOP注解驱动与XML配置

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  9. 死磕Spring之AOP篇 - Spring 事务详解

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

随机推荐

  1. 不要使用SBJSON(json-framework)

    不要使用SBJSON(json-framework) 文章目录 不知道为什么,在iOS开发中,有很多人使用 SBJSON (又被称作json-framework)来做JSON解析库.我想这是因为SBJ ...

  2. OpenCV Show Image cvShowImage() 使用方法

    新版的OpenCV在所有的函数和类前都加上了cv或Cv,这样很好的避免了区域污染(namespace pollution),而且不用在前面加‘cv::’,非常的使用.像之前的imshow()函数被现在 ...

  3. Java 的class文件关系

    一个java源文件javac出多个class文件出来!是怎么回事? 1. 你在一个文件里定义了几个类的时候,会出现这种情况,比如public class A {}class B {}class C { ...

  4. thinkphp的html模板中if的使用

    写的时候正好出错,我就纠结是{if}还是手册中的<if condition>,当然我使用的是手册中的用法,但是点击按钮时候还是没展开(if后的条件没执行).如图 试了好多写法,也检查了多次 ...

  5. windows实时操作系统

    最近一个项目需要用windows进行实时定时操作以实现同步功能(12ms),不过由于windows是分时系统,其可供用户使用的定时器误差较大. 通过查找发现了一个ardence公司开发的一个叫做RTX ...

  6. [APAC]查找资产表

    $sn = Read-Host -Prompt "请输入SN号(7位 or 10位)" $xl = New-Object -ComObject "Excel.Applic ...

  7. [ZZ] 景深效果(Depth of Field) , Pass1 将场景渲染到一个RenderTarget,做为清晰版, Pass2: BluredRT , Pass3: WDepth = Depth / Far_Z_Clip

    http://blog.csdn.net/xoyojank/article/details/1883520   什么是景深效果? 景深效果,简称DOF,在人眼跟光学摄像设备上很常见.如下图: 简单地来 ...

  8. T-SQL笔记

    主要是查询: select *|Cols_Name|聚合函数 from Table_Name;#这是基本的语法 聚合函数: count(*|Cols_Name)   计算表的数量 max(*|Cols ...

  9. Java中同步

    解决资源共享的同步操作,有两种方法:一是同步代码块,二是同步方法. 在需要同步的代码块加上synchronized关键字, 同步代码块时必须指定一个需要同步的对象,但一般都是将当前对象(this)设置 ...

  10. Web Storage事件无法触发

    不管是在同源其他页面还是在本页面都不能触发storage事件. <!DOCTYPE html> <html> <head> <meta charset=&qu ...