Spring dao 和Service 生成文件类

package com.annotationtodaotoservice;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @see  生成dao 和service 文件和文件夹
 * @author liuqing
 * @since  2011-5-1
 * @version 1.0
 */
public class DaoServiceImpl {

    private Logger log = Logger.getLogger(DaoServiceImpl.class.getName());

    private String entityDir;

    public static void main(String args[]) throws Exception {
    	
        DaoServiceImpl dao = new DaoServiceImpl();
       
        dao.entityDir = "D:\\cache\\entity";
        dao.packageToFile("dao.impl");
        dao.packageToFile("service.impl");
        Map<String,List<File>> fileMap = dao.entityFileToDaoFile();
        List<File> daoFile = fileMap.get("dao");
        List<File> daoImplFile = fileMap.get("daoImpl");
        List<File> serviceFile = fileMap.get("service");
        List<File> serviceFileImpl = fileMap.get("serviceImpl");
        OutputStream out = null;
        dao.outFileDaoAndService(daoFile,out,1);
        dao.outFileDaoAndService(daoImplFile,out,2);
        dao.outFileDaoAndService(serviceFile,out,3);
        dao.outFileDaoAndService(serviceFileImpl,out,4);
        if (out != null) {
        	out.close();
        }
        
    }

    public void outFileDaoAndService(List<File> files,OutputStream out,int type)
            throws FileNotFoundException {
        for (File fileName:files) {
            out = new FileOutputStream(fileName);
            this.print(out, type, fileName.getName());
        }
    }
    
    public String toLowerFirst(String str) {
    	if (str != null && !"".equals(str)) {
    		char ch[] = str.toCharArray();
    		if (ch.length > 0) {
    			if (ch[0] < 'a') {
    				ch[0] = (char) (ch[0] + 32);
    			}
    		}
    		return (new String(ch));
    	}
    	return str;
    }
    
    public String toUpFirst(String str) {
    	if (str != null && !"".equals(str)) {
    		char ch[] = str.toCharArray();
    		if (ch.length > 0) {
    			if (ch[0] > 'Z') {
    				ch[0] = (char) (ch[0] - 32);
    			}
    		}
    		return (new String(ch));
    	}
    	return str;
    }

    /**
     *
     * @param out
     * @param typeInfo 1:为IDao 2 DaoImpl 3 IService 4 ServiceImpl
     */
    public void print(OutputStream output,int typeInfo,String clazzName) {

        PrintWriter out = new PrintWriter(output);
        if (typeInfo == 1) {
            this.fileToDao(this.javaFileToClassName(clazzName), out, " com.bs.phs.dao.jkjy");
        }
        else if (typeInfo == 2) {
            this.fileToDaoImpl(this.javaFileToClassName(clazzName), out, " com.bs.phs.dao.jkjy.impl");
        }
        else if (typeInfo == 3) {
            this.fileToService(this.javaFileToClassName(clazzName), out, " com.bs.phs.service.jkjy");
        }
        else {
            this.fileToServiceImpl(this.javaFileToClassName(clazzName), out, " com.bs.phs.service.jkjy.impl");
        }
        out.flush();
        
    }

    public void fileToDao(String clazzName,PrintWriter out,String packageInfo) {
        out.println("package " + packageInfo + ";");
        out.println("");
      //  out.println("import " + packageInfo +"." + clazzName + ";");
        this.toLogger(clazzName, out, packageInfo);
        out.println("public interface " + clazzName +
                " extends IBaseDao<" + this.daoService(clazzName,1) + "> {");
        out.println();
        out.println("}");
        out.flush();
    }

    /**
     * 文件头部信息日志
     * @param clazzName
     * @param out
     * @param packageInfo
     */
    public void toLogger(String clazzName,PrintWriter out,String packageInfo) {
        out.println();
        out.println("/**");
        out.println(" * @since " + packageInfo + "." + clazzName + ".java");
        out.println(" * @see  " +
                clazzName + " create datetime:" + dateToString()
                + "");
        out.println(" * @author liuqing");
        out.println(" * @version 1.0");
        out.println(" */");
    }

    public void fileToDaoImpl(String clazzName,PrintWriter out,String packageInfo) {
        out.println("package " + packageInfo + ";");
        out.println("");
        out.println("import " + packageInfo +"." + clazzName + ";");
        this.toLogger(clazzName, out, packageInfo);
        out.println("@Repository(\"" +
        		this.toLowerFirst(clazzName).substring(0, clazzName.length() - 4) +
        		"\")");
        out.println("public class " + clazzName +
                " extends BaseDaoImpl<" + this.daoService(clazzName,2) + "> implements " +
                "I" + this.daoService(clazzName, 2) +
                "Dao {");
        out.println();
        out.println("}");
        out.flush();
    }

    public void fileToService(String clazzName,PrintWriter out,String packageInfo) {
        out.println("package " + packageInfo + ";");
        out.println("");
        out.println("import " + packageInfo +"." + clazzName + ";");
        this.toLogger(clazzName, out, packageInfo);
        out.println("public interface " + clazzName +
                " extends IBaseService<" + this.daoService(clazzName,3) + "> {");
        out.println();
        out.println("}");
        out.flush();
    }

