由于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. 短语密码(blowfish_secret)的设置

    简单的说,phpmyadmin就是一种mysql的管理工具,安装该工具后,即可以通过web形式直接管理mysql数据,而不需要通过执行系统命令来管理,非常适合对数据库操作命令不熟悉的数据库管理者,下面 ...

  2. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  3. Apache Spark源码走读之17 -- 如何进行代码跟读

    欢迎转载,转载请注明出处,徽沪一郎 概要 今天不谈Spark中什么复杂的技术实现,只稍为聊聊如何进行代码跟读.众所周知,Spark使用scala进行开发,由于scala有众多的语法糖,很多时候代码跟着 ...

  4. twitter storm源码走读之6 -- Trident Topology执行过程分析

    欢迎转载,转载请注明出处,徽沪一郎. TridentTopology是storm提供的高层使用接口,常见的一些SQL中的操作在tridenttopology提供的api中都有类似的影射.关于Tride ...

  5. C++类型转化分析(1)

    仔细想想地位卑贱的类型转换功能(cast),其在程序设计中的地位就象goto语句一样令人鄙视.但是它还不是无法令人忍受,因为当在某些紧要的关头,类型转换还是必需的,这时它是一个必需品. 不过C风格的类 ...

  6. DateTime Related Functions

    string a = "to_date('" + dtpStart.Value.ToString("yyyy/MM/dd") + "', 'yyyy/ ...

  7. 挑战python

    00 热身 http://www.pythonchallenge.com/pc/def/0.html import math print math.pow(2,38); # 274877906944 ...

  8. vue model双向绑定

    view <div id='demo' class="container"> <input type="text" v-model='name ...

  9. java面试问道的

    1.java可重入锁 2.Hashmap原理.说说hashMap是怎样实现的(这个之前看过,顺利回答上.还回答了多线程的问题出现的原因,面试官表示很惊讶的样.用hashmap实现hashset 3.

  10. CC254x(cc2540/cc2541)的微信AirSync调试笔记

    一.前言 本尊自诩为IOT小能手,一直没涉足蓝牙实在说不过去.刚好上个月底的时候计划做个BLE设备,这阵子利用业余时间自学了BLE协议栈,了解了GATT,磕磕绊绊完成CC254x(cc2540/cc2 ...