1.DAO层抽取

创建抽取公共方法的接口ICommonDao和它的实现类CommonDaoImpl,DAO层所有接口继承ICommonDao,所有实现类继承CommonDaoImpl,这样就达到了抽取公共方法的目的。

public interface ICommonDao<T> {void save(T entity);void update(T entity);T findObjectByID(Serializable id);void deleteObjectByIds(Serializable... ids);void deleteObjectByCollection(List<T> list);List<T> findCollectionByConditionNoPage(String condition, Object[] params, Map<String, String> orderby);}
public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {/**T型转换*/Class entityClass = TUtils.getActualType(this.getClass());@Resource(name="sessionFactory")public void setDi(SessionFactory sessionFactory){this.setSessionFactory(sessionFactory);}/**保存*/public void save(T entity) {this.getHibernateTemplate().save(entity);}/**更新*/public void update(T entity) {this.getHibernateTemplate().update(entity);}/**使用主键ID,查询对象*/public T findObjectByID(Serializable id) {return (T) this.getHibernateTemplate().get(entityClass, id);}/**删除(使用主键ID删除)*/public void deleteObjectByIds(Serializable... ids) {if(ids!=null && ids.length>0){for(Serializable id:ids){Object entity = this.findObjectByID(id);this.getHibernateTemplate().delete(entity);}}}/**删除(使用集合List进行删除)*/public void deleteObjectByCollection(List<T> list) {this.getHibernateTemplate().deleteAll(list);}/**数据库查询方法*/public List<T> findCollectionByConditionNoPage(String condition,final Object[] params, Map<String, String> orderby) {/*** 1.先写出hql语句的基本内容*/String hql = "from "+entityClass.getSimpleName()+" o where 1=1 ";/*** 2.添加查询结果的排序约束* 将Map集合中存放的字段排序,组织成ORDER BY o.textDate ASC,o.textName DESC*/String orderbyCondition = this.orderbyHql(orderby);/*** 3.将各个语句结合拼装成最终的hql语句*/final String finalHql = hql + condition + orderbyCondition;//查询,执行hql语句List<T> list = this.getHibernateTemplate().execute(new HibernateCallback() {public Object doInHibernate(Session session)throws HibernateException {Query query = session.createQuery(finalHql);if(params!=null && params.length>0){for(int i=0;i<params.length;i++){query.setParameter(i, params[i]);}}return query.list();}});return list;}/*** 将Map集合中存放的字段排序,组织成*  ORDER BY o.textDate ASC,o.textName DESC** map集合orderby内,key值为属性名,例如 o.textDate*                 value值为排序方式,例如 DESC(降序)*                 加起来组成的语句 o.textDate DESC 的含义是按textDate降序**/private String orderbyHql(Map<String, String> orderby) {StringBuffer buffer = new StringBuffer("");if(orderby!=null && orderby.size()>0){buffer.append(" ORDER BY ");for(Map.Entry<String, String> map:orderby.entrySet()){buffer.append(map.getKey()+" "+map.getValue()+",");}//在循环后,删除最后一个逗号buffer.deleteCharAt(buffer.length()-1);}return buffer.toString();}
}

2.Controller层抽取

抽取模型驱动的实现方法getModel(),同时在utils模块中创建一个泛型转换的工具类TUtils

public class BaseAction<T> extends ActionSupportimplements ModelDriven<T>,ServletRequestAware,ServletResponseAware {T entity;protected HttpServletResponse response;protected HttpServletRequest request;public BaseAction() {Class entityClass = TUtils.getActualType(this.getClass());try{entity = (T) entityClass.newInstance();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();}}@Overridepublic T getModel() {return entity;}@Overridepublic void setServletRequest(HttpServletRequest httpServletRequest) {this.request = httpServletRequest;}@Overridepublic void setServletResponse(HttpServletResponse httpServletResponse){this.response = httpServletResponse;}}
public class TUtils {public static Class getActualType(Class entity){ParameterizedType parameterizedType = (ParameterizedType) entity.getGenericSuperclass();Class entityClass = (Class) parameterizedType.getActualTypeArguments()[0];return entityClass;}
}

SSH项目搭建-03-DAO层,Controller层创建及代码抽取相关推荐

  1. java 框架 Dao层 Mapper层 controller层 service层 model层 entity层 简介

    目录 简介 entity层 mapper层 service层 controller层 简介 SSM是sping+springMVC+mybatis集成的框架. MVC即model view contr ...

  2. 实战SSM_O2O商铺_41【前端展示】店铺列表页面Dao+Service+Controller层的实现

    文章目录 概述 Dao层 接口 映射文件 单元测试 Service层 接口方法 单元测试 Controller层 增加 ShopListController 单元测试 Github地址 概述 在完成了 ...

  3. 实战SSM_O2O商铺_39【前端展示】首页轮播图和一级商铺Dao+Service+Controller层的实现

    文章目录 概述 HeadLine Dao层 接口 映射文件 单元测试 HeadLine Service层 接口 实现类 单元测试 ShopCategory Dao层完善 映射文件完善 单元测试 Con ...

  4. 实战SSM_O2O商铺_36【商品】商品列表之Dao+Service+Controller层的实现

    文章目录 概述 Dao层 ProductDao.java ProductDao.xml 单元测试 Service层 ProductService.java ProductServiceImpl.jav ...

  5. Dao,Service,Controller层作用

    1.Dao层:全称Data Access Object.Dao层比较底层,负责与数据库打交道具体到对某个表.某个实体的增删改查 2.Service层:又叫服务层或业务层,封装Dao层的操作,使一个方法 ...

  6. Dao层service层controller层mannager层和biz层详解

    本篇文章内容 1.阿里开发手册关于应用分层的部分 2.对于阿里应用分层的理解 1.阿里开发手册关于应用分层的介绍 1.开放接口层: 可直接封装 Service 方法暴露成 RPC 接口:通过 Web ...

  7. mybatis Dao层 Mapper层 controller层 service层 model层 entity层 简介

    简介 SSM是sping+springMVC+mybatis集成的框架. MVC即model view controller. model层=entity层.存放我们的实体类,与数据库中的属性值基本保 ...

  8. 实战SSM_O2O商铺_15【商铺注册】View层+Controller层之图片上传

    文章目录 概述 Maven依赖 文件上传解析器bean的配置 页面 shopoperation.html增加上传组件 shopoperation.js Controller层 Github地址 概述 ...

  9. 【转】DAO层,Service层,Controller层、View层

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  10. Spring boot 三层框架dao层、service层、controller层+实体model层

    Spring boot 三层框架dao层.service层.controller层+实体model层 model层 dao层 service层 controller层 首先创建一个springboot ...

最新文章

  1. windows 服务开发教程
  2. excel文件数据导入mysql数据库中_将excel里面的数据导入mysql数据库中
  3. android封装网络请求界面,轻松搞定 android MVP 架构、okHttp 网络模块封装 的 项目...
  4. 计算机系统操作工培训视频,计算机系统操作工培训第三篇.ppt
  5. vue中使用keepAlive组件缓存遇到的坑
  6. unity获取ugui上鼠标位置
  7. 算法章节 数组、链表、栈、队列
  8. VMware虚拟机软件
  9. 职称计算机xp练习题,职称计算机考试模块WindowsXP练习题(1)
  10. jquery 弹出层插件
  11. 抛开百度、知乎等都找不到连接不上服务器远程桌面的原因
  12. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-4.动态Sql语句Mybaties SqlProvider...
  13. linux bt 命令行,linux命令行bt下载工具
  14. TD-LTE原理及其关键技术介绍
  15. 一般纳税人税额计算_一般纳税人应纳税额如何计算?
  16. 在React中使用Shadow DOM
  17. windows10搜索卡死(有出现Cortana的请看这篇文章)
  18. 为什么我们不能坚持?
  19. indexOf 的使用
  20. 嚣张:分库分表就能无限扩容吗?

热门文章

  1. qpython 使用教程_python中spy++的使用超详细教程
  2. python设置excel的格式_python 操作Excel 设置格式
  3. Java编程:排序算法——冒泡排序
  4. Cesium:本地node运行cesium报错Must use import to load ES Module
  5. EChart:EChart与NProgress结合异步加载数据
  6. adams matlab 柔性体,【ADAMS柔性体】柔性体生成流程与注意事项
  7. SLAM_SLAM问题求解框架
  8. 深度学习笔记_各种激活函数总结对比
  9. 设计模式-适配器模式(Adapter)
  10. HTML DOM 的nodeType属性