zoukankan      html  css  js  c++  java
  • Spring 集成Hibernate的三种方式

    首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件

    第一种集成方式:首先定义一个MySessionFactory的类

    package com.tz.core;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.springframework.stereotype.Component;
    
    public class MysessionFactoryBean {
    
        public void setConfig(String config) {
            this.config = config;
        }
    
        public String getConfig() {
            return config;
        }
    
        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
        private String config;
        private SessionFactory sessionFactory;
    
        public void init() {
            Configuration configuration = new Configuration().configure(config);
            sessionFactory = configuration.buildSessionFactory();
        }
    }

    在Spring的配置文件中进行IOC注入

    <bean id="sessionFactory" class="com.tz.core.MysessionFactoryBean" init-method="init">
             <property name="config" value="hibernate.cfg.xml"></property>
        </bean>

    注意要加上init-method="init",否则不会初始化,然后获取Session

    package hibernate;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.junit.Test;
    
    public class TestHibernate {
      @Test
      public void handle(){
          Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
          SessionFactory sessionFactory = configuration.buildSessionFactory();
          Session session = sessionFactory.openSession();
          System.out.print(session);
      }
    }

    用JUTIL测试打印一个地址则成功了

    2第二种方式,

      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
             <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
       </bean>

    获取方式

    	 @Autowired
    	 private SessionFactory sessionFactory;
    

      直接注入即可,记得扫包

    第三种集成方式,在Spring的配置文件applicationContext中写入.这种方法的一大特色是可以使用SPRING的数据源,hibernate.cfg.xml不导入也可

     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>   
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </bean>   
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
           <property name="dataSource" ref="dataSource"></property>
        </bean> 
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
           <property name="dataSource" ref="dataSource"></property>
            <property name="mappingLocations">
               <list>
                   <value>com/tz/entity/Student.hbm.xml</value>
               </list>
            </property>
            <property name="hibernateProperties">
               <props>
                  <prop key ="hibernate.show_sql">true</prop>
                  <prop key="hibernate.format_sql">true</prop>
               </props>
            </property>         
        </bean>

    获取SessionFactory对象的方式与第二种相同

  • 相关阅读:
    024 其它 关联映射
    023 复合主键 关联映射
    022 component(组件)关联映射
    020 <one-to-one>、<many-to-one>单端关联上的lazy(懒加载)属性
    019 关联映射文件中集合标签中的lazy(懒加载)属性
    018 关联映射文件中<class>标签中的lazy(懒加载)属性
    017 多对多关联映射 双向(many-to-many)
    016 多对多关联映射 单向(many-to-many)
    015 一对多关联映射 双向(one-to-many)
    014 一对多关联映射 单向(one-to-many)
  • 原文地址:https://www.cnblogs.com/zengda/p/4328875.html
Copyright © 2011-2022 走看看