Spring+Hibernate整合
因为整合spring和hibernate所以,需要用到spring里面复写Hibernate的类以有DI和IOC特性

db.sql
hibernate_basic数据库
表 person
字段
pid pname psex
Person.java
package cn.edu.spring_hibernate;
public class Person {
private Long pid;
private String pname;
private String psex;
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPsex() {
return psex;
}
public void setPsex(String psex) {
this.psex = psex;
}
}
Person.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
用来描述一个持久化类
name 类的全名
table 可以不写 默认值和类名一样
catalog 数据库的名称 一般不写
-->
<class name="cn.edu.spring_hibernate.Person">
<!--
标示属性 和数据库中的主键对应
name 属性的名称
column 列的名称
-->
<id name="pid" column="pid" length="200" type="java.lang.Long">
<!--
主键的产生器
就该告诉hibernate容器用什么样的方式产生主键
-->
<generator class="increment"></generator>
</id>
<!--
描述一般属性
-->
<property name="pname" column="pname" length="20" type="string">
</property> <property name="psex" column="psex" length="10" type="java.lang.String"></property>
</class>
</hibernate-mapping>
PersonDao.java
package cn.edu.spring_hibernate;
public interface PersonDao {
public void savePerson(Person person);
}
PersonDaoImpl.java
package cn.edu.spring_hibernate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
//继承HibernateDaoSupport操作数据库,事务管理在配置文件中配,和这个类没关系
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
@Override
public void savePerson(Person person) {
this.getHibernateTemplate().save(person);
}
}
PersonService.java
package cn.edu.spring_hibernate;
public interface PersonService {
public void savePerson(Person person);
}
PersonServiceImple.java
package cn.edu.spring_hibernate;
public class PersonServiceImpl implements PersonService {
//这里要写依赖注入,配置文件中可以配置
private PersonDao personDao=new PersonDaoImpl();
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void savePerson(Person person) {
personDao.savePerson(person);
}
}
MyException.java(用于异常处理)
package cn.edu.spring_hibernate;
public class MyException {
public void defineException(Throwable ex){
System.out.println(ex.getMessage());
}
}
test.java
package cn.edu.spring_hibernate;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
@Test
public void testSpring_Hibernate()
{
ApplicationContext context=new ClassPathXmlApplicationContext("cn/edu/spring_hibernate/config/applicationContext-spring_hibernate.xml");
//这里如果 getBean("personDao")会加入数据不成功,因为配置文件中没有对其开启事务处理
PersonService personService=(PersonService)context.getBean("personService");
Person person=new Person();
person.setPname("ssss");
person.setPsex("god");
personService.savePerson(person);
}
}
配置文件中
hibernate.cfg.xml (配置文件中第二种配置sessionFactory的时候用到)这种方法方便点
<?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只能连接一个数据库
-->
<session-factory>
<!--
数据库的用户名
-->
<property name="connection.username">root</property>
<!--
密码
-->
<property name="connection.password">friends</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate_basic
</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--
作用:根据持久化类和映射文件生成表
validate
create-drop
create
update
-->
<property name="hbm2ddl.auto">update</property>
<!--
显示hibernate内部生成的sql语句
-->
<property name="show_sql">true</property>
<mapping resource="cn/edu/spring_hibernate/Person.hbm.xml" /> </session-factory>
</hibernate-configuration>
jdbc.properties (配置文件中第一种配置sessionFactory的时候用到)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/hibernate_basic
jdbc.username=root
jdbc.password=friends
applicationContext-spring_hibernate.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置引入配置文件路径 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:cn/edu/spring_hibernate/config/jdbc.properties</value>
</property>
</bean> <bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <!--
sessionFactory 1、sessionFactoryImpl 2、利用spring的IOC和DI的特征
hibernate提供的 sessionFactory没有IOC和DI特征,所以spring重写了这个这个类加入了这两个特征
-->
<!-- 这是配置sessionFactory的第一种方式 -->
<!--
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSouce" ref="dataSource"></property>
<property name="mappingResources">
导入配置文件
<list>
<value>cn/edu/spring_hibernate/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value>
</property>
</bean>
-->
<!-- 配置sessionFactory的第二种方式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:cn/edu/spring_hibernate/config/hibernate.cfg.xml</value>
</property>
</bean>
<!-- 因为personDaoImpl继承了HibernateDaoSupport,所以必须要注入sessionFactory -->
<bean id="personDao" class="cn.edu.spring_hibernate.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean> <bean id="personService" class="cn.edu.spring_hibernate.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean> <bean id="myException" class="cn.itcast.spring.jdbc.transaction.MyException"></bean>
<!-- spring和hibernate整合提供的事务管理器管理类, -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!--
通知 1、告诉spring容器,采用什么样的方法处理事务 2、告诉spring容器,目标方法应该采用什么样的事务处理策略
-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!--
save开头的函数名 name规定方法 isolation 默认值为DEFAULT propagation 传播机制 REQUIRED
-->
<tx:method name="save*" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 本来事务由程序员自己写并且当切面放入,但是这里spring提供了事务处理的通知方法,所以不用程序员写切面了 -->
<aop:config >
<aop:pointcut expression="execution(* cn.edu.spring_hibernate.PersonServiceImpl.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform" />
<!-- 指定了切面和通知 -->
<aop:aspect ref="myException">
<aop:after-throwing method="defineException" pointcut-ref="perform" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>
Spring+Hibernate整合的更多相关文章
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- SpringMVC+Spring+Hibernate整合开发
最近突然想认真研究下java web常用框架,虽然现在一直在用,但实现的整体流程不是很了解,就在网上搜索资料,尝试自己搭建,以下是自己的搭建及测试过程. 一.准备工作: 1/安装并配置java运行环境 ...
- Struts2+Spring+Hibernate整合开发(Maven多模块搭建)
Struts2+Spring+Hibernate整合开发(Maven多模块搭建) 0.项目结构 Struts2:web层 Spring:对象的容器 Hibernate:数据库持久化操作 1.父模块导入 ...
- 笔记58 Spring+Hibernate整合(一)
Spring+Hibernate整合 一.整合思路 使DAO继承HibernateTemplate这个类 HibernateTemplate这个类提供了setSessionFactory()方法用于注 ...
- MyEclipse下Spring+Hibernate整合
目前,SSH(Struts+Spring+Hibernate)是Web开发的一种常用框架组合,Struts实现了MVC,Hibernate实现了关系对象映射,Spring实现了基于Bean的配置管理. ...
- Spring+Hibernate整合配置 --- 比较完整的spring、hibernate 配置
Spring+Hibernate整合配置 分类: J2EE2010-11-25 17:21 16667人阅读 评论(1) 收藏 举报 springhibernateclassactionservlet ...
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- Spring、Struts2+Spring+Hibernate整合步骤
所使用的Jar包: Hibernate: Spring(使用MyEclipse自动导入框架功能) Struts2: 注解包和MySql驱动包: 1.配置Hibernate和Spring: <be ...
- Struts+Spring+Hibernate整合
这段笔记三两年前写的,一直因为一些琐事,或者搞忘记了,没有发.今天偶然翻出了它,就和大家一起分享下吧. 1.导入jar包 Struts的jar包: Spring的jar包: Hibernate的jar ...
随机推荐
- 【枚举+贪心】POJ2718-Smallest Difference
[题目大意] 按升序输出几个不同的数字,任意组成两个数字,输出最小的差值. [思路] 虽然是在穷竭搜索的章节里找到的题目,但是我觉得不需要穷竭搜索,枚举一下就可以了,0MS.分为一下三种情况: (1) ...
- 1.1(JavaScript学习笔记)、JavaScript基础
一.JavaScript简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型. 它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端 ...
- sqlserver 计算数据库时间差
介绍:datediff(datepart,startdate,enddate) 返回间隔datepart 的数 SELECT datediff(yy,'2010-06-1 10:10',GETDATE ...
- Server-side Query interception with MS SQL Server
up vote15down votefavorite 5 I'm researching into intercepting queries that arrive at the SQL Serv ...
- emailautocomplete
CSS代码: .emailist{border:1px solid #bdbdbd; border-radius: 4px; background-color:#fff; color:#666; fo ...
- datetimepicker只显示日期,不显示时分秒
HTML代码<div class="input-group date form_datetime form-date" data-link-field="dtp_i ...
- ARM地址重映射机制
转:http://blog.csdn.net/yuanzhangmei1/article/details/8395028 ARM体系结构中,系统上电或复位后,处理器将从地址0x0处取第一条指令,因此, ...
- Objective-C字面量语法总结
通常情况下,创建数组,字典的时候需要写一些很长的方法名,今天就总结一下如何使用字面量语法代替这些方法. 1.数值的创建 NSNumber *number1 = [NSNumber numberWith ...
- jquery避免跟其他库冲突
方法一: var $j=JQuery.noConflict(); $j('#msg').hide();//此处$j就代表JQuery 方法二: JQuery.noConflict(); JQuery( ...
- Struct2_定义拦截器并使用注解方式作用在Action的方法中
一.目的:通过在方法上加注解控制哪些方法需要登陆后才能访问 二.方式:利用拦截器判断用户是否登陆 三.实现步骤 定义配置文件struts.xml添加节点 1 2 3 4 5 6 7 8 9 1 ...