根据Entity生成DAO、Service文件

以前写过一次,是通过.properties生成的。后面用JDBC框架就没用了,结果不知道丢哪儿去了~

这次根据现在的框架,又搞了一个,记录下;

网上搜索了下,根据自己的工程结构改的;看着可能会有点乱,不过只要根据自己的目录结构先把基础的路径改了,再main执行一次,再一个一个的调写入的内容就好了~

BeanUtilTest

package com.washcar.crm.utils.auto;

import com.washcar.crm.entity.orm.AershuaiTextEntity;

public class BeanUtilTest {

	public static void main(String[] args) throws Exception {
		BeanUtilTest beanUtilTest = new BeanUtilTest();
		BeanUtils beanUtils = new BeanUtils();
		beanUtilTest.beanTool(beanUtils, User.class);
	}

	/**
	 * 根据bean生成相应的文件
	 * 
	 * @param beanUtils
	 * @param c
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public void beanTool(BeanUtils beanUtils, Class c) throws Exception {
		beanUtils.createBeanDao(c);
		beanUtils.createBeanDaoImpl(c);
		beanUtils.createBeanService(c);
		beanUtils.createBeanServiceImp(c);
	}
}

BeanUtils

package com.washcar.crm.utils.auto;

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

/**
 * 
 * @author ershuai
 * @date 2015年8月14日 下午2:58:20
 */
public class BeanUtils {

	//公共部分
    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 RTN_2 = RT_1+"\r\t\n";
    private static final String RTN_1 = "\r\t\n";
    private static final String R_1 = "\r";
    private static final String T_1 = "\t";
    
    
    //注释部分
    private static final String ANNOTATION_AUTHOR_PARAMTER = "@author ";
    private static final String ANNOTATION_AUTHOR_NAME = "ershuai";
    private static final String ANNOTATION_AUTHOR = ANNOTATION_AUTHOR_PARAMTER + ANNOTATION_AUTHOR_NAME;
    private static final String ANNOTATION_DATE = "@date ";
    private static final String ANNOTATION = "/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR +RT_1+BLANK_1+"*"+BLANK_1+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1;
    
    
    //文件 地址
    //private static final String BEAN_PATH = "com/washcar/crm/entity/orm";
    private static final String DAO_PATH = "main/java/com/washcar/crm/dao/ifaces";
    private static final String DAO_IMPL_PATH = "main/java/com/washcar/crm/dao/impl";
    private static final String SERVICE_PATH = "main/java/com/washcar/crm/services/ifaces";
    private static final String SERVICE_IMPL_PATH = "main/java/com/washcar/crm/services/impl";

    
    
    //包名
    private static final String BASE_DAO_URL = "com.washcar.crm.dao";
    private static final String BEAN_URL = "com.washcar.crm.entity.orm";
    private static final String DAO_URL = "com.washcar.crm.dao.ifaces";
    private static final String DAO_IMPL_URL = "com.washcar.crm.dao.impl";
    private static final String BASE_SERVICE_URL = "com.washcar.crm.services";
    private static final String SERVICE_URL = "com.washcar.crm.services.ifaces";
    private static final String SERVICE_IMPL_URL = "com.washcar.crm.services.impl";

    private static final String COMPONET = "org.springframework.stereotype.Component";
    private static final String AUTOWIRED = "org.springframework.beans.factory.annotation.Autowired";
    
