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

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

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

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

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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值