知识点: 1、在框架集成中spring这个框架充当的角色是黏合的作用,所以在做集成的时候我们需要将hibennate的配置文件交给spring启动。
2、目标
3、通知
4、代理
5、sessionFactory的注入
首先看一下目录结构:
步骤一:反转出相对应的实体,啰嗦一句,实体必须要有主键(代码如下):
package cn.zhuojingxinxi.entity; /** * Person entity. * * @author MyEclipse Persistence Tools */ public class Person implements java.io.Serializable { // Fields private Long pid; private String pname; private String pass; // Constructors /** default constructor */ public Person() { } /** full constructor */ public Person(String pname, String pass) { this.pname = pname; this.pass = pass; } // Property accessors public Long getPid() { return this.pid; } public void setPid(Long pid) { this.pid = pid; } public String getPname() { return this.pname; } public void setPname(String pname) { this.pname = pname; } public String getPass() { return this.pass; } public void setPass(String pass) { this.pass = pass; } }
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="cn.zhuojingxinxi.entity.Person" table="PERSON" schema="SCOTT"> <id name="pid" type="java.lang.Long"> <column name="PID" precision="22" scale="0" /> <generator class="increment" /> </id> <property name="pname" type="java.lang.String"> <column name="PNAME" length="50" /> </property> <property name="pass" type="java.lang.String"> <column name="PASS" length="50" /> </property> </class> </hibernate-mapping>
步骤二、编写Dao层,请一定要编写接口(代码如下):
PersonDao接口:
package cn.zhuojingxinxi.dao; import cn.zhuojingxinxi.entity.Person; public interface PersonDao { public void savePerson(Person person); }
PersonDaoImp实现类(为了能够拿到session必须继承HibernateDaoSupport):
package cn.zhuojingxinxi.dao.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.zhuojingxinxi.dao.PersonDao; import cn.zhuojingxinxi.entity.Person; public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao { public void savePerson(Person person) { this.getHibernateTemplate().save(person); } }
步骤三、编写biz层(即业务处理层)(同样必须写接口):
PersonService接口:
package cn.zhuojingxinxi.biz; import cn.zhuojingxinxi.entity.Person; public interface PersonService { public void savePerson(Person person); }
PersonServiceImp实现类(实现类中操作dao层中的方法,所以必须将PersonDao注入进来):
package cn.zhuojingxinxi.biz.impl; import cn.zhuojingxinxi.biz.PersonService; import cn.zhuojingxinxi.dao.PersonDao; import cn.zhuojingxinxi.entity.Person; public class PersonServiceImpl implements PersonService{ private PersonDao personDao=null; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void savePerson(Person person) { personDao.savePerson(person); } }
步骤四、编写测试类代码(假如还要和struts2集成,不要开tomcat测试,因为加载速度慢,所以编写测试类是有必要的)
package cn.zhuojingxinxi.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.zhuojingxinxi.biz.PersonService; import cn.zhuojingxinxi.dao.PersonDao; import cn.zhuojingxinxi.entity.Person; public class Test { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); // SessionFactory sessionFactory= (SessionFactory)context.getBean("sessionFactory"); // Session session=sessionFactory.openSession(); // // System.out.println(session); PersonDao dao=(PersonDao)context.getBean("personDao"); PersonService personService=(PersonService)context.getBean("personService"); Person p=new Person(); p.setPname("xiaohua"); p.setPass("123321"); personService.savePerson(p); } }
步骤五、applicationContext.xml的编写(非常重要)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop /spring-aop-2.0.xsd" > <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="personDao" class="cn.zhuojingxinxi.dao.impl.PersonDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 目标 --> <bean id="personService" class="cn.zhuojingxinxi.biz.impl.PersonServiceImpl"> <property name="personDao" ref="personDao"></property> </bean> <!-- 定义一个事务管理器(通知) --> <bean id="transactionManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 代理 --> <bean id="personServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="target" ref="personService"></property> <property name="transactionManager" ref="transactionManage"></property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>
需要注意的是:必须配置开头的tx、aop,否则事务是不会提交的。
源码下载请点这里: