因为整合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>