一键生成mvc各个类之全局替换

来源:互联网 发布:打车外卖优惠券源码 编辑:程序博客网 时间:2024/05/29 11:50

宗旨:定义一个模板,读取内容,全局替换

下面以建一个实体类为例:

第一步:在自己定义的目录下创建模板文件

第二步:定义一个类来处理模板文件生成我们想要的文件,这个类的代码如下:

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;}}


0 0