由于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. 几种进入mysql的方法

    1.首先mysql服务得打开(运行cmd命令也可以net start mysql) 2.运行cmd,打开mysq 3.mysql命令行打开mysql 4.图形管理工具,如phpMyadmin

  2. order by id asc得出的排序是什么原理

    我们要用order by id asc得出的排序应该是,4,好了原理就这么简. sql实现方法,代码如下: : 代码如下: $sql ="Select 字段 from 表名 where id ...

  3. SDR和DDR1/2/3全系列频率对照表

  4. 【新产品发布】【EVC8001 磁耦隔离式 USB 转 RS-485】

    EVC8001 是 XiaomaGee 团队打造的精品级 USB 转 RS-485 隔离转换器,全部采用最优方案,每个细节均做到最优化.最佳化.亮点举不胜举: ==================== ...

  5. mongodb3.2.3 复制集安装步骤

    mongodb 复制集 测试 node1: 172.18.20.161 47000 (主)node2: 172.18.20.162 47000 (副)node3: 172.18.20.163 4700 ...

  6. jQuery如何去判断页面是否有父页面?

    jQuery如何去判断页面是否有父页面?     是要判断当前页面是否被嵌入在frame里吗? 1 2 3 if (top != self) {     alert('我在框架里'); }

  7. 【转载 来自sdnlab】 开放网络没那么简单

    链接:开放网络没那么简单 本文是云杉网络工程师张攀对当前开源网络技术现状的一些思考和探索. 开放网元.释放数据的价值 从2012年开始至今,网络行业明显是O字辈的天下.所有我接触过了解过的组织和项目, ...

  8. http响应报文和http请求报文 详细信息

    tomcat项目本身的jar包

  9. Python的包管理工具--PIP安装使用

    最新安装方式 # wget https://bootstrap.pypa.io/get-pip.py # python get-pip.py  // 使用该方式安装已经不再要求提前安装setuptoo ...

  10. Latex公式换行、对齐

    http://blog.sina.com.cn/s/blog_64827e4c0100vnqu.html 换行后等式对齐 \begin{equation}\begin{aligned}R(S_2)&a ...