实习第15天 封装好ssh框架的一些基础类。
2015-08-19 17:18
197 查看
Service层 : 合并了Service与Dao。
当然对于简单的项目可以这样做,
用起来也方便,直接在service层写hql语句。
基本结构如下
BaseAcion.java
DaoSupport
DaoSupportImpl
QueryHelper
检查:
属于实体的属性
关联关系的属性(一个关联关系对应两个实体,不漏不重)
减少运算而冗余的特殊属性
一、特殊属性的作用
二、特殊属性的维护
hbm.xml:映射关系
模板
2,定义出Action中的方法,要写出方法名、参数、作用、返回值。
3,创建出所用到的JSP页面(目前还没有具体内容)。
4,配置Action:
1,在MyAction上写注解 @Controller与@Scope("prototype").
2,在strtus.xml中配置这个Action与所用到的result.
2,创建实现类MyServiceImpl extends DaoSupportImpl.
3,配置:在MyServiceImpl上写注解:
@Service 与 @Transactional
4,声明:在BaseAction中声明:
@Resource protected MyService myService;
b,包含进来公共的资源:
<%@ include file=“../public/commons.jspf" %>
c,把 ../ 替换为 ${pageContext.request.contextPath}/
d,修改页面内容(使用自定义标签)
当然对于简单的项目可以这样做,
用起来也方便,直接在service层写hql语句。
基本结构如下
BaseAcion.java
package edu.hainu.knowledge.base; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import edu.hainu.knowledge.service.LogService; public abstract class BaseAcion<T> extends ActionSupport implements ModelDriven<T>{ protected T model; //子类默认调用父类的构造函数 public BaseAcion(){ //获取当前子类的父类 BaseAcion ParameterizedType pt=(ParameterizedType)this.getClass().getGenericSuperclass(); //获取该类的泛型参数的第一个参数(就是子类传入的实体类) Class<T> clazz =(Class<T>) pt.getActualTypeArguments()[0]; //通过反射创建model的实例 try { model=clazz.newInstance(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } } @Override public T getModel() { return model; } //===============需要的Service统一注入=============== //@Resource //protected BaseService<T> baseService; @Resource protected LogService logService; }
DaoSupport
package edu.hainu.knowledge.base; import java.util.List; public interface DaoSupport<T> { /** * 保存实体 * @param entity */ void save(T entity); /** * 删除实体 * @param entity */ void delete(Long id); /** * 更新实体 * @param entity */ void update(T entity); /** * 按id查询 * @param id * @return */ T getById(Long id); /** * 按id数组查询 * @param ids * @return */ List<T> getByIds(Long[] ids); /** * 查询所有 * @return */ List<T> findAll(); }
DaoSupportImpl
package edu.hainu.knowledge.base; import java.lang.reflect.ParameterizedType; import java.util.Collections; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; @Transactional public class DaoSupportImpl<T> implements DaoSupport<T>{ @Resource private SessionFactory sessionFactory; private Class<T> clazz; public DaoSupportImpl() { // 使用反射技术得到T的真实类型 ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型 this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型 System.out.println("clazz ---> " + clazz); } /** * 获取当前可用的Session * 共享一个sessionFactory容器,线程安全的 * @return */ protected Session getSession(){ return sessionFactory.getCurrentSession(); } @Override public void save(T entity) { getSession().save(entity); } @Override public void delete(Long id) { Object obj =getById(id); if(obj!=null){ getSession().delete(obj); } } @Override public void update(T entity) { getSession().update(entity); } @Override public T getById(Long id) { if(id==null){ return null; }else { return (T) getSession().get(clazz,id); } } @Override public List<T> getByIds(Long[] ids) { if(ids==null||ids.length==0){ //防止nullException return Collections.EMPTY_LIST; }else{ return getSession().createQuery(// "FROM " + clazz.getSimpleName() + " WHERE id IN (:ids)")// .setParameterList("ids", ids)// .list(); } } @Override public List<T> findAll() { return getSession().createQuery(// "FROM " + clazz.getSimpleName())// .list(); } }
QueryHelper
package edu.hainu.knowledge.util; import java.util.ArrayList; import java.util.List; /** * 用于辅助拼接HQL语句 * * @author Administrator * */ public class QueryHelper { private String fromClause; // From 子句 private String whereClause = ""; // Where子句 private String orderByClause = "";// OrderBy子句 private List<Object> parameters = new ArrayList<Object>(); /** * 生成From子句 * * @param clazz * @param alias */ public QueryHelper(Class clazz, String alias) { fromClause = "FROM " + clazz.getSimpleName() + " " + alias; } /** * 拼接Where子句 * * @param condition * @param params * @return */ public QueryHelper addCondition(String condition, Object... params) { // 拼接 if (whereClause.length() == 0) { whereClause = " WHERE " + condition; } else { whereClause += " AND " + condition; } // 参数 if (params != null) { for (Object p : params) { parameters.add(p); } } return this; } /** * 拼接OrderBy子句 * * @param propertyName * 需要排序的属性名 * @param asc * true Asc false desc * @return */ public QueryHelper addOrderProperty(String propertyName, boolean asc) { if (orderByClause.length() == 0) { orderByClause = " ORDER BY " + propertyName; } else { orderByClause += " AND " + propertyName; } orderByClause += (asc ? " ASC" : " DESC"); return this; } /** * 获取生成的用于查询数据列表的HQL语句 * @return */ public String getListQueryHql(){ return fromClause+whereClause+orderByClause; } /** * 获取生成的用于查询总记录数的HQL语句 * @return */ public String getCountQueryHql(){ return "SELECT COUNT(*) "+fromClause+whereClause; } /** * 获取HQL中的参数列表 * @return */ public List<Object> getParameters() { return parameters; } public void setParameters(List<Object> parameters) { this.parameters = parameters; } }
流程总结:实现增删改查一组功能的步骤
一.设计实体
增加ER图中,实体之间的关系。检查:
属于实体的属性
关联关系的属性(一个关联关系对应两个实体,不漏不重)
减少运算而冗余的特殊属性
一、特殊属性的作用
Forum | topicCount | 主题数量 |
articleCount | 文章数量(主题数+回复数) | |
lastTopic | 最后发表的主题 | |
Topic | replyCount | 回复数量 |
lastReply | 最后发表的回复 | |
lastUpdateTime | 最后更新时间(主题的发表时间或最后回复的时间) |
发表新主题 | 发表新回复 | ||
Forum | topicCount | 加1 | |
articleCount | 加1 | 加1 | |
lastTopic | 更新为当前的新主题 | ||
Topic | replyCount | 0,默认值 | 加1 |
lastReply | Null,默认值 | 更新为当前的新回复 | |
lastUpdateTime | 主题的发表时间 | 更新为当前新回复的时间 |
domain:写javabean
hbm.xml:映射关系
模板
<id name="id"> <generator class="native" /> </id> <property name="name" />
<!—本类的attribute属性,本类与class类的一对多 --> <set name=" "> <key column=" "></key> <!-- key指明了多的一方中的外键名--> <one-to-many class=" "/>
<!--本类的attribute属性,本类与class类的多对一 --> <many-to-one name=" " class=" " column=" "> </many-to-one>
<!--本类的attribute属性,,本类与class类的多对多 --> <set name=" " table=" " > <key column=" "></key> <many-to-many class=" " column=" "></many-to-many> </set>
<!--本类的attribute属性,本类与class类一对一。 采用基于外键的一对一映射,本方有外键。 --> <many-to-one name=" " class=" " column=" " unique="true"></many-to-one>
2.做Action相关的准备
1,创建 MyAction extends BaseAction.2,定义出Action中的方法,要写出方法名、参数、作用、返回值。
/** 列表 */ public String list() throws Exception { return "list"; } /** 删除 */ public String delete() throws Exception { return "toList"; } /** 添加页面 */ public String addUI() throws Exception { return "saveUI"; } /** 添加 */ public String add() throws Exception { return "toList"; } /** 修改页面 */ public String editUI() throws Exception { return "saveUI"; } /** 修改 */ public String edit() throws Exception { return "toList"; }
3,创建出所用到的JSP页面(目前还没有具体内容)。
4,配置Action:
1,在MyAction上写注解 @Controller与@Scope("prototype").
2,在strtus.xml中配置这个Action与所用到的result.
二、做Service相关的准备
1,创建接口MyService extends DaoSupport.2,创建实现类MyServiceImpl extends DaoSupportImpl.
3,配置:在MyServiceImpl上写注解:
@Service 与 @Transactional
4,声明:在BaseAction中声明:
@Resource protected MyService myService;
三、填空:JSP页面的内容:
a,拷贝静态页面中的源代码到JSP中。b,包含进来公共的资源:
<%@ include file=“../public/commons.jspf" %>
c,把 ../ 替换为 ${pageContext.request.contextPath}/
d,修改页面内容(使用自定义标签)
相关文章推荐
- C\C++编程题之寻找丑数
- PopupWindow弹窗实现分享面板
- My SQL InnoDB 1217 - Cannot delete or update a parent row:aforeign key constraint fals
- 1、ViewPager的使用
- Linux下安装Redis应用
- 安卓自定义控件(循环Gallery)
- MFC各种控件属性介绍
- sgu 185 Two shortest(最短路+最大流)
- python-restful 代码Demo
- Java的Timer和TimerTask
- QComboBox实现复选功能
- 用smack+openfire做即时通讯
- Android如何在初始化的时候获取加载的布局的宽高
- 知乎创始人周源:社区类产品从0到1的冷启动,如何完成?
- Introduction to Java Programming编程题3.27<判断点是否在三角形内>
- 警告 1 warning C4819: 该文件包含不能在当前代码页(936)中表示的字符
- Window Layers and Levels , Mac OS
- 关于java中split的使用
- 类型转换——切片问题
- JavaScript