Spring整合Hibernate详细步骤
一、概述
Spring整合Hibernate有什么好处?
1、由IOC容器来管理Hibernate的SessionFactory
2、让Hibernate使用上Spring的声明式事务
二、整合步骤
整合前准备:
持久化类:

@Entity
public class Book
{
private Integer id;
private String bookName;
private String isbn;
private int price;
private int stock;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getBookName()
{
return bookName;
}
public void setBookName(String bookName)
{
this.bookName = bookName;
}
public String getIsbn()
{
return isbn;
}
public void setIsbn(String isbn)
{
this.isbn = isbn;
}
public int getPrice()
{
return price;
}
public void setPrice(int price)
{
this.price = price;
}
public int getStock()
{
return stock;
}
public void setStock(int stock)
{
this.stock = stock;
}
public Book(Integer id, String bookName, String isbn, int price, int stock)
{
super();
this.id = id;
this.bookName = bookName;
this.isbn = isbn;
this.price = price;
this.stock = stock;
}
}

Dao层:
public interface BookDao
{
public String findBookById(int id); public void saveBook(Book book);
}
DaoImpl:

@Repository
public class BookDaoImpl implements BookDao
{
@Autowired
private SessionFactory sessionFactory; //获取和当前线程绑定的Seesion
private Session getSession()
{
return sessionFactory.getCurrentSession();
}
public String findBookById(int id)
{
String hql="SELECT bookName from Book where id=?";
Query query=getSession().createQuery(hql).setInteger(0, id);
String str= query.uniqueResult().toString();
return str;
}
public void saveBook(Book book)
{
getSession().save(book);
}
}

Service层:
public interface BookService
{
public String findBookById(int id);
public void saveBook(Book book);
}
ServiceImpl:

@Service
public class BookServiceImpl implements BookService
{
@Autowired
private BookDao bookDao;
public String findBookById(int id)
{
return bookDao.findBookById(id);
}
public void saveBook(Book book)
{
bookDao.saveBook(book); }
}

1、加入Hibernate
- 加入hibernate jar包
- 添加Hibernate的配置文件:hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置Hibernate的基本属性 -->
<!-- 1.数据源配置到IOC容器中 -->
<!-- 2.关联的.hbm.xml也在IOC容器配置SessionFactory实例 -->
<!-- 3.配置Hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

- 编写持久化类对应的.hbm.xml文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-3-15 16:30:05 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.demo.ssm.po.Book" table="BOOK">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="bookName" type="java.lang.String">
<column name="BOOK_NAME" />
</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>

2、加入Spring
- 加入spring jar包
- 加入Spring配置文件

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:component-scan base-package="com.demo.ssm"></context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root"></property>
<property name="password" value="281889"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.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/demo/ssm/po/*.hbm.xml"></property>
</bean> <!-- 配置Spring声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</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>
<aop:pointcut expression="execution(* com.demo.ssm.daoImpl.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>

3、编写测试类

public class BookDaoImplTest
{
private ApplicationContext context=null;
private BookService bookService=null; {
context= new ClassPathXmlApplicationContext("applicationContext.xml");
bookService=context.getBean(BookService.class);
}
@Test
public void test()
{
DataSource dataSource=(DataSource) context.getBean(DataSource.class);
System.out.println(dataSource);
}
@Test
public void test2()
{
String bookName=bookService.findBookById(1);
System.out.println(bookName);
} @Test
public void test3()
{
bookService.saveBook(new Book(2, "android源码分析", "1002", 45, 10));
}
}

Spring整合Hibernate详细步骤的更多相关文章
- Spring整合Hibernate的步骤
为什么要整合Hibernate?1.使用Spring的IOC功能管理SessionFactory对象 LocalSessionFactoryBean2.使用Spring管理Session对象 Hib ...
- Spring整合Hibernate图文步骤
首先建立java Project工程 点击Finish完成 添加Hibernate和Spring所需要的jar包还有Mysql连接的jar包 创建Dao层,Dao层实现,Model层,Service层 ...
- 【SSH】spring 整合 hibernate
spring-hibernate-1.2.9.jar applicationContext.xml <bean id="sessionFactory" class=" ...
- 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能
1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- spring 整合hibernate
1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory2). 让 Hibernate 使用上 Spring 的 ...
- Spring 整合 Hibernate
Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...
- Spring整合Hibernate(转)
概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使用上Spring的声明式事务 整合步骤 整合前准 ...
随机推荐
- linux压缩和解压命令总结
一.tar.gz tar -xzvf 二.tar.bz2 tar.bz2 解压命令 bzip2 -d gcc-4.1.0.tar.bz2---上面解压完之后执行下面的命令.执行成功后,会解压生成一个 ...
- C#6.0语法糖剖析(二)
1.索引初始化 使用代码 ] = ] = ] = "thirteen"}; 编译器生成的代码 Dictionary<int, string> dictionary2 = ...
- FME中Cass扩展属性转Shp的方法
问题:真受不了CAD中的注记,只能方便显示,难于数据交互.好在Cass把属性信息基本写在扩展属性中,但显示又成问题了.此事难两全!我们通过查看实体属性,需要把宗地界线的扩展属性提取出来.即组码为-3, ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q118-Q120)
Question 118You are creating a Business Connectivity Services (BCS) entity.You need to ensure that a ...
- 3.0之后在LinearLayout里增加分割线
android:divider="@drawable/shape"<!--分割线图片--> android:showDividers="middle|begi ...
- iOS触摸事件
触摸常见的事件有以下几种,触摸事件一般写在view文件中,因为viewController文件有可能控制不止一个view,不适合写触摸事件 // 开始触摸 - (void)touchesBegan:( ...
- Android常用设计模式(一)
java有23中设计模式,Android中也用到了很多的设计模式,本篇就来介绍Android中常用的几种设计模式 一.普通工厂设计模式 普通工厂设计模式,就是创建一个工厂类负责创建对象 ,用户根据需求 ...
- iOS开发笔记4:HTTP网络通信及网络编程
这一篇主要总结iOS开发中进行HTTP通信及数据上传下载用到的方法.网络编程中常用的有第三方类库AFNetworking或者iOS7开始新推出的NSURLSession,还有NSURLSession的 ...
- 关于IOS应用程序视图
积累英语词汇: assemble [ə'semb(ə)l] vt. 集合,聚集:装配:收集 assembled [ə'sembəld] adj. 组合的:安装的 v. 装配(assemble的过去分词 ...
- Android keystore 密码忘记了的找回办法
keystore密码忘记了,准备给自己的应用发布一个新版本,在apk打包时,发现之前的用的keystore密码忘了.如果换一个keystore,则之前已经安装应用的用户就必须手工卸载原应用才能安装,非 ...