根据需求,数据库按照如下设计:
根据业务,表之间的关系如上图所示(hibernate可自动建表)。
本系统拟采用贫血模式设计,model层只有字段的内容,不包含数据库的操作方法,业务方法等。dao层只有sql文不包含业务,仅仅是根据数据表之间的关系,以可以想到的任何方式把对应model层的数据库中的内容取出,service层可以包含多个dao层,根据业务调用不同的dao层取出相应的内容,所谓业务,即任何与该Model有关的业务,不管是传入该Model作为参数还是得到该Model的实例或者List只要是跟该Model有关都是该Model的业务。不知道这样理解对不对,反正我就这么做啦,还是望大神们不吝赐教啊。
说这么多也没用,最重要的是上代码~~~
先看pojo和hibernate文件,找两个关系最简单地表,其他的依次类推,就比如goods表和goodsprice表
package com.medclub.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class GoodsPojo implements Serializable{ @Override public String toString() { return "GoodsPojo [id=" + id + ", name=" + name + ", goodsPrices=" + goodsPrices + "]"; } private Integer id; private String name; //private Set<GoodsCommentPojo> goodsComments=new HashSet<GoodsCommentPojo>(); private Set<GoodsPricePojo> goodsPrices=new HashSet<GoodsPricePojo>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } /* public Set<GoodsCommentPojo> getGoodsComments() { return goodsComments; } public void setGoodsComments(Set<GoodsCommentPojo> goodsComments) { this.goodsComments = goodsComments; }*/ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; GoodsPojo other = (GoodsPojo) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public Set<GoodsPricePojo> getGoodsPrices() { return goodsPrices; } public void setGoodsPrices(Set<GoodsPricePojo> goodsPrices) { this.goodsPrices = goodsPrices; } }
1 package com.medclub.pojo; 2 3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 public class GoodsPricePojo implements Serializable{ 8 @Override 9 public String toString() { 10 return "GoodsPricePojo [id=" + id + ", price=" + price + ", goods=" 11 + goods + ", market=" + market + "]"; 12 } 13 private Integer id; 14 private Double price; 15 private GoodsPojo goods; 16 private MarketPojo market; 17 private Set<OrderItemPojo> orderItems=new HashSet<OrderItemPojo>(); 18 public Integer getId() { 19 return id; 20 } 21 public void setId(Integer id) { 22 this.id = id; 23 } 24 public GoodsPojo getGoods() { 25 return goods; 26 } 27 public void setGoods(GoodsPojo goods) { 28 this.goods = goods; 29 } 30 public MarketPojo getMarket() { 31 return market; 32 } 33 public void setMarket(MarketPojo market) { 34 this.market = market; 35 } 36 37 @Override 38 public int hashCode() { 39 final int prime = 31; 40 int result = 1; 41 result = prime * result + ((goods == null) ? 0 : goods.hashCode()); 42 result = prime * result + ((market == null) ? 0 : market.hashCode()); 43 return result; 44 } 45 @Override 46 public boolean equals(Object obj) { 47 if (this == obj) 48 return true; 49 if (obj == null) 50 return false; 51 if (getClass() != obj.getClass()) 52 return false; 53 GoodsPricePojo other = (GoodsPricePojo) obj; 54 if (goods == null) { 55 if (other.goods != null) 56 return false; 57 } else if (!goods.equals(other.goods)) 58 return false; 59 if (market == null) { 60 if (other.market != null) 61 return false; 62 } else if (!market.equals(other.market)) 63 return false; 64 return true; 65 } 66 public Double getPrice() { 67 return price; 68 } 69 public void setPrice(Double price) { 70 this.price = price; 71 } 72 public Set<OrderItemPojo> getOrderItems() { 73 return orderItems; 74 } 75 public void setOrderItems(Set<OrderItemPojo> orderItems) { 76 this.orderItems = orderItems; 77 } 78 79 }
<class entity-name="com.medclub.pojo.GoodsPojo" name="GoodsPojo" table="Goods"> <id column="id" name="id"> <generator class="identity"/> </id> <property generated="never" lazy="false" name="name"/> <set lazy="false" inverse="true" name="goodsPrices" sort="unsorted"> <key column="goods_id"/> <one-to-many class="GoodsPricePojo"/> </set> </class> <class entity-name="com.medclub.pojo.GoodsPricePojo" name="GoodsPricePojo" table="GoodsPrice"> <id column="id" name="id"> <generator class="identity"></generator> </id> <property generated="never" lazy="false" name="price"/> <many-to-one name="market" lazy="false" column="market_id"/> <many-to-one name="goods" lazy="false" column="goods_id"/> <set lazy="false" inverse="true" name="orderItems"> <key column="goodsprice_id"/> <one-to-many class="OrderItemPojo"/> </set> </class>
只是这两个Pojo的配置文件,因此就单独列出来,便于理解,其他的依次类推就好(不同的model层可以配置在不同的*.hbm.xml文件中此处不深究)。
然后是Dao层,Dao层的设计如下:
DAO接口是所有最基础的增删查改的方法的定义,其他的ModelDao接口继承该接口并拥有自己独立的方法;
BaseDao实现(implements)DAO接口,并继承(extends)SpringSessionContext(这个类操作事务提供数据库的Session)其他的ModelDaoImpl继承(extends)这个BaseDao并实现(implements)自己的接口,如ModelDao
说这么多还是举例子最靠谱,是上面GoodsPojo和GoodsPrice对应的Dao层:
1 package com.medclub.dao; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 public interface DAO{ 7 public Object getObject(Class clazz,Serializable id); 8 public List getObjectList(Class clazz); 9 public void saveObject(Object obj); 10 public void deleteObject(Class clazz,Serializable id); 11 public List getObjectListByExample(Object example); 12 public void saveOrUpdateObject(Object obj); 13 public List getByExampleLike(Object example); 14 public void save(Object obj); 15 }
1 package com.medclub.dao; 2 3 import java.io.Serializable; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Set; 8 import java.util.Map.Entry; 9 10 import org.hibernate.Query; 11 import org.hibernate.Session; 12 import org.hibernate.criterion.MatchMode; 13 import org.hibernate.criterion.Restrictions; 14 import org.hibernate.engine.spi.SessionFactoryImplementor; 15 import org.springframework.orm.ObjectRetrievalFailureException; 16 import org.springframework.orm.hibernate4.SpringSessionContext; 17 import org.hibernate.Criteria; 18 19 import com.kc.reign.util.ReflectHelper; 20 import com.medclub.pojo.DepartmentPojo; 21 22 23 public class BaseDao<T extends Serializable,PK extends Serializable> extends SpringSessionContext implements DAO{ 24 public BaseDao(SessionFactoryImplementor sessionFactory) { 25 super(sessionFactory); 26 // TODO Auto-generated constructor stub 27 } 28 protected ReflectHelper helper; 29 public ReflectHelper getHelper() { 30 return helper; 31 } 32 33 34 public void setHelper(ReflectHelper helper) { 35 this.helper = helper; 36 } 37 38 39 public Object getObject(Class clazz, Serializable id) { 40 // TODO Auto-generated method stub 41 Object o= getHibernateTemplate().get(clazz, id); 42 if(o==null){ 43 throw new ObjectRetrievalFailureException(clazz,id); 44 } 45 return o; 46 } 47 48 49 protected Session getHibernateTemplate() { 50 // TODO Auto-generated method stub 51 return currentSession(); 52 } 53 54 55 public List<T> getObjectList(Class clazz) { 56 // TODO Auto-generated method stub 57 58 return getHibernateTemplate().createQuery("from "+clazz.getName()).list(); 59 } 60 61 public void saveObject(Object obj) { 62 // TODO Auto-generated method stub 63 getHibernateTemplate().save(obj); 64 } 65 66 public void deleteObject(Class clazz, Serializable id) { 67 // TODO Auto-generated method stub 68 getHibernateTemplate().delete(getObject(clazz, id)); 69 } 70 public List<T> getObjectListByExample(Object example){ 71 Map<String,Object> objmap=helper.getMapFromObj(example); 72 return getHibernateTemplate().createCriteria(example.getClass()).add(Restrictions.allEq(objmap)).list(); 73 } 74 75 76 public void saveOrUpdateObject(Object obj) { 77 // TODO Auto-generated method stub 78 getHibernateTemplate().saveOrUpdate(obj); 79 } 80 81 public void save(Object obj){ 82 getHibernateTemplate().save(obj); 83 } 84 public List<T> getByExampleLike(Object example) { 85 // TODO Auto-generated method stub 86 Map<String,Object> objmap=helper.getMapFromObj(example); 87 Criteria criteria=getHibernateTemplate().createCriteria(example.getClass()); 88 Set<Entry<String, Object>> entries=objmap.entrySet(); 89 if(entries!=null) 90 { 91 Iterator<Entry<String, Object>> it=entries.iterator(); 92 while(it.hasNext()) 93 { 94 Map.Entry<String,Object> entry=(Entry<String, Object>) it.next(); 95 if(entry.getKey()!=null&&entry.getValue()!=null) 96 { 97 if(!entry.getValue().toString().trim().equals("")){ 98 criteria.add(Restrictions.ilike(entry.getKey(),(String)entry.getValue(),MatchMode.ANYWHERE)); 99 } 100 } 101 } 102 } 103 return criteria.list(); 104 } 105 106 }
1 package com.medclub.tests.dao; 2 3 import java.util.Date; 4 import java.util.List; 5 6 import com.medclub.dao.DAO; 7 import com.medclub.pojo.GoodsPojo; 8 import com.medclub.pojo.GoodsPricePojo; 9 10 public interface GoodsDao extends DAO{ 11 public List<GoodsPojo> getByNameLike(String name); 12 }
1 package com.medclub.tests.dao; 2 3 import java.util.Date; 4 import java.util.List; 5 6 import com.medclub.dao.DAO; 7 import com.medclub.pojo.GoodsCarPojo; 8 import com.medclub.pojo.GoodsPojo; 9 import com.medclub.pojo.GoodsPricePojo; 10 import com.medclub.pojo.MarketPojo; 11 import com.medclub.pojo.OrderItemPojo; 12 13 public interface GoodsPriceDao extends DAO{ 14 15 public List<GoodsPricePojo> getByGoods(GoodsPojo pojo);//通过商品得到商品对应单价 16 public List<GoodsPricePojo> getByMarket(MarketPojo pojo);//通过超市得到超市对应的所有单价 17 public List<GoodsPricePojo> getOneByGoodsAndMarket(GoodsPojo goods,MarketPojo market);//通过商品和超市得到具体的商品单价 18 public GoodsPricePojo getTheLowestPriceByGoods(GoodsPojo goods);//通过商品得到最低单价(未实现!) 19 }
package com.medclub.tests.daoimpl; import java.util.Date; import java.util.List; import org.hibernate.Criteria; import org.hibernate.engine.spi.SessionFactoryImplementor; import com.medclub.dao.BaseDao; import com.medclub.pojo.GoodsCarPojo; import com.medclub.pojo.GoodsPojo; import com.medclub.pojo.GoodsPricePojo; import com.medclub.tests.dao.GoodsDao; public class GoodsDaoImpl extends BaseDao<GoodsPojo,Integer> implements GoodsDao{ public GoodsDaoImpl(SessionFactoryImplementor sessionFactory) { super(sessionFactory);//sessionFactory:注入 // TODO Auto-generated constructor stub } //通过商品名称得到商品 public List<GoodsPojo> getByNameLike(String name) { // TODO Auto-generated method stub GoodsPojo goods=new GoodsPojo(); goods.setName(name); return getByExampleLike(goods);//通过Like得到List } }
1 package com.medclub.tests.daoimpl; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.engine.spi.SessionFactoryImplementor; 7 8 import com.medclub.dao.BaseDao; 9 import com.medclub.pojo.GoodsCarPojo; 10 import com.medclub.pojo.GoodsPojo; 11 import com.medclub.pojo.GoodsPricePojo; 12 import com.medclub.pojo.MarketPojo; 13 import com.medclub.pojo.OrderItemPojo; 14 import com.medclub.tests.dao.GoodsPriceDao; 15 16 public class GoodsPriceDaoImpl extends BaseDao<GoodsPricePojo,Integer> implements GoodsPriceDao{ 17 private final String HQL="from GoodsPricePojo as g"; 18 public GoodsPriceDaoImpl(SessionFactoryImplementor sessionFactory) { 19 super(sessionFactory); 20 // TODO Auto-generated constructor stub 21 } 22 23 public List<GoodsPricePojo> getByGoods(GoodsPojo pojo) { 24 // TODO Auto-generated method 25 Integer goodsid=pojo.getId(); 26 StringBuffer hql=new StringBuffer(HQL); 27 hql.append(" where g.goods.id=:goodsid");//单价与商品:多对一,由单价来维护与商品之间的关系 28 Query q=getHibernateTemplate().createQuery(hql.toString()); 29 q.setParameter("goodsid",goodsid); 30 return q.list(); 31 } 32 33 public List<GoodsPricePojo> getByMarket(MarketPojo pojo) { 34 // TODO Auto-generated method stub 35 Integer marketid=pojo.getId(); 36 StringBuffer hql=new StringBuffer(HQL); 37 hql.append(" where g.market.id=:marketid"); 38 //单价与超市:一对多,由单价 维护与超市之间的关系 39 Query q=getHibernateTemplate().createQuery(hql.toString()); 40 q.setParameter("marketid",marketid); 41 return q.list(); 42 } 43 44 public List<GoodsPricePojo> getOneByGoodsAndMarket(GoodsPojo goods, 45 MarketPojo market) { 46 // TODO Auto-generated method stub 47 Integer marketid=market.getId(); 48 Integer goodsid=goods.getId(); 49 StringBuffer hql=new StringBuffer(HQL); 50 hql.append(" where g.goods.id=:goodsid and g.market.id=:marketid"); 51 Query q=getHibernateTemplate().createQuery(hql.toString()); 52 q.setParameter("goodsid",goodsid); 53 q.setParameter("marketid", marketid); 54 return q.list(); 55 } 56 57 // public List<GoodsPricePojo> getByOrderItem(OrderItemPojo order) { 58 // // TODO Auto-generated method stub 59 // return null; 60 // } 61 62 // public List<GoodsPricePojo> getByGoodsCar(GoodsCarPojo goodsCar) { 63 // // TODO Auto-generated method stub 64 // Integer goodscarid=goodsCar.getId(); 65 // StringBuffer hql=new StringBuffer(HQL); 66 // hql.append(" where g.goodscar.id=:goodscarid"); 67 // return null; 68 // } 69 //未实现,也没用到 70 public GoodsPricePojo getTheLowestPriceByGoods(GoodsPojo goods) { 71 // TODO Auto-generated method stub 72 return null; 73 } 74 75 76 }
泛型的话,是因为在类加载前加载父类时就初始化Model的实例,对性能比较好,而且可以对BaseDao中的返回值进行处理,不用返回Object再强转,这只是我浅薄的理解,具体的望大神们不吝赐教!
不过貌似大神们都是来看热闹的哎,,,好吧,我献丑啦
接下来是最后的Service层,在Service-Context.xml中配置Service层的组件,依然是先接口再实例,spring的代理之前讲过,是先JDK代理再CGLIB,即配置文件中是实体类,引用的时候引用接口,此处就提一下,不深究啦。配置文件以及例子(还是Goods和GoodsPrice)如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="goodsService" class="com.medclub.service.impl.GoodsServiceImpl"> <property name="daoGoods"><ref bean="goodsDao"/></property> <property name="daoGoodsPrice"><ref bean="goodsPriceDao"/></property> </bean> <bean id="goodsPriceService" class="com.medclub.service.impl.GoodsPriceServiceImpl"> <property name="daoGoodsPrice"><ref bean="goodsPriceDao"/></property> </bean> </beans>
1 package com.medclub.service; 2 3 import java.util.Date; 4 import java.util.List; 5 6 import com.medclub.pojo.GoodsCarPojo; 7 import com.medclub.pojo.GoodsPojo; 8 import com.medclub.pojo.GoodsPricePojo; 9 import com.medclub.pojo.MarketPojo; 10 11 public interface GoodsService { 12 public void saveOrUpdateGoods(GoodsPojo goods);//保存或者更新商品 13 public List<GoodsPojo> getAllGoods();//得到所有的商品 14 public List<GoodsPojo> getNameSpecifiedGoods(String name);//通过商品名的到商品 15 public List<GoodsPricePojo> getGoodsPricesByGoods(GoodsPojo goods);//通过商品得到商品单价 16 public void DeleteGoods(GoodsPojo goods);//删除商品 17 public List<GoodsPojo> getByExample(GoodsPojo goods);//通过实体得到列表 18 19 }
1 package com.medclub.service; 2 3 4 5 import java.util.List; 6 7 import com.medclub.pojo.GoodsPojo; 8 import com.medclub.pojo.GoodsPricePojo; 9 import com.medclub.pojo.MarketPojo; 10 11 public interface GoodsPriceService { 12 public void SaveOrUpdateGoodsPrice(GoodsPricePojo goodsPrice); 13 public void deleteGoodsPrice(GoodsPricePojo goodsPrice); 14 public List<GoodsPricePojo> getByMarket(MarketPojo market); 15 public List<GoodsPricePojo> getByGoods(GoodsPojo goods); 16 public List<GoodsPricePojo> getByGoodsAndMarket(GoodsPojo goods,MarketPojo market); 17 public List<GoodsPricePojo> getAllPrices(); 18 }
1 package com.medclub.service.impl; 2 3 import java.util.List; 4 5 import com.medclub.pojo.GoodsPojo; 6 import com.medclub.pojo.GoodsPricePojo; 7 import com.medclub.pojo.MarketPojo; 8 import com.medclub.service.GoodsService; 9 import com.medclub.tests.dao.GoodsDao; 10 import com.medclub.tests.dao.GoodsPriceDao; 11 12 public class GoodsServiceImpl implements GoodsService{ 13 14 private GoodsDao daoGoods;//商品Dao注入 15 private GoodsPriceDao daoGoodsPrice;//商品单价Dao注入 16 /*保存商品*/ 17 public void saveOrUpdateGoods(GoodsPojo goods) { 18 // TODO Auto-generated method stub 19 daoGoods.saveOrUpdateObject(goods); 20 } 21 /*得到所有商品*/ 22 public List<GoodsPojo> getAllGoods() { 23 // TODO Auto-generated method stub 24 return daoGoods.getObjectList(GoodsPojo.class); 25 } 26 /*通过名字得到商品*/ 27 public List<GoodsPojo> getNameSpecifiedGoods(String name) { 28 // TODO Auto-generated method stub 29 return daoGoods.getByNameLike(name); 30 } 31 /*通过超市得到商品列表*/ 32 public List<GoodsPojo> getGoodsByMarket(MarketPojo market) { 33 // TODO Auto-generated method stub 34 return null; 35 } 36 /*删除商品*/ 37 public void DeleteGoods(GoodsPojo goods) { 38 // TODO Auto-generated method stub 39 daoGoods.deleteObject(GoodsPojo.class,goods.getId()); 40 } 41 /*注入商品dao*/ 42 public GoodsDao getDaoGoods() { 43 return daoGoods; 44 } 45 46 public void setDaoGoods(GoodsDao daoGoods) { 47 this.daoGoods = daoGoods; 48 } 49 /*通过商品得到单价*/ 50 51 public List<GoodsPricePojo> getGoodsPricesByGoods(GoodsPojo goods) { 52 // TODO Auto-generated method stub 53 return daoGoodsPrice.getByGoods(goods); 54 } 55 /*通过例子得到商品列表*/ 56 57 public List<GoodsPojo> getByExample(GoodsPojo goods) { 58 // TODO Auto-generated method stub 59 return daoGoods.getObjectListByExample(goods); 60 } 61 /*注入单价Dao*/ 62 public GoodsPriceDao getDaoGoodsPrice() { 63 return daoGoodsPrice; 64 } 65 66 public void setDaoGoodsPrice(GoodsPriceDao daoGoodsPrice) { 67 this.daoGoodsPrice = daoGoodsPrice; 68 } 69 }
1 package com.medclub.service.impl; 2 3 import java.util.List; 4 5 import com.medclub.pojo.GoodsPojo; 6 import com.medclub.pojo.GoodsPricePojo; 7 import com.medclub.pojo.MarketPojo; 8 import com.medclub.service.GoodsPriceService; 9 import com.medclub.tests.dao.GoodsPriceDao; 10 11 public class GoodsPriceServiceImpl implements GoodsPriceService{ 12 private GoodsPriceDao daoGoodsPrice;//注入单价dao 13 //保存或者更新单价 14 public void SaveOrUpdateGoodsPrice(GoodsPricePojo goodsPrice) { 15 // TODO Auto-generated method stub 16 daoGoodsPrice.saveOrUpdateObject(goodsPrice); 17 } 18 //删除单价 19 public void deleteGoodsPrice(GoodsPricePojo goodsPrice) { 20 // TODO Auto-generated method stub 21 daoGoodsPrice.deleteObject(GoodsPricePojo.class, goodsPrice.getId()); 22 } 23 //注入单价dao 24 public GoodsPriceDao getDaoGoodsPrice() { 25 return daoGoodsPrice; 26 } 27 28 public void setDaoGoodsPrice(GoodsPriceDao daoGoodsPriceDao) { 29 this.daoGoodsPrice = daoGoodsPriceDao; 30 } 31 //通过超市得到所有单价 32 public List<GoodsPricePojo> getByMarket(MarketPojo market) { 33 // TODO Auto-generated method stub 34 return daoGoodsPrice.getByMarket(market); 35 } 36 //通过商品得到单价 37 public List<GoodsPricePojo> getByGoods(GoodsPojo goods) { 38 // TODO Auto-generated method stub 39 return daoGoodsPrice.getByGoods(goods); 40 } 41 //通过商品和超市得到单价 42 public List<GoodsPricePojo> getByGoodsAndMarket(GoodsPojo goods, 43 MarketPojo market) { 44 // TODO Auto-generated method stub 45 return daoGoodsPrice.getOneByGoodsAndMarket(goods, market); 46 } 47 //得到所有单价 48 public List<GoodsPricePojo> getAllPrices() { 49 // TODO Auto-generated method stub 50 return daoGoodsPrice.getObjectList(GoodsPricePojo.class); 51 } 52 53 }
恩,这一篇就写到这里吧,后面再总结action和jsp表现层部分