Struts2+Hibernate entity+dao+biz+tool

Goods 实体层及注解

package entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
 * 商品表
 * */
@Entity
@Table(name="t_goods")
public class Goods {
	@Id
	@GeneratedValue(generator="seq_goods",strategy=GenerationType.SEQUENCE)
	@SequenceGenerator(name="seq_goods",sequenceName="seq_goods",allocationSize=1,initialValue=1)
	private Integer id;//商品编号
	@Column
	private Double price;//商品价格
	@Column
	private String name;//商品名称
	@Column
	private String specification;//商品介绍
	@Column
	private String manufacture;//制造商
	public Goods() {
	}
	public Goods(Double price, String name, String specification,String manufacture) {
		this.price = price;
		this.name = name;
		this.specification = specification;
		this.manufacture = manufacture;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSpecification() {
		return specification;
	}
	public void setSpecification(String specification) {
		this.specification = specification;
	}
	public String getManufacture() {
		return manufacture;
	}
	public void setManufacture(String manufacture) {
		this.manufacture = manufacture;
	}
}

 

Order 实体层及注解

package entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
/**
 * 订单表
 * */
@Entity
@Table(name="t_order")
public class Order {
	@Id
	@GeneratedValue(generator="seq_order",strategy=GenerationType.SEQUENCE)
	@SequenceGenerator(name="seq_order",sequenceName="seq_order",allocationSize=1,initialValue=1)
	private Integer id;//订单号
	@Column
	private String name;//收货人
	@Column
	private String  address;//收货地址
	@Column
	private Date createTime;//订单创建时间
	@OneToMany
	@Cascade(CascadeType.SAVE_UPDATE)
	@JoinColumn(name="order_id")
	private Set<OrderDetail> details=new HashSet<OrderDetail>();
	public Order() {
	}
	public Order(String name, String address, Date createTime) {
		this.name = name;
		this.address = address;
		this.createTime = createTime;

	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Set<OrderDetail> getDetails() {
		return details;
	}
	public void setDetails(Set<OrderDetail> details) {
		this.details = details;
	}
}

 

OrderDetail 实体层及注解

package entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
 * 订单明细表
 * */
@Entity
@Table
public class OrderDetail {
	@Id
	@GeneratedValue(generator="seq_orderDetail",strategy=GenerationType.SEQUENCE)
	@SequenceGenerator(name="seq_orderDetail",sequenceName="seq_orderDetail",allocationSize=1,initialValue=1)
	private Integer id;//订单明细编号
	@Column(nullable=false)
	private Integer amount;//商品数量
	@ManyToOne(targetEntity=Goods.class)
	@JoinColumn(name="goods_id")
	private Goods goods;
	@ManyToOne(targetEntity=Order.class)
	@JoinColumn(name="order_id")
	private Order order;
	public OrderDetail() {
	}
	public OrderDetail(Integer amount, Goods goods, Order order) {
		super();
		this.amount = amount;
		this.goods = goods;
		this.order = order;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getAmount() {
		return amount;
	}
	public void setAmount(Integer amount) {
		this.amount = amount;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
}

 

GoodsDao 数据访问层接口与实现

package dao;

import java.util.List;
import org.hibernate.HibernateException;
import entity.Goods;
/**
 * 商品数据访问层接口
 * */
public interface GoodsDao {
	//获取所有商品的信息
	public List<Goods> getAll() throws HibernateException;
	//根据多个id获取商品信息
	public List<Goods> getGoodsByIds(Integer[] ids) throws HibernateException;
	//保存订单
	public void saveOrder(List<Goods> goodsList,Integer[] amounts,String name,String address); 
}
package dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import tool.Tool;
import util.HibernateSessionFactory;
import entity.Goods;
import entity.Order;
import entity.OrderDetail;
import dao.GoodsDao;
/**
 * 商品数据访问层实现
 * */
public class GoodsDaoImpl implements GoodsDao {
	//获取所有商品的信息
	@SuppressWarnings("unchecked")
	public List<Goods> getAll() throws HibernateException {
		String hql="from Goods";
		List<Goods> goods=null;
		Session session=HibernateSessionFactory.getSession();
		Query query=session.createQuery(hql);
		goods=query.list();
		HibernateSessionFactory.closeSession();
		return goods;
	}
	//根据多个id获取商品信息
	public List<Goods> getGoodsByIds(Integer[] ids) throws HibernateException {
		String hql="from Goods where id=:id";
		List<Goods> goods=new ArrayList<Goods>();
		Session session=HibernateSessionFactory.getSession();

		for (int i=0; i<ids.length; i++) {
			Query query=session.createQuery(hql);
			query.setInteger("id", ids[i]);
			goods.add((Goods)query.uniqueResult());
		}
		HibernateSessionFactory.closeSession();
		return goods;
	}
	//保存订单
	public void saveOrder(List<Goods> goodsList,Integer[] amounts,String name,String address){
		Session session=HibernateSessionFactory.getSession();
		Order order=new Order(name,address,new Date());
		Transaction tx=null;
		try {
			tx=session.beginTransaction();
			Tool.oId = (Integer) session.save(order);
			for(int i=0; i<amounts.length; i++){
				OrderDetail detail = new OrderDetail(amounts[i], goodsList.get(i), order);
				Integer odId =(Integer) session.save(detail);
				Tool.odIds.add(odId);
			}
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
	}
}

 

OrderDao 数据访问层接口与实现

package dao;

import org.hibernate.HibernateException;
import entity.Order;
/**
 * 订单数据访问层接口
 * */
public interface OrderDao {
	//根据id获取订单信息
	public Order getOrderByIds(Integer oId) throws HibernateException;
}
package dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateSessionFactory;
import dao.OrderDao;
import entity.Order;
/**
 * 订单数据访问层实现
 * */
public class OrderDaoImpl implements OrderDao {

	public Order getOrderByIds(Integer oId) throws HibernateException {
		String hql="from Order where id =:oId";
		Order order=null;
		Session session=HibernateSessionFactory.getSession();
		Query query=session.createQuery(hql);
		query.setInteger("oId", oId);
		order = (Order) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return order;
	}

}

 

OrderDetailDao 数据访问层接口与实现
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import entity.OrderDetail;
/**
 * 订单明细数据访问层接口
 * */
public interface OrderDetailDao {
	//根据多个id获取OrderDetail信息
	public List<OrderDetail> getOrderDetailByOdIds(List<Integer> odIds) throws HibernateException;
}
package dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateSessionFactory;
import dao.OrderDetailDao;
import entity.OrderDetail;
/**
 * 订单明细数据访问层实现
 * */
public class OrderDetailDaoImpl implements OrderDetailDao {

	public List<OrderDetail> getOrderDetailByOdIds(List<Integer> odIds)
			throws HibernateException {
		String hql="from OrderDetail where id=:odId";
		List<OrderDetail> orderDetails=new ArrayList<OrderDetail>();
		Session session=HibernateSessionFactory.getSession();
		for (int i=0; i<odIds.size(); i++) {
			Query query=session.createQuery(hql);
			query.setInteger("odId", odIds.get(i));
			orderDetails.add((OrderDetail)query.uniqueResult());
		}
		HibernateSessionFactory.closeSession();
		return orderDetails;
	}
}
 
GoodsBiz 业务逻辑层接口与实现
package biz;

import java.util.List;
import org.hibernate.HibernateException;
import entity.Goods;
import entity.Order;
import entity.OrderDetail;
/**
 * 商品业务逻辑层接口
 * */
public interface GoodsBiz {
	//获取所有商品的信息
	public List<Goods> getAll() throws HibernateException;
	//根据多个id获取商品信息
	public List<Goods> getGoodsByIds(Integer[] ids) throws HibernateException;
	//保存订单
	public void saveOrder(Integer[] ids,Integer[] amounts,String name,String address);
	//根据id获取订单信息
	public Order getOrderByIds(Integer oId) throws HibernateException;
	//根据多个id获取OrderDetail信息
	public List<OrderDetail> getOrderDetailByOdIds(List<Integer> odIds) throws HibernateException;
}
package biz.impl;

import java.util.List;
import org.hibernate.HibernateException;
import entity.Goods;
import entity.Order;
import entity.OrderDetail;
import biz.GoodsBiz;
import dao.GoodsDao;
import dao.OrderDao;
import dao.OrderDetailDao;
import dao.impl.GoodsDaoImpl;
import dao.impl.OrderDaoImpl;
import dao.impl.OrderDetailDaoImpl;
/**
 * 商品业务逻辑层实现
 * */
public class GoodsBizImpl implements GoodsBiz
{
	private GoodsDao goodsDao=new GoodsDaoImpl();
	private OrderDao orderDao =  new OrderDaoImpl();
	private OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
	//获取所有商品的信息
	public List<Goods> getAll() throws HibernateException {
		return goodsDao.getAll();
	}
	//根据多个id获取商品信息
	public List<Goods> getGoodsByIds(Integer[] ids) throws HibernateException {
		return goodsDao.getGoodsByIds(ids);
	}
	//保存订单
	public void saveOrder(Integer[] ids,Integer[] amounts,String name,String address){
		List<Goods> goodsList = goodsDao.getGoodsByIds(ids);
		goodsDao.saveOrder(goodsList, amounts, name, address);
	}
	//根据id获取订单信息
	public Order getOrderByIds(Integer oId) throws HibernateException{
		return orderDao.getOrderByIds(oId);
	}
	//根据多个id获取OrderDetail信息
	public List<OrderDetail> getOrderDetailByOdIds(List<Integer> odIds) throws HibernateException{
		return orderDetailDao.getOrderDetailByOdIds(odIds);
	}
}
 
Tool tool类
package tool;

import java.util.ArrayList;
import java.util.List;
/**
 * 工具类
 * */
public class Tool {
	public static Integer oId ;//用来接收订单表的id
	public static List<Integer> odIds = new ArrayList<Integer>();//用来接收订单明细表的ids
}
 

你可能感兴趣的:(Hibernate)