工具类---

import com.zbjk.lcs.oap.common.constants.ResponseCode;
import com.zbjk.lcs.oap.common.exception.BusinessException;
import com.zbjk.lcs.oap.common.exception.OapParamException;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@SuppressWarnings("rawtypes")
public class CommonUtils {
    private static ThreadLocal<String> param = new ThreadLocal<>();


    /**
     * 判断一个对象中,除了id、addTime、updateTime、delFlag以及传入的属性名以外的属性是否存在空值
     * 如果存在一个空值,抛出异常
     * 支持List遍历、父类遍历
     * @return
     */
    public static void isParamsExceNull(Object obj, String... argsName) {
        List<String> args = getArgs(argsName);
        try {
            for (Field f : obj.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                checkParamsExceNull(args, f, obj, argsName);
            }
            //如果含有父类,且父类不为Object,往上遍历父类
            if (!obj.getClass().getSuperclass().equals(Object.class)) {
                isParamsExceNull(obj, obj.getClass().getSuperclass(), argsName);
            }
        } catch (BusinessException | IllegalAccessException e) {
            String errorParam = param.get();
            param.remove();
            throw new OapParamException(ResponseCode.CODE_PARAMS_ERROR.getCode(), "参数:{}不能为空", errorParam);
        }
    }

    /**
     * 判断一个对象中,除了id、addTime、updateTime、delFlag以及传入的属性名以外的属性是否全部为空值
     * 支持对象中含有List
     * 支持List遍历、父类遍历
     * 全部为空抛出异常
     * @return
     */
    public static void isParamsExceNotAllNull(Object obj, String ... argsName) {
        List<String> args = getArgs(argsName);
        try {
            for (Field f : obj.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                if (args.contains(f.getName())) {
                    continue;
                }
                if (checkParamsExceNotAllNull(args, f, obj, argsName) == 0) {
                    return;
                }
            }
            //如果含有父类,且父类不为Object,往上遍历父类
            if (!obj.getClass().getSuperclass().equals(Object.class)) {
                isParamsExceNotAllNull(obj, obj.getClass().getSuperclass(), argsName);
                return;
            }
        } catch (Exception e) {
            throw new OapParamException(ResponseCode.CODE_PARAMS_ERROR.getCode(), args + " 以外的参数不能全部为空");
        }
        throw new OapParamException(ResponseCode.CODE_PARAMS_ERROR.getCode(), args + " 以外的参数不能全部为空");
    }

    /**
     * 判断一个对象中指定的参数集是否存在空值,存在抛出异常
     * 支持对象中含有List
     * 支持List遍历、父类遍历
     * @param obj
     * @param argsName
     */
    public static void isParamsNotNull(Object obj, String ... argsName) {
        List<String> args = Arrays.asList(argsName);
        try {
            for (Field f : obj.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                checkParamsNotNull(args, f, obj, argsName);
            }
            //如果含有父类,且父类不为Object,往上遍历父类
            if (!obj.getClass().getSuperclass().equals(Object.class)) {
                isParamsNotNull(obj, obj.getClass().getSuperclass(), argsName);
            }
        } catch (Exception e) {
            String errorParam = param.get();
            param.remove();
            throw new OapParamException(ResponseCode.CODE_PARAMS_ERROR.getCode(), "参数:{} 不能为空", errorParam);
        }
    }

    private static void isParamsNotNull(Object obj, Class<?> clazz, String ... argsName) {
        List<String> args = Arrays.asList(argsName);
        try {
            for (Field f : clazz.getDeclaredFields()) {
                f.setAccessible(true);
                checkParamsNotNull(args, f, obj, argsName);

            }
            //如果含有父类,且父类不为Object,往上遍历父类
            if (!clazz.getClass().getSuperclass().equals(Object.class)) {
                isParamsNotNull(obj, clazz.getClass().getSuperclass(), argsName);
            }
        } catch (Exception e) {
            throw new BusinessException(ResponseCode.CODE_PARAMS_ERROR);
        }
    }

    private static void checkParamsNotNull(List<String> args,
                                           Field f, Object obj, String ... argsName) throws IllegalAccessException {
        if (args.contains(f.getName())) {
            if (f.get(obj) == null || "".equals(f.get(obj))) {
                param.set(f.getName());
                throw new BusinessException(ResponseCode.CODE_PARAMS_ERROR);
            }
            //如果参数是List 遍历List中的参数
            if (f.getType().equals(List.class)) {
                for (Object o : (List)f.get(obj)) {
                    isParamsNotNull(o, argsName);
                }
            }
        }
    }

