由于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. tableviewCell折叠状态2

    // //  LHQContentViewCell.h //  11 - 投资管理 - 李洪强 // //  Created by vic fan on 16/4/12. //  Copyright ...

  2. _jobdu_1001

    /************************************************************************/ /* 题目描述: This time, you a ...

  3. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

    E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. JavaScript系列:event.bubbles属性(并不是所有的事件都具有冒泡)

    地址 https://www.w3.org/TR/DOM-Level-3-Events/#h3_interface-Event https://segmentfault.com/q/101000000 ...

  5. 对.net系统架构改造的一点经验和教训(转)

    在互联网行业,基于Unix/Linux的网站系统架构毫无疑问是当今主流的架构解决方案,这不仅仅是因为Linux本身足够的开放性,更因为围绕传统Unix/Linux社区有大量的成熟开源解决方案,覆盖了网 ...

  6. POJ 1185 经典状压dp

    做了很久的题 有注释 #include<stdio.h> #include<string.h> #include<algorithm> #include<ma ...

  7. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [7] APP 错误日志接口

    APP 上线以后可能遇到的问题: ① APP 强退 ② 数据加载失败 ③ APP 潜在问题 错误日志需要记录的内容 数据表 error_log 字段: id app_id:app 类别 id did: ...

  8. PHP 开发 APP 接口 学习笔记与总结 - XML 方式封装通信接口

    1.PHP 生成 XML 数据 ① 拼接字符串 ② 使用系统类(DomDocument,XMLWriter,SimpleXML) 例1 使用 PHP 系统类中的 DomDocument 类: < ...

  9. PHP生成随机密码的4种方法及性能对比

    PHP生成随机密码的4种方法及性能对比 http://www.php100.com/html/it/biancheng/2015/0422/8926.html 来源:露兜博客   时间:2015-04 ...

  10. window.open()弹出窗口防止被禁

    window.open(),顾名思义,是指在当前浏览器窗口弹出另一个浏览器窗口. 因为多种原因,浏览对window.open弹出的窗口做了多方限制.限制不同,肯定会造成各浏览器弹出窗口的差异. 大部分 ...