一、概述
Spring整合Hibernate有什么好处?
1、由IOC容器来管理Hibernate的SessionFactory
2、让Hibernate使用上Spring的声明式事务
二、整合步骤:
步骤一:导包
创建Maven项目SpringHibernate,并导入数据库驱动包、Hibernate开发包以及Spring开发包,完成后项目的pom.xml文件内容如下:
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>5.3.6.Finalversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-c3p0artifactId>
<version>5.3.6.Finalversion>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc6artifactId>
<version>11.2.0.4version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>5.1.1.RELEASEversion>
dependency>
步骤二:创建Hibernate配置文件
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hbm2ddl.auto">updateproperty>
session-factory>
注意:
步骤三:创建实体类及其映射文件:
package com.cn.pojo;
public class Book {
private Integer id;
private String bookName;
private String isbn;
private int price;
private int stock;
...省略set/get方法...
}
Book.hbm.xml文件
<hibernate-mapping>
<class name="com.cn.pojo.Book" table="TB_BOOK">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
id>
<property name="bookName" type="java.lang.String">
<column name="BOOKNAME" />
property>
<property name="isbn" type="java.lang.String">
<column name="ISBN" />
property>
<property name="price" type="int">
<column name="PRICE" />
property>
<property name="stock" type="int">
<column name="STOCK" />
property>
class>
hibernate-mapping>
步骤四:创建DAO层、Service层
1、DAO层接口:
package com.cn.dao;
import java.util.List;
import com.cn.pojo.Book;
public interface IBookDao {
public List findAll();
public String findBookById(int id);
public void save(Book book);
public void update(Book book);
public void delete(int id);
}
2、DAO层接口实现类:
package com.cn.dao;
public class BookDaoImpl implements IBookDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
//获取和当前线程绑定的Seesion
private Session getSession()
{
return sessionFactory.getCurrentSession();
}
@Override
public List findAll() {
String hql = "from Book";
Query query = this.getSession().createQuery(hql);
return query.list();
}
@Override
public String findBookById(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public void save(Book book) {
this.getSession().save(book);
}
@Override
public void update(Book book) {
// TODO Auto-generated method stub
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
}
}
3、服务层接口:
public interface IBookService {
public String findBookById(int id);
public void saveBook(Book book);
public List findAll();
}
4、服务层实现类:
public class BookServiceImpl implements IBookService {
private IBookDao dao;
public IBookDao getDao() {
return dao;
}
public void setDao(IBookDao dao) {
this.dao = dao;
}
@Override
public String findBookById(int id) {
return null;
}
@Override
public void saveBook(Book book) {
dao.save(book);
}
@Override
public List findAll() {
return dao.findAll();
}
}
步骤五:创建Spring配置文件
1、applicationContext.xml文件:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="user" value="scott"/>
<property name="password" value="root"/>
<property name="initialPoolSize" value="3"/>
<property name="maxPoolSize" value="10"/>
<property name="minPoolSize" value="1"/>
<property name="acquireIncrement" value="3"/>
<property name="maxIdleTime" value="60"/>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" lazy-init="false">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml">property>
<property name="mappingLocations" value="classpath:com/cn/pojo/*.hbm.xml"/>
bean>
<import resource="classpath:applicationContext-beans.xml"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
tx:attributes>
tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* com.cn.dao.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
aop:config>
2、applicationContext-beans.xml
<bean id="bookDao" class="com.cn.dao.BookDaoImpl">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<bean id="bookService" class="com.cn.dao.BookServiceImpl">
<property name="dao" ref="bookDao">property>
bean>
测试:
public class Test {
private static String conf = "applicationContext.xml";
private static ApplicationContext context = null;
private static IBookService bookService = null;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext(conf);
bookService = context.getBean(BookServiceImpl.class);
Book book = new Book();
book.setId(2);
book.setBookName("Java程序设计");
book.setPrice(48);
book.setStock(78);
book.setIsbn("ISBN-001-0980");
bookService.saveBook(book);
}
}