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>