使用UML设计程序
使用 用例图 画出程序的功能模块(小人代表角色,椭圆代表功能)
第一步:画出实体类的关联关系
使用类图设计程序(关键)
单向箭头表示单向关联,没有箭头表示双向关联,线的属性(关联属性)
类的属性和方法一般隐藏
第二步:Dao的设计与实现
BaseDao定义每个Dao都会使用到的通用接口<<Interface>>
BaseDaoImpl实现BaseDao的抽象类(用斜体表示抽象,用虚线空心箭头表示实现接口)
每一个实体类都会有一个Dao的实现类(用实现空心箭头表示继承一个类,用虚线三角箭头表示引用一个类)
BaseDao<T>
package com.atguigu.surveypark.dao; import java.util.List; /** * BaseDao接口 */ public interface BaseDao<T> { //写操作 public void saveEntity(T t); public void saveOrUpdateEntity(T t); public void updateEntity(T t); public void deleteEntity(T t); public void batchEntityByHQL(String hql,Object...objects); //读操作 public T loadEntity(Integer id); public T getEntity(Integer id); public List<T> findEntityByHQL(String hql,Object...objects); }
package com.atguigu.surveypark.dao.impl; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.SessionFactory; import com.atguigu.surveypark.dao.BaseDao; /** * 抽象的dao实现,专门用于继承 */ @SuppressWarnings("unchecked") public abstract class BaseDaoImpl<T> implements BaseDao<T> { //注入sessionFactory @Resource private SessionFactory sf ; private Class<T> clazz ; public BaseDaoImpl(){ //得到泛型话超类 ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); clazz = (Class<T>) type.getActualTypeArguments()[0]; } public void saveEntity(T t) { sf.getCurrentSession().save(t); } public void saveOrUpdateEntity(T t) { sf.getCurrentSession().saveOrUpdate(t); } public void updateEntity(T t) { sf.getCurrentSession().update(t); } public void deleteEntity(T t) { sf.getCurrentSession().delete(t); } /** * 按照HQL语句进行批量更新 */ public void batchEntityByHQL(String hql, Object... objects) { Query q = sf.getCurrentSession().createQuery(hql); for(int i = 0 ; i < objects.length ; i ++){ q.setParameter(i, objects[i]); } q.executeUpdate(); } public T loadEntity(Integer id) { return (T) sf.getCurrentSession().load(clazz, id); } public T getEntity(Integer id) { return (T) sf.getCurrentSession().get(clazz, id); } public List<T> findEntityByHQL(String hql, Object... objects) { Query q = sf.getCurrentSession().createQuery(hql); for(int i = 0 ; i < objects.length ; i ++){ q.setParameter(i, objects[i]); } return q.list(); } }
第三步:Service设计与实现
分两种情况:一种是单体Service(只操作一个Dao),一种是多体Service(操作多个Dao)
不是每一个Dao都有一个对应的Service
BaseService<T>:定义基本的接口,这个接口是所有的Service通用的
package com.atguigu.surveypark.service; import java.util.List; /** * 基本的dao接口 */ public interface BaseService<T> { //写操作 public void saveEntity(T t); public void saveOrUpdateEntity(T t); public void updateEntity(T t); public void deleteEntity(T t); public void batchEntityByHQL(String hql,Object...objects); //读操作 public T loadEntity(Integer id); public T getEntity(Integer id); public List<T> findEntityByHQL(String hql,Object...objects); }
package com.atguigu.surveypark.service.impl; import java.util.List; import javax.annotation.Resource; import com.atguigu.surveypark.dao.BaseDao; import com.atguigu.surveypark.service.BaseService; /** * 抽象的baseService,专门用于继承 */ public abstract class BaseServiceImpl<T> implements BaseService<T> { private BaseDao<T> dao ; //注入dao @Resource public void setDao(BaseDao<T> dao) { this.dao = dao; } public void saveEntity(T t) { dao.saveEntity(t); } public void saveOrUpdateEntity(T t) { dao.saveOrUpdateEntity(t); } public void updateEntity(T t) { dao.updateEntity(t); } public void deleteEntity(T t) { dao.deleteEntity(t); } public void batchEntityByHQL(String hql, Object... objects) { dao.batchEntityByHQL(hql, objects); } public T loadEntity(Integer id) { return dao.loadEntity(id); } public T getEntity(Integer id) { return dao.getEntity(id); } public List<T> findEntityByHQL(String hql, Object... objects) { return dao.findEntityByHQL(hql, objects); } }
UserService:定义一个扩展接口,这个接口的方法特定义User这个实体类。这里实现BaseServcie这个接口是为了面向接口编程。
package com.atguigu.surveypark.service; import com.atguigu.surveypark.model.User; /** * UserService */ public interface UserService extends BaseService<User> { }
一个简单的示例:可以将UserServiceImpl类注入到BaseServcie这个接口,这样就可以调用BaseServcie的基本方法和扩展方法了
注意:需要覆盖父类的public void setDao(BaseDao<User> dao) {这个方法,因为@Resource先匹配名字再匹配类型,类型已经被BaseServiceImpl使用了一次,多个类使用同一个类型所以是不行的,覆盖这个方法然后使用特定的名字的类的dao进行注入
package com.atguigu.surveypark.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.atguigu.surveypark.dao.BaseDao; import com.atguigu.surveypark.model.User; import com.atguigu.surveypark.service.UserService; @Service("userService") public class UserServiceImpl extends BaseServiceImpl<User> implements UserService { @Resource(name="userDao") public void setDao(BaseDao<User> dao) { super.setDao(dao); } }
第四步:action的设计与实现
BaseAction<T>
package com.atguigu.surveypark.struts2.action; import java.lang.reflect.ParameterizedType; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.Preparable; /** * 抽象action,专门用于继承 */ public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>, Preparable { private static final long serialVersionUID = 9180917383072055589L; public T model ; public BaseAction(){ try { ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); Class clazz = (Class) type.getActualTypeArguments()[0]; model = (T) clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } } public void prepare() throws Exception { } public T getModel(){ return model ; } }
package com.atguigu.surveypark.struts2.action; import javax.annotation.Resource; import org.apache.struts2.interceptor.validation.SkipValidation; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.atguigu.surveypark.model.User; import com.atguigu.surveypark.service.UserService; import com.atguigu.surveypark.util.DataUtil; import com.atguigu.surveypark.util.ValidateUtil; /** * 注册action */ @Controller @Scope("prototype") public class RegAction extends BaseAction<User> { private static final long serialVersionUID = 7351588309970506225L; private String confirmPassword ; //注入userService @Resource private UserService userService ; public String getConfirmPassword() { return confirmPassword; } public void setConfirmPassword(String confirmPassword) { this.confirmPassword = confirmPassword; } /** * 到达注册页面 */ @SkipValidation public String toRegPage(){ return "regPage" ; } /** * 进行用户注册 */ public String doReg(){ //密码加密 model.setPassword(DataUtil.md5(model.getPassword())); userService.saveEntity(model); return SUCCESS ; } /** * 校验 */ public void validate() { //1.非空 if(!ValidateUtil.isValid(model.getEmail())){ addFieldError("email", "email是必填项!"); } if(!ValidateUtil.isValid(model.getPassword())){ addFieldError("password", "password是必填项!"); } if(!ValidateUtil.isValid(model.getNickName())){ addFieldError("nickName", "nickName是必填项!"); } if(hasErrors()){ return ; } //2.密码一致性 if(!model.getPassword().equals(confirmPassword)){ addFieldError("password", "密码不一致!"); return ; } //3.email占用 if(userService.isRegisted(model.getEmail())){ addFieldError("email", "email已占用!"); } } }
ssh Dao与Service的设计与实现,布布扣,bubuko.com
原文:http://blog.csdn.net/coslay/article/details/35595187