hibernate 基于cglib的 事务代理

xiaoxiao2021-03-01  18

实例:使用hibernate 事务代理完成一个事务,添加一个人,这个人有两个地址。这是一个事务,要么全部插进去,要么谁也不插进去。 1,Person类 package com.dd.domain;import java.util.HashSet;import java.util.List;import java.util.Set;public class Person { private int id; private String name; private int age; private Set<Address> addresss = new HashSet<Address>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Set<Address> getAddresss() { return addresss; } public void setAddresss(Set<Address> addresss) { this.addresss = addresss; }} Person类的映射文件 <?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 default-lazy="true" default-access="property" package="com.dd.domain" > <class name="Person" dynamic-update="true" dynamic-insert="true" table="myperson"> <id name="id" column="id" type="integer"> <generator class="native"/> </id> <property name="name" type="string" column="name" length="20"/> <property name="age" type="integer" column="age" length="3"/> <set name="addresss" inverse="true" cascade="all" > <key column="personid"/> <one-to-many class="Address"/> </set> </class></hibernate-mapping> 2,Address类 package com.dd.domain;public class Address { private int id; private String name; private String info; private String remark; private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; }}Address类映射文件<?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 default-lazy="true" default-access="property" package="com.dd.domain" > <class name="Address" dynamic-update="true" dynamic-insert="true" table="address"> <id name="id" column="id" type="integer"> <generator class="native"/> </id> <property name="name" type="string" column="NAME" length="20"/> <property name="info" type="string" column="INFO" length="30"/> <property name="remark" type="string" column="REMARK" length="30"/> <many-to-one name="person" class="Person" column="personid" not-null="true"></many-to-one> </class></hibernate-mapping>4,Sprping配置文件 <!-- Person业务类的事务代理,业务中实际用的是这个代理类 --> <bean id="myPersonServiceProxy" parent="baseTransactionProxy"> <property name="target"> <ref bean="personService"/> </property> </bean> <!-- 真是的PersonService类,实际业务中没有用到这个类,而是这个类的代理类 --> <bean id="personService" class="com.dd.service.PersonServiceImpl"> <property name="personDao" ref="personDao2"></property> <property name="addressDao" ref="addressDaoImp"></property> </bean> <!-- Person的数据库访问类 --> <bean id="personDao2" class="com.dd.dao.PersonDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 添加Person的Action类,注意这个类用的是sping生成的,而不是struts生成的 --> <bean id="addMyPerson" class="com.dd.action.AddPersonAction"> <property name="personService" ref="myPersonServiceProxy"></property> </bean> <!-- 添加地址的数据库访问类 --> <bean id="addressDaoImp" class="com.dd.dao.AddressDaoImp"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> 4,Person的Action类 package com.dd.action;import com.dd.domain.Person;import com.dd.service.PersonServiceImpl;public class AddPersonAction { private PersonServiceImpl personService; private int number1; private int number2; private int sum; public int getNumber1() { return number1; } public void setNumber1(int number1) { this.number1 = number1; } public int getNumber2() { return number2; } public void setNumber2(int number2) { this.number2 = number2; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } public String add(){ Person person = new Person(); person.setName("陈超样"); person.setAge(27); getPersonService().addPerson(person); return "success"; } public PersonServiceImpl getPersonService() { return personService; } public void setPersonService(PersonServiceImpl personService) { this.personService = personService; }} 5,Person的业务Service类 package com.dd.service;import com.dd.dao.AddressDaoImp;import com.dd.dao.PersonDaoImpl;import com.dd.domain.Address;import com.dd.domain.Person;public class PersonServiceImpl { private PersonDaoImpl personDao; private AddressDaoImp addressDao; public void addPerson(Person person) { Address address = new Address(); address.setName("美国"); address.setInfo("非常富有"); getPersonDao().addPerson(person); getAddressDao().addAddress(address); } public PersonDaoImpl getPersonDao() { return personDao; } public void setPersonDao(PersonDaoImpl personDao) { this.personDao = personDao; } public AddressDaoImp getAddressDao() { return addressDao; } public void setAddressDao(AddressDaoImp addressDao) { this.addressDao = addressDao; }} 6,Person数据库访问类 package com.dd.dao;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.dd.domain.Person;public class PersonDaoImpl extends HibernateDaoSupport { public void addPerson(Person person){ getHibernateTemplate().save(person); }} 7,Address数据库访问类 package com.dd.dao;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.dd.domain.Address;public class AddressDaoImp extends HibernateDaoSupport { public void addAddress(Address address) { getHibernateTemplate().save(address); }} 8,添加人员的jsp页面 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <body> <s:form action="addMyPerson"> <s:textfield name="username" key="user"></s:textfield> <s:textfield name="password" key="pass"></s:textfield> <input type="button" value="注册"/> <s:submit value="添加"></s:submit> </s:form> </body></html> 相关资源:hibernate的中三种版本的cglib.jar
转载请注明原文地址: https://www.6miu.com/read-4050000.html

最新回复(0)