一键生成mvc各个类之全局替换
2016-01-25 10:48
295 查看
宗旨:定义一个模板,读取内容,全局替换
下面以建一个实体类为例:
第一步:在自己定义的目录下创建模板文件
第二步:定义一个类来处理模板文件生成我们想要的文件,这个类的代码如下:
下面直接run,然后到指定目录下refresh就可以看见生成的文件了,以上为生成实体类的例子,dao,action这类也是一样。
模板文件内容:
下面以建一个实体类为例:
第一步:在自己定义的目录下创建模板文件
第二步:定义一个类来处理模板文件生成我们想要的文件,这个类的代码如下:
public static void createEntity(){ try { //模板页面 String daoTemplate = getPath("template\\entity1.txt"); //写入到磁盘里面去 String result = replaceModel(daoTemplate); //要生成的根目录或者指定的目录下 String daoRoot = getPath("template"); File rootPath = new File(daoRoot); //如果不存在那么久创建 if(!rootPath.exists())rootPath.mkdirs(); //产生接口文件 File daoJava = new File(rootPath, entity+".java"); //讲模板中替换好的数据通过写入目录中去 FileUtils.writeStringToFile(daoJava, result, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } }
/** * 把定义的常量在模板文件中进行替换 */ public static String replaceModel(String path) throws IOException { String result = FileUtils.readFileToString(new File(path),"UTF-8"); result = result.replaceAll("\\[entity\\]", entity) .replaceAll("\\[lowEntity\\]", lowEntity) .replaceAll("\\[mainfield1\\]", mainfield1) .replaceAll("\\[author\\]", author) .replaceAll("\\[mainfield2\\]", mainfield2) .replaceAll("\\[mainfield3\\]", mainfield3) .replaceAll("\\[mainfield4\\]", mainfield4) .replaceAll("\\[maintablename\\]", maintablename) .replaceAll("\\[tablename1\\]", tablename1) .replaceAll("\\[tablename2\\]", tablename2) .replaceAll("\\[tablename3\\]", tablename3) .replaceAll("\\[tablename4\\]", tablename4); return result; }
public static boolean isEmpty(String str) { return null == str || str.length() == 0 || "".equals(str)|| str.matches("\\s*"); }
public static String getPath(String appendPath){ String path = System.getProperty("user.dir"); if(isEmpty(appendPath)){ return path; }else{ return path+"\\"+appendPath; } }
public static void main(String[] args) { createEntity(); }
下面直接run,然后到指定目录下refresh就可以看见生成的文件了,以上为生成实体类的例子,dao,action这类也是一样。
模板文件内容:
package server.dataaccess.service.hibernate; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.transform.Transformers; import org.hibernate.type.StandardBasicTypes; import org.springframework.stereotype.Component; import server.dataaccess.po.PagePO; import server.dataaccess.po.[entity]PO; import server.dataaccess.service.[entity]DAO; import server.dataaccess.type.[entity]Type; import server.dataaccess.type.VipconuponlogModel; @Component("[lowEntity]DAO") public class [entity]DAOImpl extends GenericHibernateDAO<PictureBookCouponPO> implements [entity]DAO{ public static final String GET_Picture_Book_Coupon_SQL= " select child.id as childId, operater.id as operaterId, pictureBookCoupon.[mainfield3] as schoolId,[lowEntity].[mainfield4] as classId, " + " child.display_name as childName,operater.name as systemUserName,class.class_name as className,school.full_name as schoolName, [lowEntity].type as type " + " from [maintablename] as [lowEntity] " + " left join [tablename1] as child on [lowEntity].[mainfield1]=child.id " + " left join [tablename2] as operater on [lowEntity].[mainfield2]= operater.id " + " left join [tablename3] as school on [lowEntity].[mainfield3]= school.id " + " left join [tablename4] as class on [lowEntity].[mainfield4]=class.id"; public static final String GET_CHILD_COUNT_SQL = " select count(1) from [maintablename] as [lowEntity] " + " left join [tablename1] as child on [lowEntity].[mainfield1]=child.id " + " left join [tablename2] as operater on [lowEntity].[mainfield2]=operater.id " + " left join [tablename3] as school on [lowEntity].[mainfield3]=school.id " + " left join [tablename4] as class on [lowEntity].[mainfield4]=class.id "; public PagePO<VipconuponlogModel> getVipconuponlog(String childName, String systemUserName,String schoolName, String className, [entity]Type type, int page, int pageSize) { final int offset = PagePO.countOffset(pageSize, page);// 当前页开始记录 String sqlWhere = " where [lowEntity].deleted = false "; if (type != [entity].ALL) { sqlWhere += " and [lowEntity].type =" + type.getValue(); } if (StringUtils.trimToNull(childName) != null) { sqlWhere += " and (lower(child.display_name) like :childName)"; } if (StringUtils.trimToNull(systemUserName) != null) { sqlWhere += " and (lower(operater.name) like :systemUserName)"; } if (StringUtils.trimToNull(schoolName) != null) { sqlWhere += " and (lower(school.full_name) like :schoolName)"; } if (StringUtils.trimToNull(className) != null) { sqlWhere += " and (lower(class.class_name) like :className)"; } String order = " order by [lowEntity].id desc"; PreparedSqlAndArgs p = new PreparedSqlAndArgs(GET_Picture_Book_Coupon_SQL + sqlWhere + order, null); SQLQuery query = ((SQLQuery) getSession().createSQLQuery(p.sql).setResultTransformer(Transformers.aliasToBean(VipconuponlogModel.class))) .addScalar("childId", StandardBasicTypes.LONG) .addScalar("operaterId", StandardBasicTypes.LONG) .addScalar("schoolId", StandardBasicTypes.LONG) .addScalar("classId", StandardBasicTypes.LONG) .addScalar("childName", StandardBasicTypes.STRING) .addScalar("systemUserName", StandardBasicTypes.STRING) .addScalar("schoolName", StandardBasicTypes.STRING) .addScalar("className", StandardBasicTypes.STRING) .addScalar("type", StandardBasicTypes.INTEGER); if (StringUtils.trimToNull(childName) != null) { query.setParameter("childName", "%" + childName + "%"); } if (StringUtils.trimToNull(systemUserName) != null) { query.setParameter("systemUserName", "%" + systemUserName + "%"); } if (StringUtils.trimToNull(schoolName) != null) { query.setParameter("schoolName", "%" + schoolName + "%"); } if (StringUtils.trimToNull(className) != null) { query.setParameter("className", "%" + className + "%"); } int allRow = this.getChildCount( childName,systemUserName, schoolName,className,type);// 总记录数 query.setFirstResult(offset); if (pageSize >= 0) { query.setMaxResults(pageSize); } @SuppressWarnings("unchecked") List<VipconuponlogModel> list = (List<VipconuponlogModel>) query.list(); int totalPage = PagePO.countTotalPage(pageSize, allRow);// 总页数 final int currentPage = PagePO.countCurrentPage(page); // 把分页信息保存到Bean中 PagePO<VipconuponlogModel> pagePO = new PagePO<VipconuponlogModel>(); pagePO.setPageSize(pageSize); pagePO.setCurrentPage(currentPage); pagePO.setAllRow(allRow); pagePO.setTotalPage(totalPage); pagePO.setList(list); pagePO.init(); return pagePO; } public int getChildCount(String childName,String systemUserName, String schoolName, String className, [entity]Type type) { String sqlWhere = " where pictureBookCoupon.deleted = false "; if (type != [entity].ALL) { sqlWhere += " and [lowEntity].type =" + type.getValue(); } if (StringUtils.trimToNull(childName) != null) { sqlWhere += " and (lower(child.display_name) like :childName)"; } if (StringUtils.trimToNull(systemUserName) != null) { sqlWhere += " and (lower(operater.name) like :systemUserName)"; } if (StringUtils.trimToNull(schoolName) != null) { sqlWhere += " and (lower(school.full_name) like :schoolName)"; } if (StringUtils.trimToNull(className) != null) { sqlWhere += " and (lower(class.class_name) like :className)"; } PreparedSqlAndArgs pCount = new PreparedSqlAndArgs(GET_CHILD_COUNT_SQL+ sqlWhere, null); Query queryCount = getSession().createSQLQuery(pCount.sql); if (childName != null && !childName.isEmpty()) { queryCount.setParameter("childName", "%" + childName + "%"); } if (schoolName != null && !schoolName.isEmpty()) { queryCount.setParameter("schoolName", "%" + schoolName + "%"); } if (className != null && !className.isEmpty()) { queryCount.setParameter("className", "%" + className + "%"); } if (systemUserName != null && !systemUserName.isEmpty()) { queryCount.setParameter("systemUserName", "%" + systemUserName + "%"); } int allRow = Integer.parseInt(queryCount.uniqueResult().toString());// 总记录数 return allRow; } }
相关文章推荐
- 20.Which three statements are true regarding the data types in Oracle Database 10g/11g? (Choosethree
- JS之模块化工具requirejs教程(二):基本知识
- java 回调的同步回调和异步回调
- 三个迹象是时候升级你的最新KVM切换器
- Linux系统下如何查看物理内存占用率
- rabbitmqctl 命令集合
- JS之模块化工具requirejs教程(一)
- 【链表】Linked List Cycle
- iOS UICollectionView之二(垂直滚动)
- dataGridView设置自增
- 初识AutoMapper
- 超赞的jQuery图片滑块动画特效代码汇总
- ZOJ-3609-Modular Inverse【同余定理】【9th浙江省赛】
- iOS开发笔记--数据持久化的四种方式
- iOS开发笔记--viewWithTag获取subview规则详解
- Oracle根据表名生成唯一流水
- js之RequireJs学习
- 启动模式
- 【转】 (C#)利用Aspose.Cells组件导入导出excel文件
- 推送服务的心跳与断线重连