    //基本类名称
    private static final String BASE_BEGIN_TAG = "I";
    private static final String BASE_END_TAG = "Entity";
    private static final String BASE_DAO_NAME = "IWashDAO";
    private static final String BASE_SERVICE_NAME = "IWashService";
    private static final String BASE_DB_DAO_NAME = "AbstractRelationalDBDAO";
    private static final String BASE_ABSTRACTSERVICE = "AbstractService";
    private static final String BASE_OVERRIDE = "@Override";
    private static final String BASE_AUTOWIRED = "@Autowired";
    
    
    /**
     * 创建bean的Dao<br>
     * 
     * @param c
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
	public void createBeanDao(Class c) throws Exception {
        String cName = c.getName();
        String fileName = System.getProperty("user.dir") + "/src/" + DAO_PATH
                + "/" + BASE_BEGIN_TAG + getLastChar(cName) + "DAO.java";
        File f = new File(fileName);
        FileWriter fw = new FileWriter(f);
        fw.write("package "+DAO_URL+";"+RT_2+"import "+BASE_DAO_URL+"."+BASE_DAO_NAME+";"+RT_1+"import "+BEAN_URL+"."+getLastCharForEntity(cName)+";"
        		+RT_2+ANNOTATION+"public interface " + 
        		BASE_BEGIN_TAG + getLastChar(cName) + "DAO extends "+BASE_DAO_NAME+"<" + getLastChar(cName) + BASE_END_TAG + "> {"+RTN_2+"}");
        fw.flush();
        fw.close();
        showInfo(fileName);
    }

    /**
     * 创建bean的Dao的实现类
     * @param c
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
	public void createBeanDaoImpl(Class c) throws Exception{
        String cName = c.getName();
        String fileName = System.getProperty("user.dir") + "/src/" + DAO_IMPL_PATH
                + "/" + getLastChar(cName) + "DAOImpl.java";
        File f = new File(fileName);
        FileWriter fw = new FileWriter(f);
        fw.write("package "+DAO_IMPL_URL+";"+RT_2+
        		"import "+COMPONET+";"+RT_2+
        		"import "+DAO_URL +"." +BASE_BEGIN_TAG + getLastChar(cName)+"DAO;"+RT_2+
        		"import "+BASE_DAO_URL+"."+BASE_DB_DAO_NAME+";"+RT_1+
        		"import "+BEAN_URL+"."+getLastCharForEntity(cName)+";"+
        		RT_2+ANNOTATION+
        		"@Component(value=\""+getLastChar(cName)+"DAOImpl\")"+RT_1+
        		"public class " + 
                getLastChar(cName) + "DAOImpl extends "+BASE_DB_DAO_NAME+"<" + 
                getLastCharForEntity(cName) + "> implements " + BASE_BEGIN_TAG + getLastChar(cName)+"DAO {"+
                RTN_1+T_1+BASE_OVERRIDE+R_1+T_1+
                "protected Class<"+getLastCharForEntity(cName)+"> getEntityClass() {"+R_1+T_1+T_1+"return "+getLastCharForEntity(cName)+".class;"+R_1+T_1+"}"+
                R_1+
                "}"
                );
        fw.flush();
        fw.close();
        showInfo(fileName);
    }
    
    
    
    /**
     * 创建bean的service
     * @param c
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
	public void createBeanService(Class c) throws Exception{
        String cName = c.getName();
        String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_PATH
                + "/" + BASE_BEGIN_TAG + getLastChar(cName) + "Service.java";
        File f = new File(fileName);
        FileWriter fw = new FileWriter(f);
        fw.write("package "+SERVICE_URL+";"+RT_2+"import "+BASE_SERVICE_URL+"."+BASE_SERVICE_NAME+";"+RT_2+
        		ANNOTATION+"public interface " + 
        		BASE_BEGIN_TAG + getLastChar(cName) + "Service extends "+BASE_SERVICE_NAME+" {"+RTN_2+"}");
        fw.flush();
        fw.close();
        showInfo(fileName);
    }
    
    /**
     * 创建bean的service的实现类
     * @param c
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
	public void createBeanServiceImp(Class c) throws Exception{
        String cName = c.getName();
        String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_IMPL_PATH 
                + "/" +getLastChar(cName)+"ServiceImp.java";
        File f = new File(fileName);
        FileWriter fw = new FileWriter(f);
        fw.write("package "+SERVICE_IMPL_URL+";"+RT_2+
        		"import "+COMPONET+";"+RT_1+"import "+AUTOWIRED+";"+RT_2+
        		"import "+DAO_URL +"." +BASE_BEGIN_TAG + getLastChar(cName)+"DAO;"+RT_1+
        		"import "+BEAN_URL+"."+getLastCharForEntity(cName)+";"+RT_1+
        		"import "+BASE_SERVICE_URL+"."+BASE_ABSTRACTSERVICE+";"+RT_1+
        		"import "+SERVICE_URL+"."+BASE_BEGIN_TAG+getLastChar(cName)+"Service;"+
        		
        		RT_2+ANNOTATION+
        		"@Component(value=\""+getLastChar(cName)+"ServiceImp\")"+RT_1+
        		"public class " 
                + getLastChar(cName) + "ServiceImp extends "+BASE_ABSTRACTSERVICE+
                "<"+ getLastCharForEntity(cName) + ", " + BASE_BEGIN_TAG + getLastChar(cName) + "DAO>"+
                " implements "+BASE_BEGIN_TAG+getLastChar(cName)+"Service {"+
                RT_2+T_1+
                BASE_AUTOWIRED+RT_1+T_1+
                "private "+BASE_BEGIN_TAG+getLastChar(cName)+"DAO "+getLowercaseChar(getLastChar(cName))+"DAO;"+
                
                RT_2+T_1+BASE_OVERRIDE+RT_1+T_1+
                "protected " +BASE_BEGIN_TAG + getLastChar(cName)+"DAO getBaseDAO() {"+RT_1+T_1+T_1+
                	"return "+getLowercaseChar(getLastChar(cName))+"DAO;"+RT_1+T_1+
                "}"+RT_1+
                "}");
        fw.flush();
        fw.close();
        showInfo(fileName);
    }

    /**
     * 获取路径的最后面字符串<br>
     * 如:<br>
     *     <code>str = "com.washcar.crm.entity.orm.User"</code><br>
     *     <code> return "User";<code>
     * @param str
     * @return
     */
    public String getLastChar(String str) {
        if ((str != null) && (str.length() > 0)) {
            int dot = str.lastIndexOf('.');
            if ((dot > -1) && (dot < (str.length() - 1))) {
            	str = str.substring(dot + 1);
            }
        }
        return str.replace("Entity", "");
    }

    /**
     * 获取路径的最后面字符串<br>
     * 如:<br>
     *     <code>str = "com.washcar.crm.entity.orm.User"</code><br>
     *     <code> return "User";<code>
     * @param str
     * @return
     */
    public String getLastCharForEntity(String str) {
        if ((str != null) && (str.length() > 0)) {
            int dot = str.lastIndexOf('.');
            if ((dot > -1) && (dot < (str.length() - 1))) {
            	str = str.substring(dot + 1);
            }
        }
        return str;
    }
    
    /**
     * 把第一个字母变为小写<br>
     * 如:<br>
     *     <code>str = "UserDao";</code><br>
     *     <code>return "userDao";</code>
     * @param str
     * @return
     */
    public String getLowercaseChar(String str){
        return str.substring(0,1).toLowerCase()+str.substring(1);
    }

    /**
     * 显示信息
     * @param info
     */
    public void showInfo(String info){
        System.out.println("创建文件:"+ info+ "成功!");
    }
    
    /**
     * 获取系统时间
     * @return
     */
    public static String getDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值