您好,欢迎来到爱够旅游网。
搜索
您的当前位置:首页实体类,mapper,xml,sql自动生成器

实体类,mapper,xml,sql自动生成器

来源:爱够旅游网

实体类,mapper,xml,sql自动生成器

package com.chne.chne_totalbackstage.privateUtils;


import com.chne.chne_base.entity.*;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
 * add by LSH
 * 2016-11-7
 * fun 根据实体类生成对应的目标文件【SQL & MAPPER & DAO】
 * 反复执行将会覆盖原文件
 */
public class SM_MODEL_UTIL {

    /**
     * xmlPath xml文件存放位置
     * sqlPath sql文件存放位置
     * daoPath dao文件存放位置
     * Class T 需要生成的对应的类
     * @param args
     * @throws Exception
     * @TIP 反复执行将会覆盖原文件
     */
    public static void main(String args[]) throws Exception{

        Class T = TTask.class;
        String xmlPath = "D:\\chne_cloud\\chne_common\\chne_base\\src\\main\\resources\\mapper\\";
        String sqlPath = "D:\\chne_cloud\\chne_common\\chne_base\\src\\main\\resources\\sql\\";
        String daoPath = "D:\\chne_cloud\\chne_common\\chne_base\\src\\main\\java\\com\\chne\\chne_base\\mapper\\";
        fuc(T,xmlPath,sqlPath,daoPath);
    }





    public static void fuc(Class T,String xmlPath,String sqlPath,String daoPath) throws Exception{
        //获得类的名字
        String entityName = T.getSimpleName();

        //获得表的名字
        String tableName = decamelize(entityName);
        tableName = tableName.substring(1,tableName.length());


        String word = "";
        Field[]  f = T.getDeclaredFields(); //获取该类的字段(public, protected, default (package) access, and private)
        for(Field ff:f)
        {
            if(ff.getType() != List.class){
                if("".equals(word)){
                    word = ff.getName();
                }else{
                    word = word + "," + ff.getName();
                }
            }

        }
        //生成XML文件
        makeXML(entityName+"Mapper",word,tableName,xmlPath);

        //生成sql文件
        makeSQL(tableName,f,sqlPath);

        //生成dao文件
        makeDao(entityName,daoPath);
    }


    /**
     * 一键生成Mapper
     * 文件名 【字短名称】表名称
     * fileName words table
     * @throws Exception
     */

