Spring(四、Spring整合hibernate)

4 篇文章 0 订阅
2 篇文章 0 订阅

spring与hibernate的整合目的就是为了让 IOC 容器来管理 Hibernate 的核心接口SessionFactory以及让 Hibernate 使用上 Spring 的声明式事务来进行事务操作.
spring整合hibernate 有两种方式 1、注解方式 2、xml方式实现
1、注解方式实现:
applicationContext.xml配置文件:

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
       <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"></property>
       <property name="username" value="iotek"></property>
       <property name="password" value="iotek"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>

   <property name="packagesToScan" value="com.iotek.bean"></property> 

        <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">create</prop>
            </props>
        </property>
    </bean>

    <bean id="person" class="com.iotek.daoImpl.PersonImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
 </beans>

2.xml方式实现 applicationContext.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: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.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">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
       <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"></property>
       <property name="username" value="iotek"></property>
       <property name="password" value="iotek"></property>
    </bean>
    <!-- session:::sessionFactory 
        相当于 hibernate.cfg.xml
        -->
  <!-- 配置SessionFactory -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mappingResources">
            <list>
                <value>com/iotek/bean/person.hbm.xml</value>
            </list>
        </property> 
        <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">create</prop>
            </props>
        </property>
    </bean>

    <bean id="person" class="com.iotek.daoImpl.PersonImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>    
</beans>

两种配置最大的区别就是注解方式不用在写O/R映射配置文件而xml方式实现的要配置O/R映射配置文件
采用注解的方式是:(扫描指定的包)

  <property name="packagesToScan" value="com.iotek.bean"></property> 

采用xml方式是:(配置**.hbm.xml文件)

<property name="mappingResources">
            <list>
                <value>com/iotek/bean/person.hbm.xml</value>
            </list>
        </property> 

实体类

package com.iotek.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(length=11)
private String name;
private int age;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

}
package com.iotek.daoImpl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import com.iotek.bean.Person;
import com.iotek.dao.PersonDao;

public class PersonImpl implements PersonDao {
    /*@Autowired*/
    private SessionFactory sessionFactory;


    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    @Override
    public void savePerson(Person p) {
        Session session = sessionFactory.openSession();
        if(session==null){
            System.out.println("null");
        }else{
            System.out.println("not null");
        }

        Transaction tran = null;
        try{
        System.out.println(p.getName());
            tran = session.beginTransaction();
            session.save(p);
            tran.commit();
            System.out.println("提交");

        }catch(Exception e){
            tran.rollback();
        }finally{
            session.close();
        }


    }

}

测试类:

package com.iotek.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.iotek.bean.Person;
import com.iotek.dao.PersonDao;
import com.iotek.daoImpl.PersonImpl;

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonImpl person=context.getBean("person", PersonImpl.class);
        Person p = new Person();
        p.setId(10);
        p.setName("xiaoming");
        p.setAge(22);
        person.savePerson(p);
        System.out.println("成功");

    }

}

通过.properties文件写入驱动,账号等信息(jdbc.properties)

jdbcUrl        = jdbc:oracle:thin:@127.0.0.1:1521:XE
driverClass    = oracle.jdbc.driver.OracleDriver
user        = iotek
password    =iotek

applicationContexte.xml文件的写法

<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.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">
          <!-- 导入外部的properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="url" value="${jdbcUrl}"></property>
         <property name="driverClassName" value="${driverClass}"></property>
         <property name="username" value="${user}"></property>
         <property name="password" value="${password}"></property>
    </bean>
     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
   <property name="packagesToScan" value="com.iotek.bean"></property> 

        <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">create</prop>
            </props>
        </property>
    </bean>

    <bean id="person" class="com.iotek.daoImpl.PersonImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值