spring 整合 hibernate:

  hibernate :对数据库交互

  spring: ioc   aop

整合点:

    1.sessionFactory对象不再由hibernate生成,交由spring生成,也就是说数据库连接信息   全局配置   映射文件的配置  由spring完成

    2.ioc 管理dao对象  baseDao对象

    3.aop 事务的控制

步骤:

    1.普通工程  copy jar 包

    2.配置applicationContext-resource.xml

        配置 sessionFactory  配置 dataSourse

        数据库连接配置   全局配置   映射文件的配置             属性名来自于底层属性,固定写法

    3.写po  及映射文件   以前怎么写的现在还是怎么写

    4.写dao      dao内必须有sessionFactory属性

    5.配置applicationContext-dao.xml

    6.测试即可

po:

public class Students {
  private Integer stuId;
  private String stuName;
  private String email;
  private Integer age;
  private Integer sex;
  private Integer cid;
  public Integer getStuId() {
    return stuId;
  }
  public void setStuId(Integer stuId) {
    this.stuId = stuId;
  }
  public String getStuName() {
    return stuName;
  }
  public void setStuName(String stuName) {
    this.stuName = stuName;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public Integer getAge() {
    return age;
  }
  public void setAge(Integer age) {
    this.age = age;
  }
  public Integer getSex() {
    return sex;
  }
  public void setSex(Integer sex) {
    this.sex = sex;
  }
  public Integer getCid() {
    return cid;
  }
  public void setCid(Integer cid) {
    this.cid = cid;
  }
}

Students.hbm.xml:

<?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
  package="com.huawei.sh.po">
  <class name="Students" table="test_students" >
    <id name="stuId" column="stu_id" >
      <generator class="sequence" >
        <param name="sequence">seq_test_students</param>
      </generator>
    </id>
    <property name="stuName" column="stu_name" />
    <property name="email" />
    <property name="age" />
    <property name="sex" />
    <property name="cid" />
  </class>
</hibernate-mapping>

dao:

public class StudentsDao {
  private SessionFactory sessionFactory;
  public void addStudents(Students stu){
    Session session = sessionFactory.openSession();
    Transaction tx =session.beginTransaction();
    session.save(stu);
    tx.commit();
    session.close();
  }
  public SessionFactory getSessionFactory() {
    return sessionFactory;
  }
  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }
  public static void main(String[] args) {
    Students stu = new Students();
    stu.setStuName("张三");
    stu.setEmail("hui@huawei.com");
    String[] res = new String[]{"applicationContext-dao.xml","applicationContext-resource.xml"};
    ApplicationContext context = new ClassPathXmlApplicationContext(res);
    StudentsDao stuDao =(StudentsDao) context.getBean("studentsDao");
    stuDao.addStudents(stu);
  }
}

applicationContext-dao.xml:

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
  <bean id="studentsDao" class="com.huawei.sh.dao.StudentsDao">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
</beans>

applicationContext-resource.xml:

<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
  <!--<context:component-scan base-package="com.chdsxt" />
  <aop:aspectj-autoproxy /> 支持AOP的注解方式 使用注解方案 必写这两项 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
    <property name="username" value="scott" />
    <property name="password" value="tiger" />
  </bean>
  <!-- 数据库核心配置 -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <!-- 数据库连接配置 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 全局性配置 -->
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</prop>
        <prop key="hibernate.show_sql" >true</prop>
        <prop key="hibernate.format_sql" >true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
      </props>
    </property>
    <!-- 映射文件对应配置 -->
    <property name="mappingResources">
      <list>
        <value>com/huawei/sh/po/Students.hbm.xml</value>
      </list>
    </property>
  </bean>
</beans>

spring 整合 hibernate xml配置的更多相关文章

  1. spring 整合 struts2 xml配置

    整合之前要搞清楚struts2是什么; struts2:表现层框架  增删改查  作用域  页面跳转   异常处理  ajax 上传下载  excel   调用service spring :IOC/ ...

  2. Spring第十一篇——–Spring整合Hibernate之配置数据源

    DataSource(数据源)提供了一个标准化的取得数据库连接的方式,通过getConnection()方法即可取得数据库的连接,Spring也提供了数据库连接池(DataBase connectio ...

  3. Spring整合Hibernate的XML文件配置,以及web.xml文件配置

    利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...

  4. Spring整合Hibernate的时候使用hibernate.cfg.xml

    Spring整合Hibernate其实也就是把Hibernate的SessionFactory对象封装成:org.springframework.orm.hibernate3.LocalSession ...

  5. 二 SSH整合:Spring整合Hibernate,无障碍整合&无核心配置整合,Hibernate模版常用方法,

    重建SSH项目 java项目可以直接复制,但是web项目除了改名字还要该配置,如下: 方式一:无障碍整合:带Hibernate配置文件 <?xml version="1.0" ...

  6. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  7. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  8. Spring 整合 Hibernate

    Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...

  9. 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能

    1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...

随机推荐

  1. SEO之H1,H2,H3,H4....STRONG使用方法

    作为一个SEO从业人员,我们不仅仅是要懂得如何通过网站内容和外链等SEO手段,其实一个优秀的SEOER在从事一个SEO案例时,首先着手的是如何从网站程序本身来打好网站SEO基础. 在平时和很多朋友的交 ...

  2. qt windows下的配置 以及VS2010的使用

    qt在windows下的使用方式有两种: 1.将qt内置在vs下,例如,内置在vs2010下,使用vs的编译器及调试器. 2.在windows下,使用qt creator以及MingW作为编译器的使用 ...

  3. java操作Excel之POI(2)

    一.设置单元格对齐方式: /** * 设置单元格对齐方式 */ public static void main(String[] args) throws Exception { Workbook w ...

  4. VS2005常用快捷键

    Visual C++ 2005有很多种快捷键的映射方案,有适合 Emacs 用户的,有适合 Visual C++ 6.0 用户的,也有 Visual Studio 2005的,下面的快捷键符合IDE默 ...

  5. CA证书认证单向和双向的区别

     我觉得最科学的应该是,单向的,每次客户端发两把锁住的东西给服务端,服务端解密两次,服务端用客户端发来的对称密钥加密数据,发送给客户端,客户端只需解密一次,然后客户端每次修改随机密码,传给服务端,服务 ...

  6. linux进程与线程的区别【转】

    知乎上总结: "linux使用的1:1的线程模型,在内核中是不区分线程和进程的,都是可运行的任务而已.fork调用clone(最少的共享),pthread_create也是调用clone(最 ...

  7. IO基础知识

    传统的IO是阻塞的,BIO----基于流的模式,数据与Stream直接通信 NIO非阻塞的基于快的模式.数据与channel不直接交换数据,而是通过buffer进行数据交换. 基于文件的IO 基于网络 ...

  8. 网络文件系统与 Linux

    网络文件系统 是文件系统之上的一个网络抽象,来允许远程客户端以与本地文件系统类似的方式,来通过网络进行访问.虽然 NFS 不是第一个此类系统,但是它已经发展并演变成 UNIX® 系统中最强大最广泛使用 ...

  9. solr4.x之原子更新

    solr4.x发布以后,最值得人关注的一个功能,就是原子更新功能,传说的solr是否能真正的做到像数据库一样,支持单列更新呢? 在solr官方的介绍中,原子更新是filed级别的更新,不会涉及整个Do ...

  10. express基础

    express框架 const express = require("express"); 引入express框架 var app= express(); 实例化   相当于构造函 ...