    /**
     * UsbServiceImpl
     * @param clazzName
     * @param out
     * @param packageInfo
     */
    public void fileToServiceImpl(String clazzName,PrintWriter out,String packageInfo) {
        out.println("package " + packageInfo + ";");
        out.println("");
        out.println("import " + packageInfo +"." + clazzName + ";");
        this.toLogger(clazzName, out, packageInfo);
        out.println("@Transactional");
        out.println("@Service(\"" + this.toLowerFirst(clazzName).substring(0, clazzName.length() - 4) + "\")");
        out.println("public class " + clazzName +
                " extends BaseServiceImpl<" + this.daoService(clazzName,4) + "> implements " +
                "I" + this.daoService(clazzName,4) + "Service" +
                " {");
        out.println("");
        String annotationName = clazzName.substring(0, clazzName.length() - 4);
        out.println("");
        String daoName = annotationName.substring(0, annotationName.length() - 7) + "Dao";
        out.println("@Resource(name=\"" + this.toLowerFirst(daoName) + "\")");
        out.println("private I" + this.toUpFirst(daoName) + " " + this.toLowerFirst(daoName) +";");
        out.println("");
        out.println("@Override");
        out.println("public IBaseDao<" +
        		this.toUpFirst(daoName.substring(0, daoName.length() - 3)) +
        		"> getTargetDao() {");
        out.println("    return " + this.toLowerFirst(daoName) + ";");
        out.println("}");
        out.println("");
        
        out.println("}");
        out.flush();

    }
    
    /**
     *  根据文件生成 ClassName
     * @param inclassName
     * @param typeInfo 1:为IDao 2 DaoImpl 3 IService 4 ServiceImpl
     * @return
     */
    public String daoService(String inclassName,int typeInfo) {
        int classLength = inclassName.length();
        if (typeInfo == 1) {
            return inclassName.substring(1, classLength - 3);
        }
        else if (typeInfo == 2){
            return inclassName.substring(0, classLength - 7);
        }
        else if (typeInfo == 3){
            return inclassName.substring(1, classLength - 7);
        }
        else {
            return inclassName.substring(0, classLength - 11);
        }
    }

    /**
     * 找到entityFile
     * @param covertDao
     * @param packageInfo
     * @throws FileNotFoundException
     */
    public File[] entityFileDir ()
            throws FileNotFoundException {
        List<File> files = new ArrayList<File>();
        File entityDirInfo = new File(entityDir);
        //新生成File类型
        File[] newFiles = new File[]{};
        File[] entityFiles = entityDirInfo.listFiles();
        entityDirInfo.mkdirs();
        	for (File f:entityFiles) {
        		if (f.getName().endsWith(".java")) {
        			files.add(f);
        			//System.out.println("---" + f.getName());
        		}
        	}
       return files.toArray(newFiles);
    }

    /**
     * 通过实体文件生成dao 和 service 文件
     * @param entityDir
     * @param typeInfo
     */
    public Map<String,List<File>> entityFileToDaoFile()
            throws FileNotFoundException {
        Map<String,List<File>> fileMap = new HashMap<String,List<File>>();
        List<File> dao = new ArrayList<File>();
        List<File> daoImpl = new ArrayList<File>();
        List<File> service = new ArrayList<File>();
        List<File> serviceImpl = new ArrayList<File>();
        File[] entityFiles = this.entityFileDir();
        for (File enFile:entityFiles) {
            int pos = enFile.getName().indexOf(".");
            String fileName = enFile.getName();
            String daoFileName = "I" + fileName.substring(0, pos) + "Dao.java";
            String daoImplFileName = fileName.substring(0, pos) + "DaoImpl.java";
            dao.add(new File(this.basePath() + File.separator +
                    "dao" + File.separator + daoFileName));
            daoImpl.add(new File(this.basePath() +
                    File.separator + "dao" + File.separator + "impl" +
                    File.separator + daoImplFileName));
            String serviceFileName = "I" + fileName.substring(0, pos) + "Service.java";
            String serviceImplFileName = fileName.substring(0, pos) + "ServiceImpl.java";
            service.add(new File(this.basePath() + "" +
                    File.separator + "service" + File.separator +serviceFileName));
            serviceImpl.add(new File(this.basePath() +
                    File.separator + "service" + File.separator + "impl" +
                    File.separator + serviceImplFileName));
        }
        fileMap.put("dao", dao);
        fileMap.put("daoImpl", daoImpl);
        fileMap.put("service", service);
        fileMap.put("serviceImpl", serviceImpl);
        return fileMap;
    }

    /**
     * 返加文件夹操作权限
     * @param directoryFile
     * @param packageInfo 创建文件夹
     * @return File
     */
    public File packageToFile(String packageInfo) {
        StringBuffer strBuff = new StringBuffer(basePath());
        strBuff.append(File.separator + packageInfo.replace(".", File.separator));
        File file = new File(strBuff.toString());
        file.mkdirs();
        return file;
    }