    private static List<String> getArgs(String[] argsName) {
        List<String> args = new ArrayList<>();
        args.add("id");
        args.add("addTime");
        args.add("updateTime");
        args.add("delFlag");
        args.addAll(Arrays.asList(argsName));
        return args;
    }
    private static void isParamsExceNotAllNull(Object obj, Class<?> clazz, String ... argsName) {
        List<String> args = getArgs(argsName);
        try {
            for (Field f : clazz.getDeclaredFields()) {
                f.setAccessible(true);
                if (args.contains(f.getName())) {
                    continue;
                }
                if (checkParamsExceNotAllNull(args, f, obj, argsName) == 0) {
                    return;
                }

            }
            //如果含有父类,且父类不为Object,往上遍历父类
            if (!clazz.getClass().getSuperclass().equals(Object.class)) {
                isParamsExceNotAllNull(obj, clazz.getClass().getSuperclass(), argsName);
                return;
            }
        } catch (Exception e) {
            throw new BusinessException(ResponseCode.CODE_PARAMS_ERROR);
        }
        throw new BusinessException(ResponseCode.CODE_PARAMS_ERROR);
    }

    private static int checkParamsExceNotAllNull(List<String> args,
                                                 Field f, Object obj, String ... argsName) throws IllegalAccessException {

        if (f.get(obj) != null && !"".equals(f.get(obj))) {
            if (f.getType().equals(List.class)) {
                for (Object o : (List) f.get(obj)) {
                    try {
                        isParamsExceNotAllNull(o, argsName);
                        return 0;
                    } catch (BusinessException e) {
                                /*FALLTHRU*/
                    }
                }
                //该List中全部都是null
                return 1;
            }
            return 0;
        }
        return 1;
    }

    private static void isParamsExceNull(Object obj, Class<?> clazz, String ... argsName) {
        List<String> args = getArgs(argsName);
        try {
            for (Field f : clazz.getDeclaredFields()) {
                f.setAccessible(true);
                checkParamsExceNull(args, f, obj, argsName);

            }
            //如果含有父类,且父类不为Object,往上遍历父类
            if (!clazz.getClass().getSuperclass().equals(Object.class)) {
                isParamsExceNull(obj, clazz.getClass().getSuperclass(), argsName);
            }
        } catch (Exception e) {
            throw new BusinessException(ResponseCode.CODE_PARAMS_ERROR);
        }
    }

	private static void checkParamsExceNull(List<String> args,
                                            Field f, Object obj, String... argsName) throws IllegalAccessException {
        if (args.contains(f.getName())) {
            return;
        }
        if (f.get(obj) == null || "".equals(f.get(obj))) {
            param.set(f.getName());
            throw new BusinessException(ResponseCode.CODE_PARAMS_ERROR);
        }
        //如果参数是List 遍历List中的参数
        if (f.getType().equals(List.class)) {
            for (Object o : (List)f.get(obj)) {
                isParamsExceNull(o, argsName);
            }
        }
    }
}


import java.io.Serializable;
import java.util.List;


import org.springframework.util.ObjectUtils;




public class PageModel<T> implements Serializable {


	private static final long serialVersionUID = 1L;
	
	public static final int DEFAULT_PAGE = 1;
	
	public static final int DEFAULT_PAGESIZE = 10;
	
	private int currentPage = DEFAULT_PAGE;// 当前页
	private int pageSize = DEFAULT_PAGESIZE;// 每页显示条数
	private int total;// 总记录数
	private transient List<T> rows;// 分页数据
	private int count; // 返回数据条数
	
	private int totalPage;// 总页数
	private int totalRecord;// 总记录数
	@SuppressWarnings("rawtypes")
	private transient List dataList;// 分页数据
	public int getTotalPage() {
		return totalPage;
	}


	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}


	public int getTotalRecord() {
		return totalRecord;
	}


	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}


	public List getDataList() {
		return dataList;
	}


	public void setDataList(List dataList) {
		this.dataList = dataList;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getTotal() {
		return total;
	}


	public void setTotal(int total) {
		this.total = total;
	}


	public List<T> getRows() {
		return rows;
	}


	public void setRows(List<T> rows) {
		this.rows = rows;
	}


	public int getCount() {
		if (!ObjectUtils.isEmpty(rows)) {
			return rows.size();
		}
		return count;
	}


	public void setCount(int count) {
		this.count = count;
	}


}


