因为整合spring和hibernate所以,需要用到spring里面复写Hibernate的类以有DI和IOC特性
db.sql
hibernate_basic数据库
表 person
字段
pid pname psex
Person.java
1 package cn.edu.spring_hibernate; 2 3 public class Person { 4 private Long pid; 5 private String pname; 6 private String psex; 7 public Long getPid() { 8 return pid; 9 } 10 public void setPid(Long pid) { 11 this.pid = pid; 12 } 13 public String getPname() { 14 return pname; 15 } 16 public void setPname(String pname) { 17 this.pname = pname; 18 } 19 public String getPsex() { 20 return psex; 21 } 22 public void setPsex(String psex) { 23 this.psex = psex; 24 } 25 26 }
Person.hbm.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <!-- 6 用来描述一个持久化类 7 name 类的全名 8 table 可以不写 默认值和类名一样 9 catalog 数据库的名称 一般不写 10 --> 11 <class name="cn.edu.spring_hibernate.Person"> 12 <!-- 13 标示属性 和数据库中的主键对应 14 name 属性的名称 15 column 列的名称 16 --> 17 <id name="pid" column="pid" length="200" type="java.lang.Long"> 18 <!-- 19 主键的产生器 20 就该告诉hibernate容器用什么样的方式产生主键 21 --> 22 <generator class="increment"></generator> 23 </id> 24 <!-- 25 描述一般属性 26 --> 27 <property name="pname" column="pname" length="20" type="string"> 28 </property> 29 30 <property name="psex" column="psex" length="10" type="java.lang.String"></property> 31 </class> 32 </hibernate-mapping>
PersonDao.java
1 package cn.edu.spring_hibernate; 2 public interface PersonDao { 3 public void savePerson(Person person); 4 }
PersonDaoImpl.java
1 package cn.edu.spring_hibernate; 2 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 3 //继承HibernateDaoSupport操作数据库,事务管理在配置文件中配,和这个类没关系 4 public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao { 5 @Override 6 public void savePerson(Person person) { 7 this.getHibernateTemplate().save(person); 8 } 9 }
PersonService.java
1 package cn.edu.spring_hibernate; 2 public interface PersonService { 3 public void savePerson(Person person); 4 }
PersonServiceImple.java
1 package cn.edu.spring_hibernate; 2 public class PersonServiceImpl implements PersonService { 3 //这里要写依赖注入,配置文件中可以配置 4 private PersonDao personDao=new PersonDaoImpl(); 5 public PersonDao getPersonDao() { 6 return personDao; 7 } 8 public void setPersonDao(PersonDao personDao) { 9 this.personDao = personDao; 10 } 11 @Override 12 public void savePerson(Person person) { 13 personDao.savePerson(person); 14 } 15 }
MyException.java(用于异常处理)
1 package cn.edu.spring_hibernate; 2 public class MyException { 3 public void defineException(Throwable ex){ 4 System.out.println(ex.getMessage()); 5 } 6 }
test.java
1 package cn.edu.spring_hibernate; 2 import org.junit.Test; 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 public class test { 6 @Test 7 public void testSpring_Hibernate() 8 { 9 ApplicationContext context=new ClassPathXmlApplicationContext("cn/edu/spring_hibernate/config/applicationContext-spring_hibernate.xml"); 10 //这里如果 getBean("personDao")会加入数据不成功,因为配置文件中没有对其开启事务处理 11 PersonService personService=(PersonService)context.getBean("personService"); 12 Person person=new Person(); 13 person.setPname("ssss"); 14 person.setPsex("god"); 15 personService.savePerson(person); 16 } 17 }
配置文件中
hibernate.cfg.xml (配置文件中第二种配置sessionFactory的时候用到)这种方法方便点
1 <?xml version=‘1.0‘ encoding=‘utf-8‘?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <!-- 7 一个session-factory只能连接一个数据库 8 --> 9 <session-factory> 10 <!-- 11 数据库的用户名 12 --> 13 <property name="connection.username">root</property> 14 <!-- 15 密码 16 --> 17 <property name="connection.password">friends</property> 18 <!-- 19 url 20 --> 21 <property name="connection.url"> 22 jdbc:mysql://localhost:3306/hibernate_basic 23 </property> 24 25 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 26 <!-- 27 作用:根据持久化类和映射文件生成表 28 validate 29 create-drop 30 create 31 update 32 --> 33 <property name="hbm2ddl.auto">update</property> 34 <!-- 35 显示hibernate内部生成的sql语句 36 --> 37 <property name="show_sql">true</property> 38 <mapping resource="cn/edu/spring_hibernate/Person.hbm.xml" /> 39 40 </session-factory> 41 </hibernate-configuration>
jdbc.properties (配置文件中第一种配置sessionFactory的时候用到)
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc\:mysql\://localhost\:3306/hibernate_basic 3 jdbc.username=root 4 jdbc.password=friends
applicationContext-spring_hibernate.xml
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 3 xmlns:tx="http://www.springframework.org/schema/tx" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 6 http://www.springframework.org/schema/aop 7 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 10 <!-- 配置引入配置文件路径 --> 11 <bean 12 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 13 <property name="locations"> 14 <value>classpath:cn/edu/spring_hibernate/config/jdbc.properties</value> 15 </property> 16 </bean> 17 18 <bean id="dataSource" destroy-method="close" 19 class="org.apache.commons.dbcp.BasicDataSource"> 20 <property name="driverClassName" value="${jdbc.driverClassName}" /> 21 <property name="url" value="${jdbc.url}" /> 22 <property name="username" value="${jdbc.username}" /> 23 <property name="password" value="${jdbc.password}" /> 24 </bean> 25 26 <!-- 27 sessionFactory 1、sessionFactoryImpl 2、利用spring的IOC和DI的特征 28 hibernate提供的 sessionFactory没有IOC和DI特征,所以spring重写了这个这个类加入了这两个特征 29 --> 30 <!-- 这是配置sessionFactory的第一种方式 --> 31 <!-- 32 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 33 <property name="dataSouce" ref="dataSource"></property> 34 <property name="mappingResources"> 35 导入配置文件 36 <list> 37 <value>cn/edu/spring_hibernate/Person.hbm.xml</value> 38 </list> 39 </property> 40 <property name="hibernateProperties"> 41 <value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value> 42 </property> 43 </bean> 44 --> 45 <!-- 配置sessionFactory的第二种方式 --> 46 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 47 <property name="configLocation"> 48 <value>classpath:cn/edu/spring_hibernate/config/hibernate.cfg.xml</value> 49 </property> 50 </bean> 51 <!-- 因为personDaoImpl继承了HibernateDaoSupport,所以必须要注入sessionFactory --> 52 <bean id="personDao" class="cn.edu.spring_hibernate.PersonDaoImpl"> 53 <property name="sessionFactory"> 54 <ref bean="sessionFactory"/> 55 </property> 56 </bean> 57 58 <bean id="personService" class="cn.edu.spring_hibernate.PersonServiceImpl"> 59 <property name="personDao"> 60 <ref bean="personDao"/> 61 </property> 62 </bean> 63 64 65 <bean id="myException" class="cn.itcast.spring.jdbc.transaction.MyException"></bean> 66 <!-- spring和hibernate整合提供的事务管理器管理类, --> 67 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 68 <property name="sessionFactory"> 69 <ref bean="sessionFactory"/> 70 </property> 71 </bean> 72 <!-- 73 通知 1、告诉spring容器,采用什么样的方法处理事务 2、告诉spring容器,目标方法应该采用什么样的事务处理策略 74 --> 75 <tx:advice id="tx" transaction-manager="transactionManager"> 76 <tx:attributes> 77 <!-- 78 save开头的函数名 name规定方法 isolation 默认值为DEFAULT propagation 传播机制 REQUIRED 79 --> 80 <tx:method name="save*" read-only="false" /> 81 </tx:attributes> 82 </tx:advice> 83 <!-- 本来事务由程序员自己写并且当切面放入,但是这里spring提供了事务处理的通知方法,所以不用程序员写切面了 --> 84 <aop:config > 85 <aop:pointcut expression="execution(* cn.edu.spring_hibernate.PersonServiceImpl.*(..))" id="perform"/> 86 <aop:advisor advice-ref="tx" pointcut-ref="perform" /> 87 <!-- 指定了切面和通知 --> 88 <aop:aspect ref="myException"> 89 <aop:after-throwing method="defineException" pointcut-ref="perform" throwing="ex"/> 90 </aop:aspect> 91 </aop:config> 92 </beans>
Spring+Hibernate整合
时间: 2024-10-31 07:34:07