    /**
     * 基础信息
     * @return String
     */
    public String basePath(){
        int i = this.entityDir.lastIndexOf(File.separator);
        System.out.println("");
        System.out.println(this.entityDir.substring(0, i));
        String basePath = this.entityDir.substring(0, i);
        log.info(basePath);
        return basePath;
    }

    public String javaFileToClassName(String javaFileName) {
        int i = javaFileName.indexOf(".");
        return javaFileName.substring(0, i);
    }

    public String dateToString() {
        SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return simp.format(new Date());
    }

    public String getEntityDir() {
        return entityDir;
    }

    public void setEntityDir(String entityDir) {
        this.entityDir = entityDir;
    }

}

spring2.5之前片本

package com.annotationtodaotoservice;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @see 生成Spring bean 文件
 * @author liuqing
 */
public class SpringBeanFileImpl extends DaoServiceImpl {

    private String packeInformation;

    private String outFilePath;


    public static void main(String args[]) throws FileNotFoundException {
        SpringBeanFileImpl imp =
                new SpringBeanFileImpl("com.hd.digitalxcampus","f:\\beaninfo");
        imp.setEntityDir("E:\\liuqing\\workspace\\phs\\workspace\\phs\\src\\com\bs\\phs\\domain\\report");
        imp.toSpringBeanFile();
    }

    public SpringBeanFileImpl() {
    }

    public SpringBeanFileImpl(String packeInformation, String outFilePath) {
        this.packeInformation = packeInformation;
        this.outFilePath = outFilePath;
    }

    public void toSpringBeanFile() throws FileNotFoundException {
        System.out.println("---" + this.outFilePath);
        OutputStream outputDao = new FileOutputStream(this.outFilePath + File.separator + "spring-dao.xml");
        OutputStream outputService = new FileOutputStream(this.outFilePath + File.separator + "spring-service.xml");
        PrintWriter outDao = new PrintWriter(outputDao);
        PrintWriter outService = new PrintWriter(outputService);
        for (String clazzName:this.getClazzName()) {
            bean(clazzName,outDao,outService);
        }
        if (outputDao != null) {
            try {
                outputDao.close();
            }
            catch (IOException ex) {
            }
        }
        if (outputService != null) {
            try {
                outputService.close();
            }
            catch (IOException ex) {
            }
        }

    }

    public List<String> getClazzName() throws FileNotFoundException {
        List<String> classNames = new ArrayList<String>();
        File entityFiles[] = this.entityFileDir();
        for (File en:entityFiles) {
            classNames.add(this.javaFileToClassName(en.getName()));
        }
        return classNames;
    }

    public void bean(String clazzName,PrintWriter outDao
            ,PrintWriter serviceDao) {
        this.toIdInfo(clazzName, 1);
        outDao.println("<bean id=\"" + this.firstLower(clazzName) + "Dao" +
                "\" parent=\"adminInfoDao\" class=\"" + this.daoClassName(clazzName) +
                "\"> ");
        outDao.println("</bean>");

        serviceDao.println("<bean id=\"" + this.firstLower(clazzName) + "Service" +
                "\" class=\"" + this.serviceClassName(clazzName) +
                "\"> ");
//        <property name="baseDao" ref="foodTypeDao" />
        serviceDao.println("    <property name=\"baseDao\" ref=\"" +
                this.firstLower(clazzName) + "Dao" +
                "\" />");
        serviceDao.println("</bean>");
        outDao.flush();
        serviceDao.flush();
    }
    /**
     * 
     * @param clazzName
     * @param type 1: dao; 2: service;
     * @return
     */
    public String toIdInfo(String clazzName,int type) {
        if (type == 1) {
            return this.packeInformation + ".dao.impl." + clazzName + "DaoImpl";
        }
        else if (type == 2) {
            return this.packeInformation + ".service.impl." + clazzName + "ServiceImpl";
        }
        else {
            return null;
        }
    }
    /**
     *
     * @param clazzName
     * @return
     */
    public String daoClassName(String clazzName) {
        return this.toIdInfo(clazzName, 1);
    }

    /**
     * 
     * @param clazzName
     * @return
     */
    public String serviceClassName(String clazzName) {
         return this.toIdInfo(clazzName, 2);
    }

    /**
     * 
     * @param clazzName
     * @return String
     */
    public String firstLower(String clazzName) {
        char ch[] = clazzName.toCharArray();
        if (ch[0] <= ((char)'z') && ch[0] >= ((char)'A')) {
            ch[0] = (char)(ch[0] + 32);
        }
        return new String(ch);
    }

    public String getPackeInformation() {
        return packeInformation;
    }

    public void setPackeInformation(String packeInformation) {
        this.packeInformation = packeInformation;
    }

    public String getOutFilePath() {
        return outFilePath;
    }

    public void setOutFilePath(String outFilePath) {
        this.outFilePath = outFilePath;
    }

}

猜你喜欢

转载自mianhuaman.iteye.com/blog/1404120