import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;


public class BeanMapConvertUtil {


        private static Logger logger = Logger.getLogger(BeanMapConvertUtil.class);
        public static  Map<String, Object> beanToMap(Object bean){
            if(bean == null){
                return null;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
                PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
                for (PropertyDescriptor property : propertyDescriptors) {
                    String key = property.getName();

                    // 过滤class属性
                    if (!key.equals("class")) {
                        // 得到property对应的getter方法
                        Method getter = property.getReadMethod();
                        Object value = getter.invoke(bean);
                        map.put(key, value);
                    }

                }
            } catch (Exception e) {
                System.out.println("transBean2Map Error " + e);
            }

            return map;
        }

        public static <T> T mapToBean(Map<String, Object> map, T bean){
            try {
                BeanUtils.populate(bean, map);
            } catch (IllegalAccessException | InvocationTargetException e) {
                logger.error("",e);
            }
            return bean;
        }
    }



import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  自动生成MyBatis的实体类、实体映射XML文件、Mapper
 *
 */
@SuppressWarnings("hiding")
public class EntityUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(EntityUtil.class);
    /**
     **********************************使用前必读*******************
     **
     ** 使用前请将moduleName更改为自己模块的名称即可(一般情况下与数据库名一致),其他无须改动。
     **
     ***********************************************************
     */

    private final String type_char = "char";

    private final String type_date = "date";

    private final String type_timestamp = "timestamp";

    private final String type_int = "int";

    private final String type_bigint = "bigint";

    private final String type_text = "text";

    private final String type_bit = "bit";

    private final String type_decimal = "decimal";

    private final String type_blob = "blob";


    private final String moduleName = "lcs.msm"; // 对应模块名称(根据自己模块做相应调整!!!务必修改^_^)

    private final String bean_path = "D:/entity_bean_lcs";

    private final String mapper_path = "D:/entity_mapper_lcs";

    private final String xml_path = "D:/entity_mapper_lcs/xml";

    private final String bean_package =  "com.zbjk." + moduleName + ".domain";
    private final String mapper_package = "com.zbjk." + moduleName + ".mapper";



    private final String driverName = "com.mysql.jdbc.Driver";

    private final String user = "msm-dev";

    private final String password = "joinboom";

    private final String url = "jdbc:mysql://dev-db.joinboom.net:3306/msm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true";

    private String tableName = null;

    private String beanName = null;
    private String lowerbeanName = null;

    private String mapperName = null;

    private Connection conn = null;


    private void init() throws ClassNotFoundException, SQLException {
        Class.forName(driverName);
        conn = DriverManager.getConnection(url, user, password);
    }


    /**
     *  获取所有的表
     *
     * @return
     * @throws SQLException
     */
    private List<String> getTables() throws SQLException {
        List<String> tables = new ArrayList<String>();
        PreparedStatement pstate = conn.prepareStatement("show tables");
        ResultSet results = pstate.executeQuery();
        while ( results.next() ) {
            String tableName = results.getString(1);
                      if ( tableName.toLowerCase().startsWith("msm_") ) {
            tables.add(tableName);
                      }
        }
        return tables;
    }


    private void processTable( String table ) {
        StringBuffer sb = new StringBuffer(table.length());
        String tableNew = table.toLowerCase();
        String[] tables = tableNew.split("_");
        String temp = null;
        for ( int i = 0 ; i < tables.length ; i++ ) {
            temp = tables[i].trim();
            sb.append(temp.substring(0, 1).toUpperCase()).append(temp.substring(1));
        }
        beanName = sb.toString();
        lowerbeanName = beanName.substring(0, 1).toLowerCase() + beanName.substring(1);
        mapperName = beanName + "Mapper";
    }


    private String processType( String type ) {
        if ( type.indexOf(type_char) > -1 ) {
            return "String";
        } else if ( type.indexOf(type_bigint) > -1 ) {
            return "Long";
        } else if ( type.indexOf(type_int) > -1 ) {
            return "Integer";
        } else if ( type.indexOf(type_date) > -1 ) {
            return "java.util.Date";
        } else if ( type.indexOf(type_text) > -1 ) {
            return "String";
        } else if ( type.indexOf(type_timestamp) > -1 ) {
            return "java.util.Date";
        } else if ( type.indexOf(type_bit) > -1 ) {
            return "Boolean";
        } else if ( type.indexOf(type_decimal) > -1 ) {
            return "java.math.BigDecimal";
        } else if ( type.indexOf(type_blob) > -1 ) {
            return "byte[]";
        }
        return null;
    }


    private String processField( String field ) {
        StringBuffer sb = new StringBuffer(field.length());
        //field = field.toLowerCase();
        String[] fields = field.split("_");
        String temp = null;
        sb.append(fields[0]);
        for ( int i = 1 ; i < fields.length ; i++ ) {
            temp = fields[i].trim();
            sb.append(temp.substring(0, 1).toUpperCase()).append(temp.substring(1));
        }
        return sb.toString();
    }


    /**
     *  将实体类名首字母改为小写
     *
     * @param beanName
     * @return
     */
    private String processResultMapId( String beanName ) {
        return beanName.substring(0, 1).toLowerCase() + beanName.substring(1);
    }


    /**
     *  构建类上面的注释
     *
     * @param bw
     * @param text
     * @return
     * @throws IOException
     */
    private BufferedWriter buildClassComment( BufferedWriter bw, String text ) throws IOException {
        bw.newLine();
        bw.newLine();
        bw.write("/**");
        bw.newLine();
        bw.write(" * ");
        bw.newLine();
        bw.write(" * " + text);
        bw.newLine();
        bw.write(" * ");
        bw.newLine();
        bw.write(" **/");
        return bw;
    }


    /**
     *  构建方法上面的注释
     *
     * @param bw
     * @param text
     * @return
     * @throws IOException
     */
    private BufferedWriter buildMethodComment( BufferedWriter bw, String text ) throws IOException {
        bw.newLine();
        bw.write("\t/**");
        bw.newLine();
        bw.write("\t * ");
        bw.newLine();
        bw.write("\t * " + text);
        bw.newLine();
        bw.write("\t * ");
        bw.newLine();
        bw.write("\t **/");
        return bw;
    }


    /**
     *  生成实体类
     *
     * @param columns
     * @param types
     * @param comments
     * @throws IOException
     */
    private void buildEntityBean( List<String> columns, List<String> types, List<String> comments, String tableComment )
            throws IOException {
        File folder = new File(bean_path);
        if ( !folder.exists() ) {
            folder.mkdir();
        }

        File beanFile = new File(bean_path, beanName + ".java");
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(beanFile)));
        bw.write("package " + bean_package + ";");
        bw.newLine();
        bw.write("import java.io.Serializable;");
        bw.newLine();
        //bw.write("import lombok.Data;");
        //      bw.write("import javax.persistence.Entity;");
        bw = buildClassComment(bw, tableComment);
        bw.newLine();
        bw.write("@SuppressWarnings(\"serial\")");
        bw.newLine();
        //      bw.write("@Entity");
        //bw.write("@Data");
