一、概述
Spring整合Hibernate有什么好处?
1、由IOC容器来管理Hibernate的SessionFactory
2、让Hibernate使用上Spring的声明式事务
二、整合步骤:
步骤一:导包
创建Maven项目SpringHibernate,并导入数据库驱动包、Hibernate开发包以及Spring开发包,完成后项目的pom.xml文件内容如下:
<!--
https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.6.Final</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.3.6.Final</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
步骤二:创建Hibernate配置文件
<session-factory>
<!-- 配置Hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
注意: <!-- 1.数据源配置到Spring的IOC容器中 -->
<!-- 2.关联的.hbm.xml也在IOC容器配置SessionFactory实例 -->
步骤三:创建实体类及其映射文件:
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<Book> 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<Book> findAll() {
String hql = "from Book";
Query<Book> 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<Book> 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<Book> 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>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" lazy-init="false">
<!-- 注入datasource,给sessionfactoryBean内setdatasource提供数据源 -->
<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"/>
<!-- 配置Spring声明式事务,采用AOP形式切入 -->
<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);
}
}