zoukankan      html  css  js  c++  java
  • spring 整合 hibernate xml配置

    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>

  • 相关阅读:
    Mtk Ft6306 touch 驱动 .
    第一屏不显示懒加载的图片内容,这个方法可以搞定
    C#多线程中访问winform控件 (解决Winform 对象当前正在其他地方使用)
    变化的科技感十足的网站,推荐
    新年有感
    获取高精度时间注意事项 (QueryPerformanceCounter , QueryPerformanceFrequency)
    修改 TeamViewer ID 的方法
    VS2017离线安装包[百度云盘](收藏了)
    老子今天不加班,程序员也需要自由
    改变Eclipse 中代码字体大小
  • 原文地址:https://www.cnblogs.com/hwgok/p/5452230.html
Copyright © 2011-2022 走看看