SSH项目搭建-03-DAO层,Controller层创建及代码抽取
2017-09-20 16:42
357 查看
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.getHib 4000 ernateTemplate().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模块中创建一个泛型转换的工具类TUtilspublic class BaseAction<T> extends ActionSupport implements 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(); } } @Override public T getModel() { return entity; } @Override public void setServletRequest(HttpServletRequest httpServletRequest) { this.request = httpServletRequest; } @Override public 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项目搭建-01-使用idea创建Maven工程
- 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架
- 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架
- Maven4MyEclipse 搭建ssh2+extjs项目(1)创建mavenweb项目
- SSH项目(struts+spring+hibernate)搭建_代码简化
- 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架
- 【SSH 框架项目的搭建--简化版】使用注解代替xml中的<bean>,减少代码量
- [BI项目记]-搭建代码管理环境之创建团队项目
- [BI项目记]-搭建代码管理环境之创建团队项目
- 纯代码创建UITabBarController为window的根控制器的项目导航栏搭建
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例
- 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架
- SSH-BOS项目:底层代码抽取(BaseDao、BaseAction)
- IOS博客项目搭建-03-代码重构
- django创建虚拟环境、自定义用户、用户拦截以及项目的搭建
- Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程
- VisualStudio 快速创建空解决方案项目代码
- 【JAVA 动态创建风水罗盘 超精华Swing项目方案-项目目录 - 文章03】
- Maven项目的创建及SSM环境的搭建
- 框架搭建(从已有项目抽取出框架)