关于JPA持久层 代码自动生成工具类

自动生成工具类

  • 路径需要根据自己的本地路径进行修改
package com.jeff.tianti.common.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Dao、Service层代码自动生成工具类
 * @author SuperGu
 * @since 2015-12-09
 */
public class GenCodeUtil {
	
	//公共部分
		private static final String RT_1 = "\r\n";
		private static final String RT_2 = RT_1+RT_1;
		private static final String BLANK_1 =" ";
		
		//注释部分
		private static final String ANNOTATION_AUTHOR_PARAMTER = "@author ";
		private static final String ANNOTATION_DESC = "@desc ";
		private static final String ANNOTATION_DATE = "@date ";
		
		/**
		 * 创建查询信息封装DTO(用于后台的查询信息封装)
		 * @param c实体类
		 * @param commonPackage 基础包:如com.jeff.tianti.info
		 * @param author 作者
		 * @param desc 描述
		 * @throws IOException
		 */
		public static void createQueryDTO(Class c,String commonPackage,String author) throws IOException{
			String cName = c.getName();
			String dtoPath = "";
			if(author == null || author.equals("")){
				author = "Administrator";
			}
			if(commonPackage != null && !commonPackage.equals("")){
				System.out.println("System.getProperty(\"user.dir\")+++++++++"+System.getProperty("user.dir"));
				String fileName = System.getProperty("user.dir") + "/tianti-cms/src/main/java/com/jeff/tianti/cms/"/*src/main/java/"*/ + dtoPath+"/dto"
						+ "/" + getLastChar(cName) + "QueryDTO.java";
				File f = new File(fileName);
				FileWriter fw = new FileWriter(f);
				fw.write("package "+commonPackage+".dto"+";"+RT_2+"import com.jeff.tianti.common.dto.CommonQueryDTO;"+RT_2
						+"/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR_PARAMTER+ author +RT_1
						+BLANK_1+"*"+BLANK_1+ANNOTATION_DESC +getLastChar(cName)+"QueryDTO"+BLANK_1+RT_1
						+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1
						+"public class " +getLastChar(cName) +"QueryDTO extends CommonQueryDTO{"+RT_2+"}");
				fw.flush();
				fw.close();
				showInfo(fileName);
			}else{
				System.out.println("创建"+getLastChar(cName)+"QueryDTO失败,原因是commonPackage为空!");
			}
		}
		
		/**
		 * 创建查询信息封装DTO(用于前台的查询信息封装)
		 * @param c实体类
		 * @param commonPackage 基础包:如com.jeff.tianti.info
		 * @param author 作者
		 * @param desc 描述
		 * @throws IOException
		 */
		public static void createFrontQueryDTO(Class c,String commonPackage,String author) throws IOException{
			String cName = c.getName();
			String dtoPath = "";
			if(author == null || author.equals("")){
				author = "Administrator";
			}
			if(commonPackage != null && !commonPackage.equals("")){
				dtoPath = commonPackage.replace(".", "/");
				String fileName = System.getProperty("user.dir") + "/src/main/java/" + dtoPath+"/dto"
						+ "/" + getLastChar(cName) + "FrontQueryDTO.java";
				File f = new File(fileName);
				FileWriter fw = new FileWriter(f);
				fw.write("package "+commonPackage+".dto"+";"+RT_2+"import com.jeff.tianti.common.dto.CommonQueryDTO;"+RT_2
						+"/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR_PARAMTER+ author +RT_1
						+BLANK_1+"*"+BLANK_1+ANNOTATION_DESC +getLastChar(cName)+"QueryDTO"+BLANK_1+RT_1
						+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1
						+"public class " +getLastChar(cName) +"FrontQueryDTO extends CommonQueryDTO{"+RT_2+"}");
				fw.flush();
				fw.close();
				showInfo(fileName);
			}else{
				System.out.println("创建"+getLastChar(cName)+"FrontQueryDTO失败,原因是commonPackage为空!");
			}
		}

		/**
		 * 创建Dao接口
		 * @param c实体类
		 * @param commonPackage 基础包:如com.jeff.tianti.info
		 * @param author 作者
		 * @param desc 描述
		 * @throws IOException
		 */
		public static void createDaoInterface(Class c,String commonPackage,String author) throws IOException{
			String cName = c.getName();
			String daoPath = "";
			if(author == null || author.equals("")){
				author = "Administrator";
			}
			if(commonPackage != null && !commonPackage.equals("")){
				daoPath = commonPackage.replace(".", "/");
				String fileName = System.getProperty("user.dir") + "/tianti-cms/src/main/java/"/*src/main/java/"*/ + daoPath+"/dao"
						+ "/" + getLastChar(cName) + "Dao.java";
				File f = new File(fileName);
				FileWriter fw = new FileWriter(f);
				fw.write("package "+commonPackage+".dao"+";"+RT_2+"import "+commonPackage+".entity"+"."+getLastChar(cName)+";"+RT_1+"import com.jeff.tianti.common.dao.CommonDao;"+RT_2
						+"/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR_PARAMTER+ author +RT_1
						+BLANK_1+"*"+BLANK_1+ANNOTATION_DESC +getLastChar(cName)+"Dao接口"+BLANK_1+RT_1
						+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1
						+&
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值