//        bw.newLine();
        bw.write("public class " + beanName + " implements Serializable {");
        bw.newLine();
        bw.newLine();
        int size = columns.size();
        for ( int i = 0 ; i < size ; i++ ) {
            bw.write("\t/**" + comments.get(i) + "**/");
            bw.newLine();
            bw.write("\tprivate " + processType(types.get(i)) + " " + processField(columns.get(i)) + ";");
            bw.newLine();
            bw.newLine();
        }
        bw.newLine();
        // 生成get 和 set方法
        String tempField = null;
        String _tempField = null;
        String tempType = null;
        for ( int i = 0 ; i < size ; i++ ) {
            tempType = processType(types.get(i));
            _tempField = processField(columns.get(i));
            tempField = _tempField.substring(0, 1).toUpperCase() + _tempField.substring(1);
            bw.newLine();
            //          bw.write("\tpublic void set" + tempField + "(" + tempType + " _" + _tempField + "){");
            bw.write("\tpublic void set" + tempField + "(" + tempType + " " + _tempField + "){");
            bw.newLine();
            //          bw.write("\t\tthis." + _tempField + "=_" + _tempField + ";");
            bw.write("\t\tthis." + _tempField + " = " + _tempField + ";");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            bw.newLine();
            bw.write("\tpublic " + tempType + " get" + tempField + "(){");
            bw.newLine();
            bw.write("\t\treturn this." + _tempField + ";");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
        }
        bw.newLine();
        bw.write("}");
        bw.newLine();
        bw.flush();
        bw.close();
    }


    /**
     *  构建Mapper文件
     *
     * @throws IOException
     */
    private void buildMapper() throws IOException {
        File folder = new File(mapper_path);
        if ( !folder.exists() ) {
            folder.mkdirs();
        }

        File mapperFile = new File(mapper_path, mapperName + ".java");
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(mapperFile), "utf-8"));
        bw.write("package " + mapper_package + ";");
        bw.newLine();
        bw.write("import java.util.List;");
        bw.newLine();
        bw.write("import " + bean_package + "." + beanName + ";");
        bw.newLine();
        bw.write("import org.apache.ibatis.annotations.Param;");
        bw = buildClassComment(bw, mapperName + "数据库操作接口类");
        bw.newLine();
        bw.newLine();
        //      bw.write("public interface " + mapperName + " extends " + mapper_extends + "<" + beanName + "> {");
        bw.write("public interface " + mapperName + "{");
        bw.newLine();
        bw.newLine();
        // ----------定义Mapper中的方法Begin----------
        bw = buildMethodComment(bw, "查询(根据主键ID查询)");
        bw.newLine();
        bw.write("\t" + beanName + "  selectByPrimaryKey ( @Param(\"id\") Integer id );");
        bw.newLine();
        bw = buildMethodComment(bw, "添加");
        bw.newLine();
        bw.write("\t" + "int insert( " + beanName + " record );");
        bw.newLine();
        bw = buildMethodComment(bw, "添加 (匹配有值的字段)");
        bw.newLine();
        bw.write("\t" + "int insertSelective( " + beanName + " record );");
        bw.newLine();
        bw = buildMethodComment(bw, "修改 (匹配有值的字段)");
        bw.newLine();
        bw.write("\t" + "int updateByPrimaryKeySelective( " + beanName + " record );");
        bw.newLine();
        bw = buildMethodComment(bw, "分页查询</br>eg: xx.getByPage(null, 0, 3, \"id\", \"desc\");");
        bw.newLine();
        bw.write("\t" + "List<" + beanName + "> getByPage (@Param(\""+lowerbeanName +"\") "+ beanName + " " + lowerbeanName +", @Param(\"start\") Integer start, @Param(\"limit\") Integer limit, @Param(\"orderColumn\") String orderColumn, @Param(\"orderType\") String orderType );");
        bw.newLine();
        bw = buildMethodComment(bw, "根据实体属性查询");
        bw.newLine();
        bw.write("\t" + "List<" + beanName + "> getListByCondition (@Param(\""+lowerbeanName +"\") "+ beanName + " " + lowerbeanName + " );");
        bw.newLine();
        bw = buildMethodComment(bw, "根据实体属性查询");
        bw.newLine();
        bw.write("\t" + "int getCountByCondition (@Param(\""+lowerbeanName +"\") "+ beanName + " " + lowerbeanName + " );");
        bw.newLine();
        bw = buildMethodComment(bw, "查询单条数据");
        bw.newLine();
        bw.write("\t" + beanName + " getOneByCondition (@Param(\""+lowerbeanName +"\") "+ beanName + " " + lowerbeanName + " );");
        bw.newLine();

        // ----------定义Mapper中的方法End----------
        bw.newLine();
        bw.write("}");
        bw.flush();
        bw.close();
    }


    /**
     *  构建实体类映射XML文件
     *
     * @param columns
     * @param types
     * @param comments
     * @throws IOException
     */
    private void buildMapperXml( List<String> columns, List<String> types, List<String> comments ) throws IOException {
        File folder = new File(xml_path);
        if ( !folder.exists() ) {
            folder.mkdirs();
        }

        File mapperXmlFile = new File(xml_path, mapperName + ".xml");
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(mapperXmlFile)));
        bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        bw.newLine();
        bw.write("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" ");
        bw.newLine();
        bw.write("    \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">");
        bw.newLine();
        bw.write("<mapper namespace=\"" + mapper_package + "." + mapperName + "\">");
        bw.newLine();
        bw.newLine();

        bw.write("\t<!--实体映射-->");
        bw.newLine();
        bw.write("\t<resultMap id=\"BaseResultMap\" type=\"" + bean_package + "." + beanName + "\">");
        bw.newLine();
        bw.write("\t\t<id property=\"" + this.processField(columns.get(0)) + "\" column=\"" + columns.get(0) + "\" />");
        bw.newLine();
        int size = columns.size();
        for ( int i = 1 ; i < size ; i++ ) {
            bw.write("\t\t<result property=\""
                    + this.processField(columns.get(i)) + "\" column=\"" + columns.get(i) + "\" />");
            bw.newLine();
        }
        bw.write("\t</resultMap>");

        bw.newLine();
        bw.newLine();
        bw.newLine();

        // 下面开始写SqlMapper中的方法
        // this.outputSqlMapperMethod(bw, columns, types);
        buildSQL(bw, columns, types);

        bw.write("</mapper>");
        bw.flush();
        bw.close();
    }


    private void buildSQL( BufferedWriter bw, List<String> columns, List<String> types ) throws IOException {
        int size = columns.size();
        // 通用结果列
        bw.write("\t<!-- 通用查询结果列-->");
        bw.newLine();
        bw.write("\t<sql id=\"Base_Column_List\">");
        bw.newLine();

        bw.write("\t\t id,");
        for ( int i = 1 ; i < size ; i++ ) {
            bw.write("\t" + columns.get(i));
            if ( i != size - 1 ) {
                bw.write(",");
            }
        }

        bw.newLine();
        bw.write("\t</sql>");
        bw.newLine();
        bw.newLine();


        // 查询(根据主键ID查询)
        bw.write("\t<!-- 查询(根据主键ID查询) -->");
        bw.newLine();
        bw.write("\t<select id=\"selectByPrimaryKey\" resultMap=\"BaseResultMap\" parameterType=\"java.lang." + processType(types.get(0)) + "\">");
        bw.newLine();
        bw.write("\t\t SELECT");
        bw.newLine();
        bw.write("\t\t <include refid=\"Base_Column_List\" />");
        bw.newLine();
        bw.write("\t\t FROM " + tableName);
        bw.newLine();
        bw.write("\t\t WHERE " + columns.get(0) + " = #{" + processField(columns.get(0)) + "}");
        bw.newLine();
        bw.write("\t</select>");
        bw.newLine();
        bw.newLine();
        // 查询完


        // 分页查询
        bw.write("\t<!-- 分页查询 -->");
        bw.newLine();
        bw.write("\t<select id=\"getByPage\" resultMap=\"BaseResultMap\" resultType=\"" + bean_package + "." + beanName + "\">");
        bw.newLine();
        bw.write("\t\t SELECT");
        bw.newLine();
        bw.write("\t\t <include refid=\"Base_Column_List\" />");
        bw.newLine();
        bw.write("\t\t FROM " + tableName + " where 1=1 ");
        bw.newLine();
        bw.write("\t\t <if test=\" "+ lowerbeanName +" != null\">");
        bw.newLine();
        for ( int i = 0 ; i < size ; i++ ) {
            bw.write("\t\t\t<if test=\"" + lowerbeanName + "." + processField(columns.get(i)) + " != null\">");
            bw.newLine();
            bw.write("\t\t\t   and " + columns.get(i) + " = #{" + lowerbeanName + "." + processField(columns.get(i)) + "}");
            bw.newLine();
            bw.write("\t\t\t</if>");
            bw.newLine();
        }
        bw.write("\t\t </if>");
        bw.newLine();
        bw.write("\t\t <if test=\"orderColumn != null\">");
        bw.newLine();
        bw.write("\t\t\t order by ${orderColumn} ");
        bw.newLine();
        bw.write("\t\t </if>");
        bw.newLine();
        bw.write("\t\t <if test=\"orderType != null\">");
        bw.newLine();
        bw.write("\t\t\t ${orderType} ");
        bw.newLine();
        bw.write("\t\t </if>");
        bw.newLine();
        bw.write("\t\t <if test=\"start != null and limit != null\">");
        bw.newLine();
        bw.write("\t\t\t limit #{start}, #{limit}");
        bw.newLine();
        bw.write("\t\t </if>");
        bw.newLine();
        bw.write("\t</select>");
        bw.newLine();
        bw.newLine();
        // 分页查询完

        // 简单查询
        bw.write("\t<!-- 简单查询 -->");
        bw.newLine();
        bw.write("\t<select id=\"getListByCondition\" resultMap=\"BaseResultMap\" resultType=\"" + bean_package
                + "." + beanName + "\">");
        bw.newLine();
        bw.write("\t\t SELECT");
        bw.newLine();
        bw.write("\t\t <include refid=\"Base_Column_List\" />");
        bw.newLine();
        bw.write("\t\t FROM " + tableName + " where 1 = 1 ");
        bw.newLine();
        bw.write("\t\t <if test=\" "+ lowerbeanName +" != null\">");
        bw.newLine();
        for ( int i = 0 ; i < size ; i++ ) {
            bw.write("\t\t\t<if test=\"" + lowerbeanName + "." + processField(columns.get(i)) + " != null\">");
            bw.newLine();
            bw.write("\t\t\t   and " + columns.get(i) + " = #{" + lowerbeanName + "." + processField(columns.get(i)) + "}");
            bw.newLine();
            bw.write("\t\t\t</if>");
            bw.newLine();
        }
        bw.write("\t\t </if>");
        bw.newLine();
        bw.write("\t</select>");
        bw.newLine();
        bw.newLine();
        // 简单查询


        // 单条查询
        bw.write("\t<!-- 单条查询 -->");
        bw.newLine();
        bw.write("\t<select id=\"getOneByCondition\" resultMap=\"BaseResultMap\" resultType=\"" + bean_package
                + "." + beanName + "\">");
        bw.newLine();
        bw.write("\t\t SELECT");
        bw.newLine();
        bw.write("\t\t <include refid=\"Base_Column_List\" />");
        bw.newLine();
        bw.write("\t\t FROM " + tableName + " where 1 = 1 ");
        bw.newLine();
        bw.write("\t\t <if test=\" "+ lowerbeanName +" != null\">");
        bw.newLine();
        for ( int i = 0 ; i < size ; i++ ) {
            bw.write("\t\t\t<if test=\"" + lowerbeanName + "." + processField(columns.get(i)) + " != null\">");
            bw.newLine();
            bw.write("\t\t\t   and " + columns.get(i) + " = #{" + lowerbeanName + "." + processField(columns.get(i)) + "}");
            bw.newLine();
            bw.write("\t\t\t</if>");
            bw.newLine();
        }
        bw.write("\t\t </if>");
        bw.newLine();
        bw.write("\t</select>");
        bw.newLine();
        bw.newLine();
        // 单条查询

        // 数量查询
        bw.write("\t<!-- 数量查询 -->");
        bw.newLine();
        bw.write("\t<select id=\"getCountByCondition\" resultType=\"java.lang.Integer\">");
        bw.newLine();
        bw.write("\t\t SELECT");
        bw.newLine();
        bw.write("\t\t count(id) ");
        bw.newLine();
        bw.write("\t\t FROM " + tableName + " where 1 = 1 ");
        bw.newLine();
        bw.write("\t\t <if test=\" "+ lowerbeanName +" != null\">");
        bw.newLine();
        for ( int i = 0 ; i < size ; i++ ) {
            bw.write("\t\t\t<if test=\"" + lowerbeanName + "." + processField(columns.get(i)) + " != null\">");
            bw.newLine();
            bw.write("\t\t\t   and " + columns.get(i) + " = #{" + lowerbeanName + "." + processField(columns.get(i)) + "}");
            bw.newLine();
            bw.write("\t\t\t</if>");
            bw.newLine();
        }
        bw.write("\t\t </if>");
        bw.newLine();
        bw.write("\t</select>");
        bw.newLine();
        bw.newLine();
        // 数量查询


        // 添加insert方法
        bw.write("\t<!-- 添加 -->");
        bw.newLine();
        bw.write("\t<insert id=\"insert\" parameterType=\""+ bean_package + "."
                + beanName + "\">");
        bw.newLine();
        bw.write("\t\t INSERT INTO " + tableName);
        bw.newLine();
        bw.write(" \t\t(");
        for ( int i = 0 ; i < size ; i++ ) {
            bw.write(columns.get(i));
            if ( i != size - 1 ) {
                bw.write(",");
            }
        }
        bw.write(") ");
        bw.newLine();
        bw.write("\t\t VALUES ");
        bw.newLine();
        bw.write(" \t\t(");
        for ( int i = 0 ; i < size ; i++ ) {
            bw.write("#{" + processField(columns.get(i)) + "}");
            if ( i != size - 1 ) {
                bw.write(",");
            }
        }
        bw.write(") ");
        bw.newLine();
        bw.write("\t</insert>");
        bw.newLine();
        bw.newLine();
        // 添加insert完


        //---------------  insert方法(匹配有值的字段)
        bw.write("\t<!-- 添加 (匹配有值的字段)-->");
        bw.newLine();
        bw.write("\t<insert id=\"insertSelective\" parameterType=\"" + bean_package + "." + beanName + "\">");
        bw.newLine();
        bw.write("\t\t INSERT INTO " + tableName);
        bw.newLine();
        bw.write("\t\t <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >");
        bw.newLine();

        String tempField = null;
        for ( int i = 0 ; i < size ; i++ ) {
            tempField = processField(columns.get(i));
            bw.write("\t\t\t<if test=\"" + tempField + " != null\">");
            bw.newLine();
            bw.write("\t\t\t\t " + columns.get(i) + ",");
            bw.newLine();
            bw.write("\t\t\t</if>");
            bw.newLine();
        }

        bw.newLine();
        bw.write("\t\t </trim>");
        bw.newLine();

        bw.write("\t\t <trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >");
        bw.newLine();

        tempField = null;
        for ( int i = 0 ; i < size ; i++ ) {
            tempField = processField(columns.get(i));
            bw.write("\t\t\t<if test=\"" + tempField + "!=null\">");
            bw.newLine();
            bw.write("\t\t\t\t #{" + tempField + "},");
            bw.newLine();
            bw.write("\t\t\t</if>");
            bw.newLine();
        }

        bw.write("\t\t </trim>");
        bw.newLine();
        bw.write("\t</insert>");
        bw.newLine();
        bw.newLine();
        //---------------  完毕


        // 修改update方法
        bw.write("\t<!-- 修 改-->");
        bw.newLine();
        bw.write("\t<update id=\"updateByPrimaryKeySelective\" parameterType=\"" + bean_package + "." + beanName + "\">");
        bw.newLine();
        bw.write("\t\t UPDATE " + tableName);
        bw.newLine();
        bw.write(" \t\t <set> ");
        bw.newLine();

        tempField = null;
        for ( int i = 1 ; i < size ; i++ ) {
            tempField = processField(columns.get(i));
            bw.write("\t\t\t<if test=\"" + tempField + " != null\">");
            bw.newLine();
            bw.write("\t\t\t\t " + columns.get(i) + " = #{" + tempField + "},");
            bw.newLine();
            bw.write("\t\t\t</if>");
            bw.newLine();
        }

        bw.newLine();
        bw.write(" \t\t </set>");
        bw.newLine();
        bw.write("\t\t WHERE " + columns.get(0) + " = #{" + processField(columns.get(0)) + "}");
        bw.newLine();
        bw.write("\t</update>");
        bw.newLine();
        bw.newLine();
        // update方法完毕

    }


    /**
     *  获取所有的数据库表注释
     *
     * @return
     * @throws SQLException
     */
    private Map<String, String> getTableComment() throws SQLException {
        Map<String, String> maps = new HashMap<String, String>();
        PreparedStatement pstate = conn.prepareStatement("show table status");
        ResultSet results = pstate.executeQuery();
        while ( results.next() ) {
            String tableName = results.getString("NAME");
            String comment = results.getString("COMMENT");
            maps.put(tableName, comment);
        }
        return maps;
    }


    public void generate() throws ClassNotFoundException, SQLException, IOException {
        init();
        String prefix = "show full fields from ";
        List<String> columns = null;
        List<String> types = null;
        List<String> comments = null;
        PreparedStatement pstate = null;
        List<String> tables = getTables();
        Map<String, String> tableComments = getTableComment();
        for ( String table : tables ) {
            if (table.equals("raw.fin_scl_cust_credit_list")) {
                continue;
            }
            columns = new ArrayList<String>();
            types = new ArrayList<String>();
            comments = new ArrayList<String>();
            pstate = conn.prepareStatement(prefix + table);
            ResultSet results = pstate.executeQuery();
            while ( results.next() ) {
                columns.add(results.getString("FIELD"));
                types.add(results.getString("TYPE"));
                comments.add(results.getString("COMMENT"));
            }
            tableName = table;
            processTable(table);
            //          this.outputBaseBean();
            String tableComment = tableComments.get(tableName);
            buildEntityBean(columns, types, comments, tableComment);
            buildMapper();
            buildMapperXml(columns, types, comments);
        }
        conn.close();
    }


    public static void main( String[] args ) {
        try {
            new EntityUtil().generate();
            // 自动打开生成文件的目录
            Runtime.getRuntime().exec("cmd /c start explorer D:\\");
        } catch ( ClassNotFoundException e ) {
            LOGGER.error("error:", e);
        } catch ( SQLException e ) {
            LOGGER.error("error:", e);
        } catch ( IOException e ) {
            LOGGER.error("error:", e);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值