B2C电子商务网站:三大框架整合开发的级联删除

B2C电子商务网站:三大框架整合开发的级联删除

       最近在做B2C电子商务网站,所以一直很忙,都没有什么时间写我的技术ITEYE博客,今天已经把进度提上去了,后台做的差不多了。今天我想给大家分享我的项目中的级联删除的操作。
       比如:对于商品表,当删除某个商品订单的时候,应该删除该订单下所有的订单详情。
       最后运行的结果是:当你点击订单的删除时,它会把所有的订单明细全部删除。这样就达到了级联删除的操作。
       以下是详细代码,大家可以参考。

 

cn.z_xiaofei168.domain包

package cn.z_xiaofei168.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class Orders implements java.io.Serializable {

	/**
	 * @author z_xiaofei168
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private Payments payments;
	private Employees employees;
	private Customers customers;
	private String number;
	private Date rdate;
	private Double sum;
	private String sendmode;
	private String status;
	private Set<Orderdetails> orderdetailses = new HashSet<Orderdetails>(0);
	
	//以下省略所有的set和get方法
	
}

 

 
   Orders.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 default-lazy="false"
	package="cn.z_xiaofei168.domain">
	<class name="Orders" table="orders">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="identity" />
		</id>
		<many-to-one class="Payments" fetch="select" lazy="false"
			name="payments">
			<column name="payment_id" not-null="true" />
		</many-to-one>
		<many-to-one class="Employees" fetch="select" lazy="false"
			name="employees">
			<column name="employees_id" not-null="true" />
		</many-to-one>
		<many-to-one class="Customers" fetch="select" lazy="false"
			name="customers">
			<column name="customers_id" not-null="true" />
		</many-to-one>
		<property generated="never" lazy="false" name="number"
			type="java.lang.String">
			<column length="20" name="number" not-null="true" />
		</property>
		<property generated="never" lazy="false" name="rdate" type="date">
			<column length="19" name="rdate" not-null="true" />
		</property>
		<property generated="never" lazy="false" name="sum"
			type="java.lang.Double">
			<column name="sum" not-null="true" precision="22" scale="0" />
		</property>
		<property generated="never" lazy="false" name="sendmode"
			type="java.lang.String">
			<column length="50" name="sendmode" not-null="true" />
		</property>
		<property generated="never" lazy="false" name="status"
			type="java.lang.String">
			<column length="10" name="status" not-null="true" />
		</property>
		<set inverse="true" lazy="false" name="orderdetailses" sort="unsorted">
			<key>
				<column name="orders_id" not-null="true" />
			</key>
			<one-to-many class="Orderdetails" />
		</set>
	</class>
</hibernate-mapping>

 

  cn.z_xiaofei168.action包

 

package cn.z_xiaofei168.action;

import java.util.List;
import cn.z_xiaofei168.domain.Orders;
import cn.z_xiaofei168.service.OrdersServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

public class OrdersAction extends ActionSupport {

	/**
	 * @author z_xiaofei168
	 */
	private static final long serialVersionUID = 1L;

	/** 业务层对象 */
	private OrdersServiceImpl ordersServiceImpl;
	private Orders entity;
	private List<Orders> entities;
	
	//以下省略所以的set和get方法
	
	/** 级联删除的操作 */
	public String delete() throws Exception {
		ordersServiceImpl.delete(entity.getId());
		return "delete";
	}
}
 

 

   cn.z_xiaofei168.dao包

 

package cn.z_xiaofei168.dao;

import java.util.List;
import java.util.Set;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.z_xiaofei168.domain.Orderdetails;
import cn.z_xiaofei168.domain.Orders;

public class OrdersDaoImpl extends HibernateDaoSupport implements OrdersDao {

	public void delete(Orders entity) throws Exception {
		
		/** 删除订单之前,应该把订单对应的订单明细删除 */
		Set<Orderdetails> orderdetails = entity.getOrderdetailses();
		if(orderdetails.size()==0){
			System.out.println("orderdetails对象为空");
		}else{
			for(Orderdetails ord: orderdetails){
				this.getHibernateTemplate().delete(ord);
			}
		}
		this.getHibernateTemplate().delete(entity);
	}

	public void delete(Integer id) throws Exception {
		this.delete(this.findById(id));
	}
	
	public Orders findById(Integer id) throws Exception {
		return (Orders) this.getHibernateTemplate().get(Orders.class, id);
	}
}

 

  cn.z_xiaofei168.service包

 

 

package cn.z_xiaofei168.service;

import java.util.List;

import cn.z_xiaofei168.dao.OrdersDaoImpl;
import cn.z_xiaofei168.domain.Orders;

public class OrdersServiceImpl implements OrdersService {

	private OrdersDaoImpl ordersDaoImpl;

	//set方法注入
	public void setOrdersDaoImpl(OrdersDaoImpl ordersDaoImpl) {
		this.ordersDaoImpl = ordersDaoImpl;
	}
	
	public void delete(Integer id) throws Exception {
		ordersDaoImpl.delete(id);
	}
}

 

       具体的JSP页面就不在这贴了,相信大家都会,都明白。

       大家有什么可以给我留言。

 

你可能感兴趣的:(java,DAO,Hibernate,框架,电子商务)