    public static void makeXML(String fileName,String words,String table,String filePath) throws Exception{

        //先创建一个文件
        String entity = fileName.substring(0,fileName.indexOf("Mapper"));
        String sss = "id,"+ words + ",createBy,createByName,createTime,updateBy,updateByName,updateTime,isDeleted";
        String[]  list = sss.split(",");
        File file = new File(filePath + fileName + ".xml");
        if(!file.exists())
            file.createNewFile();
        FileWriter fw = new FileWriter(file);

        //头部分
        fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n");
        fw.write("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\n");
        fw.write("<mapper namespace=\"com.chne.chne_base.mapper." + fileName + "\">\n");
        fw.write("    <resultMap id=\"BaseResultMap\" type=\"com.chne.chne_base.entity." + entity + "\">\n");
        fw.write("        <id column=\"id\" property=\"id\"/>\n");
        for(String temp : list){
            if("id".equals(temp))
                continue;
            fw.write("       <result column=\"" + temp + "\" property=\"" + temp + "\"/>\n");
        }
        fw.write("    </resultMap>\n");
        fw.write(" \n");


        //Base_Column_List部分
        fw.write("   <sql id=\"Base_Column_List\">\n");
        fw.write("        "+sss+"\n");
        fw.write("   </sql>\n");
        fw.write(" \n");


        //queryOne部分
        fw.write("    <select id=\"queryOne\" resultMap=\"BaseResultMap\" parameterType=\"com.chne.chne_base.entity." + entity + "\">\n");
        fw.write("        select\n");
        fw.write("        <include refid=\"Base_Column_List\"/>\n");
        fw.write("        from "+ table + " where isDeleted=0\n");
        for(String temp : list){
            fw.write("        <if test=\"" + temp + " != null and " + temp + " != '' \">\n");
            fw.write("            and " + temp + " = #{" + temp + "}\n");
            fw.write("        </if>\n");
        }
        fw.write("    </select>\n");
        fw.write(" \n");


        //queryPage部分
        fw.write("    <select id=\"queryPage\" resultMap=\"BaseResultMap\" parameterType=\"com.chne.chne_base.entity." + entity + "\">\n");
        fw.write("        select\n");
        fw.write("        <include refid=\"Base_Column_List\"/>\n");
        fw.write("        from "+ table + " where isDeleted=0\n");
        for(String temp : list){
            fw.write("        <if test=\"" + temp + " != null and " + temp + " != '' \">\n");
            fw.write("            and " + temp + " = #{" + temp + "}\n");
            fw.write("        </if>\n");
        }
        fw.write("        order by createTime desc\n");
        fw.write("        limit #{pageIndex}, #{pageSize}\n");
        fw.write("    </select>\n");
        fw.write(" \n");


        //countTotal部分
        fw.write("    <select id=\"countTotal\" resultType=\"long\" parameterType=\"com.chne.chne_base.entity." + entity + "\">\n");
        fw.write("        select\n");
        fw.write("        count(id)\n");
        fw.write("        from "+ table + " where isDeleted=0\n");
        for(String temp : list){
            fw.write("        <if test=\"" + temp + " != null and " + temp + " != '' \">\n");
            fw.write("            and " + temp + " = #{" + temp + "}\n");
            fw.write("        </if>\n");
        }
        fw.write("    </select>\n");
        fw.write(" \n");



        //queryList部分
        fw.write("    <select id=\"queryList\" resultMap=\"BaseResultMap\" parameterType=\"com.chne.chne_base.entity." + entity + "\">\n");
        fw.write("        select\n");
        fw.write("        <include refid=\"Base_Column_List\"/>\n");
        fw.write("        from "+ table + " where isDeleted=0\n");
        for(String temp : list){
            fw.write("        <if test=\"" + temp + " != null and " + temp + " != '' \">\n");
            fw.write("            and " + temp + " = #{" + temp + "}\n");
            fw.write("        </if>\n");
        }
        fw.write("        order by createTime desc\n");
        fw.write("    </select>\n");
        fw.write(" \n");


        //deleteByModel部分
        fw.write("    <update id=\"deleteByModel\" parameterType=\"com.chne.chne_base.entity." + entity + "\">\n");
        fw.write("           update " + table + " set isDeleted = 1 where id = #{id}\n");
        fw.write("    </update>\n");
        fw.write(" \n");

        //insertByModel部分
        fw.write("    <insert id=\"insertByModel\" parameterType=\"com.chne.chne_base.entity."+entity + "\">\n");
        fw.write("        insert into " + table +"\n");
        fw.write("        <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
        for(String temp : list){
            fw.write("            <if test=\"" + temp + " != null\">\n");
            fw.write("                "+temp +",\n");
            fw.write("            </if>\n");
        }
        fw.write("        </trim>\n");
        fw.write("        <trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\">\n");
        for(String temp : list){
            fw.write("            <if test=\"" + temp + " != null\">\n");
            fw.write("                #{"+temp +"},\n");
            fw.write("            </if>\n");
        }
        fw.write("        </trim>\n");
        fw.write("    </insert>\n");
        fw.write(" \n");




        //updateByModel部分
        fw.write("    <update id=\"updateByModel\" parameterType=\"com.chne.chne_base.entity."+entity + "\">\n");
        fw.write("        update " + table + "\n");
        fw.write("        <set>\n");
        for(String temp : list){
            fw.write("            <if test=\"" + temp + " != null\">\n");
            fw.write("                "+ temp + " = #{" + temp + "},\n");
            fw.write("            </if>\n");
        }
        fw.write("        </set>\n");
        fw.write("        where id = #{id}\n");
        fw.write("    </update>\n");


        //尾步
        fw.write(" \n");
        fw.write(" \n");
        fw.write("</mapper>\n");


        fw.flush();
        fw.close();
    }


    /**
     * 一键生成SQL文件
     * @param tableName
     * @param f
     * @param sqlPath
     */
    private static void makeSQL(String tableName, Field[] f, String sqlPath) throws Exception{
        File file = new File(sqlPath + tableName + ".sql");
        if(!file.exists())
            file.createNewFile();
        FileWriter fw = new FileWriter(file);

        fw.write("CREATE TABLE `"+tableName+"` (\n");
        fw.write("  `id` bigint(10) NOT NULL AUTO_INCREMENT,\n");

        for(Field ff : f){
            String name = ff.getName();
            Class type = ff.getType();
            if(type == String.class){
                fw.write("  `"+name+"` varchar(100) DEFAULT NULL,\n");
            }
            if(type == Date.class){
                fw.write("  `"+name+"` datetime DEFAULT NULL,\n");
            }
            if(type == Long.class){
                fw.write("  `"+name+"` bigint(10) DEFAULT NULL,\n");
            }
            if(type == Integer.class){
                fw.write("  `"+name+"` int(10) DEFAULT NULL,\n");
            }
            if(type == BigDecimal.class){
                fw.write("  `"+name+"` decimal(10,2) DEFAULT NULL,\n");
            }
        }


        fw.write("  `createTime` datetime DEFAULT NULL,\n");
        fw.write("  `createBy` varchar(50) DEFAULT NULL,\n");
        fw.write("  `createByName` varchar(50) DEFAULT NULL,\n");
        fw.write("  `updateTime` datetime DEFAULT NULL,\n");
        fw.write("  `updateBy` varchar(50) DEFAULT NULL,\n");
        fw.write("  `updateByName` varchar(50) DEFAULT NULL,\n");
        fw.write("  `isDeleted` tinyint(2) DEFAULT '0',\n");
        fw.write("  PRIMARY KEY (`id`)\n");
        fw.write(") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n");
        fw.flush();
        fw.close();

    }

    /**
     * 一键生成DAO文件
     * @param fileName
     * @param daoPath
     */
    private static void makeDao(String fileName, String daoPath) throws Exception{
        File file = new File(daoPath + fileName + "Mapper.java");
        if(!file.exists())
            file.createNewFile();
        FileWriter fw = new FileWriter(file);

        fw.write("package com.chne.chne_base.mapper;\n");
        fw.write("import com.chne.chne_base.entity."+fileName+";\n");
        fw.write("\n");
//        public interface TRoleMapper extends BaseMapper<TRole> {
        fw.write("public interface "+fileName+"Mapper extends BaseMapper<"+fileName+"> {\n");
        fw.write("}\n");
        fw.write("\n");
        fw.flush();
        fw.close();

    }

    /**
     * 获得表名
     * @param camelCaseStr
     * @return
     */
    public static String decamelize(String camelCaseStr){
        return isBlank(camelCaseStr) ? camelCaseStr : camelCaseStr.replaceAll("[A-Z]", "_$0").toLowerCase();
    }

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }



}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务