对比新文件 |
| | |
| | | package com.lunhan.xxx.host; |
| | | |
| | | import com.lunhan.xxx.common.ConstantFactory; |
| | | import com.lunhan.xxx.common.util.ListUtil; |
| | | import com.lunhan.xxx.common.util.LocalDateTimeUtil; |
| | | import com.lunhan.xxx.common.util.NumericUtil; |
| | | import com.lunhan.xxx.common.util.StringUtil; |
| | | import org.apache.commons.lang3.BooleanUtils; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileWriter; |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.sql.*; |
| | | import java.util.*; |
| | | |
| | | public class GenCodeMysql { |
| | | private static final String HOST = "113.250.189.120"; |
| | | private static final String PORT = "23306"; |
| | | private static final String USER = "lunhan"; |
| | | private static final String PASSWORD = "lunhan123"; |
| | | private static final String DB_NAME = "water_basic"; |
| | | private static final String AUTHOR = "lin.liu"; |
| | | |
| | | public static Connection CONN = null; |
| | | private static final String DRIVER = "com.mysql.cj.jdbc.Driver"; |
| | | private static final String CONNECTION_STR = "jdbc:mysql://" + HOST + ":" + PORT + "/" + DB_NAME + "?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false"; |
| | | |
| | | //region 输出文件路径设置 |
| | | class OutSet { |
| | | public static final String PO = "./src/main/java/com/lunhan/water/repository/po/"; |
| | | public static final String VO = "./src/main/java/com/lunhan/water/repository/vo/"; |
| | | public static final String COMVERT_MAPPER = "./src/main/java/com/lunhan/water/service/convert/"; |
| | | public static final String COLUMNS_ENUM = "./src/main/java/com/lunhan/water/repository/columns/"; |
| | | public static final String DAO = "./src/main/java/com/lunhan/water/repository/dao/"; |
| | | public static final String MAPPER = "./src/main/java/com/lunhan/water/repository/"; |
| | | public static final String MAPPERXML = "./src/main/resources/mapper/"; |
| | | public static final String SERVICE = "./src/main/java/com/lunhan/water/service/"; |
| | | public static final String Controller = "./src/main/java/com/lunhan/water/host/controller/"; |
| | | public static final String SEARCH = "./src/main/java/com/lunhan/water/entity/dto/search/"; |
| | | public static final String RequestDTO = "./src/main/java/com/lunhan/water/entity/request/"; |
| | | public static final String ResponseDTO = "./src/main/java/com/lunhan/water/entity/response/"; |
| | | } |
| | | //endregion |
| | | |
| | | //region 架包路径设置 |
| | | class PackageSet { |
| | | public static final String PO = "com.lunhan.water.repository.po"; |
| | | public static final String VO = "com.lunhan.water.repository.vo"; |
| | | public static final String CONVERT_MAPPER = "com.lunhan.water.service.convert"; |
| | | public static final String DAO = "com.lunhan.water.repository.dao"; |
| | | public static final String COLUMNS_ENUM = "com.lunhan.water.repository.columns"; |
| | | public static final String MAPPER = "com.lunhan.water.repository"; |
| | | public static final String SERVICE = "com.lunhan.water.service"; |
| | | public static final String Controller = "com.lunhan.water.host.controller"; |
| | | public static final String SEARCH = "com.lunhan.water.entity.dto.search"; |
| | | public static final String Request = "com.lunhan.water.entity.request"; |
| | | public static final String Response = "com.lunhan.water.entity.response"; |
| | | } |
| | | //endregion |
| | | |
| | | //region 后缀名设置 |
| | | class SuffixSet { |
| | | public static final String PO = "PO"; |
| | | public static final String VO = "VO"; |
| | | public static final String COLUMNS_ENUM = "";//POColumns |
| | | public static final String DAO = "Dao"; |
| | | public static final String MAPPER = "Mapper"; |
| | | public static final String SERVICE = "Service"; |
| | | public static final String Controller = "Controller"; |
| | | public static final String Request = "Req"; |
| | | public static final String Response = "Res"; |
| | | public static final String Create = "ReqCreate"; |
| | | public static final String Modify = "ReqModify"; |
| | | } |
| | | //endregion |
| | | |
| | | //region po中getter/setter生成方式设置 |
| | | /** |
| | | * po中getter/setter生成方式 传统=1,lombok注解=2 |
| | | */ |
| | | private static final Integer SET_PO_Getter = 1; |
| | | //endregion |
| | | |
| | | //region po类头部文字设置 |
| | | private static final String SET_BEFORE_TEXT = |
| | | "/**\n" + |
| | | "# __----~~~~~~~~~~~------___\n" + |
| | | "# . . ~~//====...... __--~ ~~\n" + |
| | | "# -. \\_|// |||\\\\ ~~~~~~::::... /~\n" + |
| | | "# ___-==_ _-~o~ \\/ ||| \\\\ _/~~-\n" + |
| | | "# __---~~~.==~||\\=_ -_--~/_-~|- |\\\\ \\\\ _/~\n" + |
| | | "# _-~~ .=~ | \\\\-_ '-~7 /- / || \\ /\n" + |
| | | "# .~ .~ | \\\\ -_ / /- / || \\ /\n" + |
| | | "# / ____ / | \\\\ ~-_/ /|- _/ .|| \\ /\n" + |
| | | "# |~~ ~~|--~~~~--_ \\ ~==-/ | \\~--===~~ .\\\n" + |
| | | "# ' ~-| /| |-~\\~~ __--~~\n" + |
| | | "# |-~~-_/ | | ~\\_ _-~ /\\\n" + |
| | | "# / \\ \\__ \\/~ \\__\n" + |
| | | "# _--~ _/ | .-~~____--~-/ ~~==.\n" + |
| | | "# ((->/~ '.|||' -_| ~~-/ , . _||\n" + |
| | | "# -_ ~\\ ~~---l__i__i__i--~~_/\n" + |
| | | "# _-~-__ ~) \\--______________--~~\n" + |
| | | "# //.-~~~-~_--~- |-------~~~~~~~~\n" + |
| | | "# //.-~~~--\\\n" + |
| | | "# 神兽保佑\n" + |
| | | "# 永无BUG!\n" + |
| | | "*/"; |
| | | //endregion |
| | | |
| | | //region po类 import 内容设置 |
| | | private static final String[] SET_PO_IMPORT = new String[]{ |
| | | "import javax.persistence.*;", |
| | | "import java.io.Serializable;", |
| | | "import java.sql.Timestamp;" |
| | | }; |
| | | //endregion |
| | | |
| | | //region vo类 import 内容设置 |
| | | private static final String[] SET_VO_IMPORT = new String[]{ |
| | | "import com.lunhan.water.common.util.LocalDateTimeUtil;", |
| | | "import com.lunhan.water.common.util.NumericUtil;", |
| | | "import java.util.Objects;" |
| | | }; |
| | | //endregion |
| | | |
| | | //region 实体映射类 import 内容设置 |
| | | private static final String[] SET_CONVERT_IMPORT = new String[]{ |
| | | "import org.mapstruct.Mapper;" + |
| | | "import org.mapstruct.factory.Mappers;" + |
| | | "" + |
| | | "import java.util.List;" |
| | | }; |
| | | //endregion |
| | | |
| | | //region 字段名枚举类 import 内容设置 |
| | | private static final String[] SET_COLUMNS_ENUM_IMPORT = new String[]{ |
| | | "import com.lunhan.water.common.orm2.enums.ColumnBasic;", |
| | | "import java.util.Arrays;", |
| | | "import java.util.Objects;" |
| | | }; |
| | | //endregion |
| | | |
| | | //region dao类 import 内容设置 |
| | | private static final String[] SET_DAO_IMPORT = new String[]{ |
| | | "import com.lunhan.water.common.ConstantFactory;", |
| | | "import com.lunhan.water.common.PagerResult;", |
| | | "import com.lunhan.water.common.orm2.SqlBuilder;", |
| | | "import com.lunhan.water.common.util.*;", |
| | | "import com.lunhan.water.entity.dto.OrderByDTO;", |
| | | "import com.lunhan.water.entity.enums.*;", |
| | | "import com.lunhan.water.repository.BasicDao;", |
| | | "import org.springframework.stereotype.Repository;", |
| | | "import java.util.*;", |
| | | "import org.springframework.transaction.annotation.Transactional;" |
| | | }; |
| | | //endregion |
| | | |
| | | //region service类 import 内容设置 |
| | | private static final String[] SET_Service_IMPORT = new String[]{ |
| | | "import com.lunhan.water.common.*;", |
| | | "import com.lunhan.water.common.model.Tuple;", |
| | | "import com.lunhan.water.common.util.*;", |
| | | "import org.apache.commons.lang3.BooleanUtils;", |
| | | "import org.springframework.beans.factory.annotation.Autowired;", |
| | | "import org.springframework.stereotype.Service;", |
| | | "import java.util.ArrayList;", |
| | | "import java.util.List;", |
| | | "import java.util.Objects;" |
| | | }; |
| | | //endregion |
| | | |
| | | //region Controller类 import 内容设置 |
| | | private static final String[] SET_Controller_IMPORT = new String[]{ |
| | | "import com.lunhan.water.common.ExecutedResult;", |
| | | "import com.lunhan.water.common.PagerResult;", |
| | | "import com.lunhan.water.common.util.ParameterUtil;", |
| | | "import com.lunhan.water.common.validator.ParameterValidateResult;", |
| | | "import com.lunhan.water.common.validator.ParameterValidator;", |
| | | "import com.lunhan.water.entity.request.ReqListId;", |
| | | "import com.lunhan.water.host.BasicController;", |
| | | "import org.springframework.beans.factory.annotation.Autowired;", |
| | | "import org.springframework.web.bind.annotation.*;", |
| | | "import java.util.List;" |
| | | }; |
| | | //endregion |
| | | |
| | | //region search类 import 内容设置 |
| | | private static final String[] SET_SEARCH_IMPORT = new String[]{ |
| | | "import " + PackageSet.SEARCH.replaceAll("\\.search", "") + ".SearchBasicDTO;", |
| | | }; |
| | | //endregion |
| | | |
| | | //region 类注释模版 |
| | | private static final String SET_CLASS_DESC_TPL = |
| | | "/**\n" + |
| | | " * {#=desc}\n" + |
| | | " * @author {#=author}\n" + |
| | | " * @description {#=desc}\n" + |
| | | " */"; |
| | | private static final String SET_METHOD_DESC_TPL = |
| | | " /**\n" + |
| | | " * {#=desc}\n" + |
| | | " * @author {#=author}\n" + |
| | | " * @description {#=desc}\n" + |
| | | " */"; |
| | | private static final String SET_PRO_DESC_TPL = |
| | | "\t/**\n" + |
| | | "\t * {#=desc}\n" + |
| | | "\t */"; |
| | | //endregion |
| | | |
| | | //#region 创建接口忽略字段配置 |
| | | private static final List<String> NO_CREATE_COLUMNS = Arrays.asList( |
| | | "id", |
| | | "create_time", "create_user", "create_user_id", "create_user_name", |
| | | "update_time", "update_user", "update_user_id", "update_user_name", |
| | | "datachange_lasttime", "data_change_last_time", "is_delete", "status", "state" |
| | | ); |
| | | //#endregion |
| | | |
| | | //#region 编辑接口忽略字段配置 |
| | | private static final List<String> NO_UPDATE_COLUMNS = Arrays.asList( |
| | | "create_time", "create_user", "create_user_id", "create_user_name", |
| | | "update_time", "update_user", "update_user_id", "update_user_name", |
| | | "datachange_lasttime", "data_change_last_time", "is_delete" |
| | | ); |
| | | private static final List<String> UPDATE_TIME_COLUMNS = Arrays.asList( |
| | | "update_time", "datachange_lasttime", "data_change_last_time" |
| | | ); |
| | | //#endregion |
| | | |
| | | static { |
| | | //加载驱动 |
| | | try { |
| | | Class.forName(DRIVER); |
| | | } catch (ClassNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //获得连接 |
| | | try { |
| | | CONN = DriverManager.getConnection(CONNECTION_STR, USER, PASSWORD); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 只生成以下配置的表 |
| | | */ |
| | | private static final List<String> ONLY_TABLES = Arrays.asList( |
| | | "test_info" |
| | | ); |
| | | |
| | | public static void main(String[] args) { |
| | | if (null == CONN) { |
| | | System.out.println("conn is null."); |
| | | return; |
| | | } |
| | | Map<String, String> tables = getTables(DB_NAME); |
| | | for (Map.Entry<String, String> table : tables.entrySet()) { |
| | | String tableName = table.getKey(); |
| | | String tableDesc = table.getValue(); |
| | | System.out.println(tableName + " - " + tableDesc); |
| | | if (ONLY_TABLES.size() > 0 && BooleanUtils.isFalse(ONLY_TABLES.contains(tableName))) { |
| | | continue; |
| | | } |
| | | List<Map<String, Object>> columns = getColumns(tableName, DB_NAME); |
| | | genPO(columns, tableDesc); |
| | | genVO(columns, tableDesc); |
| | | genColumnsEnum(columns, tableDesc); |
| | | genCreateDto(columns, tableName, tableDesc); |
| | | genModifyDto(columns, tableName, tableDesc); |
| | | genDao(tableName, tableDesc); |
| | | genSearch(columns, tableDesc); |
| | | genConvertMapper(tableName, tableDesc); |
| | | genService(tableName, tableDesc); |
| | | genController(tableName, tableDesc); |
| | | |
| | | //genMapperXml(columns, tableDesc); |
| | | //genMapper(columns, tableDesc); |
| | | } |
| | | } |
| | | |
| | | private static void genPO(List<Map<String, Object>> columns, String tableDesc) { |
| | | if (ListUtil.isNullOrEmpty(columns)) { |
| | | return; |
| | | } |
| | | String tableName = columns.get(0).get("objName").toString(); |
| | | String modelName = underline2Camel(tableName, true); |
| | | String poName = modelName + SuffixSet.PO; |
| | | boolean isData = Objects.equals(SET_PO_Getter, 2); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.PO); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (isData) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("import lombok.Data;"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | if (SET_PO_IMPORT.length > 0) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | for (String s : SET_PO_IMPORT) { |
| | | content.append(s); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | } |
| | | |
| | | boolean hasBigDecimal = false; |
| | | List<String> listColumn = new ArrayList<>(); |
| | | List<String> listGetterSetter = new ArrayList<>(); |
| | | for (Map<String, Object> column : columns) { |
| | | String name = column.get("name").toString(); |
| | | String desc = String.valueOf(column.get("description")); |
| | | String type = column.get("type").toString(); |
| | | int length = NumericUtil.tryParseInt(column.get("length"), 0); |
| | | String defValue = String.valueOf(column.get("default")); |
| | | int isPK = NumericUtil.tryParseInt(column.get("isPK"), 0); |
| | | int isIdentity = NumericUtil.tryParseInt(column.get("isIdentity"), 0); |
| | | int isNullable = NumericUtil.tryParseInt(column.get("isNullable"), 0); |
| | | |
| | | String javaType = getJavaType(type); |
| | | if ("BigDecimal".equalsIgnoreCase(javaType)) { |
| | | hasBigDecimal = true; |
| | | } |
| | | String propName = underline2Camel(name); |
| | | String columnNameUpper = underline2Camel(name, true); |
| | | |
| | | listColumn.add(SET_PRO_DESC_TPL.replaceAll("\\{\\#\\=desc\\}", desc)); |
| | | if (Objects.equals(isPK, 1)) { |
| | | listColumn.add("\t@Id"); |
| | | } |
| | | if (Objects.equals(isIdentity, 1)) { |
| | | listColumn.add("\t@GeneratedValue(strategy = GenerationType.AUTO)"); |
| | | } |
| | | String columnAnnotation = "\t@Column(name = \"" + name + "\""; |
| | | //忽略最后修改时间这一列的新增和编辑 |
| | | if (UPDATE_TIME_COLUMNS.stream().anyMatch(c -> c.equalsIgnoreCase(name))) { |
| | | columnAnnotation += ", insertable = false, updatable = false"; |
| | | } |
| | | if ("datachange_lasttime".equalsIgnoreCase(name)) { |
| | | propName = "dataChangeLastTime"; |
| | | columnNameUpper = "DataChangeLastTime"; |
| | | } |
| | | listColumn.add(columnAnnotation + ")"); |
| | | //追加列 |
| | | listColumn.add(String.format("\tprivate %s %s;", javaType, propName)); |
| | | |
| | | listGetterSetter.add(""); |
| | | listGetterSetter.add(String.format("\tpublic %s get%s() {", javaType, columnNameUpper)); |
| | | listGetterSetter.add(String.format("\t\treturn this.%s;", propName)); |
| | | listGetterSetter.add("\t}"); |
| | | listGetterSetter.add(String.format("\tpublic void set%s(%s %s) {", columnNameUpper, javaType, propName)); |
| | | listGetterSetter.add(String.format("\t\tthis.%s = %s;", propName, propName)); |
| | | listGetterSetter.add("\t}"); |
| | | } |
| | | if (hasBigDecimal) { |
| | | content.append("import java.math.BigDecimal;"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (isData) { |
| | | content.append("@Data"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | content.append("@Entity"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("@Table(name = \"" + tableName); |
| | | content.append("\")"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("public class "); |
| | | content.append(poName); |
| | | content.append(" implements Serializable {"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(StringUtil.join(listColumn, ConstantFactory.STR_NEWLINE)); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(StringUtil.join(listGetterSetter, ConstantFactory.STR_NEWLINE)); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("}"); |
| | | |
| | | writeFile(OutSet.PO, modelName + SuffixSet.PO + ".java", content.toString()); |
| | | } |
| | | |
| | | private static void genVO(List<Map<String, Object>> columns, String tableDesc) { |
| | | if (ListUtil.isNullOrEmpty(columns)) { |
| | | return; |
| | | } |
| | | String tableName = columns.get(0).get("objName").toString(); |
| | | String modelName = underline2Camel(tableName, true); |
| | | String poName = modelName + SuffixSet.PO; |
| | | String voName = modelName + SuffixSet.VO; |
| | | boolean isData = Objects.equals(SET_PO_Getter, 2); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.VO); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (isData) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("import lombok.Data;"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | if (SET_VO_IMPORT.length > 0) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | for (String s : SET_VO_IMPORT) { |
| | | content.append(s); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | } |
| | | content.append("import "); |
| | | content.append(PackageSet.PO); |
| | | content.append("."); |
| | | content.append(poName + ";\n"); |
| | | |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (isData) { |
| | | content.append("@Data"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("public class "); |
| | | content.append(voName); |
| | | content.append(" extends " + poName + " implements BasicVO {\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" @Override\n" + |
| | | " public String getCreateTimeView() {\n" + |
| | | " if (NumericUtil.tryParseLong(this.getCreateTime()).compareTo(0L) > 0) {\n" + |
| | | " return LocalDateTimeUtil.toFormatString(this.getCreateTime());\n" + |
| | | " }\n" + |
| | | " return \"\";\n" + |
| | | " }\n" + |
| | | "\n" + |
| | | " @Override\n" + |
| | | " public String getUpdateTimeView() {\n" + |
| | | " if (Objects.isNull(this.getUpdateTime())) {\n" + |
| | | " return \"\";\n" + |
| | | " }\n" + |
| | | " return LocalDateTimeUtil.toFormatFullString(this.getUpdateTime());\n" + |
| | | " }"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.VO + modelName + SuffixSet.VO + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.VO, modelName + SuffixSet.VO + ".java", content.toString()); |
| | | } |
| | | |
| | | private static void genSearch(List<Map<String, Object>> columns, String tableDesc) { |
| | | if (ListUtil.isNullOrEmpty(columns)) { |
| | | return; |
| | | } |
| | | String tableName = columns.get(0).get("objName").toString(); |
| | | String modelName = underline2Camel(tableName, true); |
| | | String searchName = "Search" + modelName; |
| | | String tableNamePackage = tableName.toLowerCase().replaceAll("_", ""); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("package "); |
| | | content.append(PackageSet.SEARCH + ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (SET_SEARCH_IMPORT.length > 0) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | for (String s : SET_SEARCH_IMPORT) { |
| | | content.append(s); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("public class " + searchName + " extends SearchBasicDTO {"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t@Override"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\tpublic String toString() {"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\treturn \"" + searchName + "{\" + \"'\" +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\"keywords='\" + getKeywords() +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\", listId =\" + getListId() +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\", status =\" + getStatus() +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\", listStatus =\" + getListStatus() +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\", createTimeRange='\" + getCreateTimeRange() + \"'\" +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\", createTimeStart =\" + getCreateTimeEnd() +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\", createTimeEnd =\" + getCreateTimeEnd() +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\", lastId =\" + getListId() +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t\t\", orderBy='\" + getOrderBy() + \"'\" +"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t\t'}';"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t}"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.SEARCH + "/" + searchName + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.SEARCH + "/", searchName + ".java", content.toString()); |
| | | } |
| | | |
| | | private static void genColumnsEnum(List<Map<String, Object>> columns, String tableDesc) { |
| | | if (ListUtil.isNullOrEmpty(columns)) { |
| | | return; |
| | | } |
| | | String tableName = columns.get(0).get("objName").toString(); |
| | | String modelName = underline2Camel(tableName, true); |
| | | String enumName = "E" + modelName + SuffixSet.COLUMNS_ENUM; |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.COLUMNS_ENUM); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (SET_COLUMNS_ENUM_IMPORT.length > 0) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | for (String s : SET_COLUMNS_ENUM_IMPORT) { |
| | | content.append(s); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("public enum "); |
| | | content.append(enumName); |
| | | content.append(" implements ColumnBasic {"); |
| | | |
| | | List<String> listColumn = new ArrayList<>(); |
| | | String lastStr = ""; |
| | | int idx = 0; |
| | | for (Map<String, Object> column : columns) { |
| | | idx++; |
| | | String name = column.get("name").toString(); |
| | | String desc = String.valueOf(column.get("description")); |
| | | String type = column.get("type").toString(); |
| | | int length = NumericUtil.tryParseInt(column.get("length"), 0); |
| | | String defValue = String.valueOf(column.get("default")); |
| | | int isPK = NumericUtil.tryParseInt(column.get("isPK"), 0); |
| | | int isIdentity = NumericUtil.tryParseInt(column.get("isIdentity"), 0); |
| | | int isNullable = NumericUtil.tryParseInt(column.get("isNullable"), 0); |
| | | |
| | | String propName = underline2Camel(name); |
| | | |
| | | listColumn.add(SET_PRO_DESC_TPL.replaceAll("\\{\\#\\=desc\\}", desc)); |
| | | String str = String.format("\t%s(\"%s\", %s),", name.toUpperCase(), name, idx); |
| | | //追加列 |
| | | listColumn.add(str); |
| | | lastStr = str; |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(StringUtil.join(listColumn, ConstantFactory.STR_NEWLINE).replace(lastStr, lastStr.replace("),", ");"))); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\tprivate String columnName;\n"); |
| | | content.append("\tprivate Integer value;\n"); |
| | | content.append("\tpublic Integer getValue() {\n"); |
| | | content.append("\t\treturn value;\n"); |
| | | content.append("\t}\n"); |
| | | content.append("\tpublic String getColumnName() {\n"); |
| | | content.append("\t\treturn columnName;\n"); |
| | | content.append("\t}\n"); |
| | | content.append("\tstatic final String TABLE_NAME = \"" + tableName + "\";\n"); |
| | | content.append("\tpublic String getTableName() {\n"); |
| | | content.append("\t\treturn TABLE_NAME;\n"); |
| | | content.append("\t}\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t"); |
| | | content.append(enumName); |
| | | content.append("(String columnName, Integer value) {\n"); |
| | | content.append("\t\tthis.columnName = columnName;\n"); |
| | | content.append("\t\tthis.value = value;\n"); |
| | | content.append("\t}\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\tpublic static " + enumName + " getByValue(Integer value) {\n"); |
| | | content.append("\t\treturn Arrays.stream(" + enumName + ".values())\n"); |
| | | content.append("\t\t\t\t.filter(e -> Objects.equals(e.getValue(), value))\n"); |
| | | content.append("\t\t\t\t.findAny()\n"); |
| | | content.append("\t\t\t\t.orElse(null);\n"); |
| | | content.append("\t}\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t@Override\n"); |
| | | content.append("\tpublic String toString() {\n"); |
| | | content.append("\t\treturn \"" + enumName + "{\" +\n"); |
| | | content.append("\t\t\t\t\"columnName='\" + columnName + '\\'' +\n"); |
| | | content.append("\t\t\t\t\", value=\" + value +\n"); |
| | | content.append("\t\t\t\t'}';\n"); |
| | | content.append("\t}\n"); |
| | | content.append("}"); |
| | | |
| | | writeFile(OutSet.COLUMNS_ENUM, enumName + ".java", content.toString()); |
| | | } |
| | | |
| | | private static void genDao(String tableName, String tableDesc) { |
| | | String modelName = underline2Camel(tableName, true); |
| | | String daoName = modelName + SuffixSet.DAO; |
| | | String poName = modelName + SuffixSet.PO; |
| | | String enumName = "E" + modelName; |
| | | String searchName = "Search" + modelName; |
| | | String tableNamePackage = tableName.toLowerCase().replaceAll("_", ""); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.DAO); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (SET_DAO_IMPORT.length > 0) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | for (String s : SET_DAO_IMPORT) { |
| | | content.append(s); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | } |
| | | content.append("import "); |
| | | content.append(PackageSet.COLUMNS_ENUM); |
| | | content.append("."); |
| | | content.append("E" + modelName + SuffixSet.COLUMNS_ENUM + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.PO); |
| | | content.append("."); |
| | | content.append(poName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.SEARCH); |
| | | content.append("."); |
| | | content.append(searchName + ";\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("@Repository\n"); |
| | | content.append("public class "); |
| | | content.append(daoName); |
| | | content.append(" extends BasicDao<"); |
| | | content.append(modelName + SuffixSet.PO); |
| | | content.append("> {"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("\t" + daoName + "() {\n"); |
| | | content.append("\t\tsuper(E" + modelName + SuffixSet.COLUMNS_ENUM + ".values());\n"); |
| | | content.append("\t}\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" /**\n" + |
| | | " * 批量删除数据\n" + |
| | | " * @param listId 主键id列表\n" + |
| | | " * @return 是否成功\n" + |
| | | " */\n" + |
| | | " public Boolean removeByListId(List<Long> listId) {\n" + |
| | | " return super.remove(new SqlBuilder().where(" + enumName + ".ID.in(listId)));\n" + |
| | | " }\n\n"); |
| | | content.append(" /**\n" + |
| | | " * 根据主键获取数据列表\n" + |
| | | " * @param listId 主键id列表\n" + |
| | | " * @return 数据实体\n" + |
| | | " */\n" + |
| | | " @Transactional(readOnly = true)\n" + |
| | | " public List<" + poName + "> getListById(List<Long> listId) {\n" + |
| | | " return super.getList(new SqlBuilder()\n" + |
| | | " .where(" + enumName + ".ID.in(listId))\n" + |
| | | " );\n" + |
| | | " }\n\n"); |
| | | content.append("\tpublic PagerResult<" + poName + "> search(" + searchName + " search) {\n" + |
| | | "\t\tSqlBuilder builder = new SqlBuilder()\n" + |
| | | "\t\t\t\t// 分页参数\n" + |
| | | "\t\t\t\t.page(NumericUtil.tryParseInt(search.getPage(), 1), NumericUtil.tryParseInt(search.getLimit(), 1))\n" + |
| | | "\n" + |
| | | "\t\t\t\t// 数据非逻辑删除\n" + |
| | | "\t\t\t\t//.and(" + enumName + ".IS_DELETE.equal(EYesOrNo.NO.getValue()))\n" + |
| | | "\t\t\t\t// 状态非删除状态\n" + |
| | | "\t\t\t\t//.and(" + enumName + ".STATUS.notEqual(EState.DELETED.getValue()))\n" + |
| | | "\n" + |
| | | "\t\t\t\t// 状态\n" + |
| | | "\t\t\t\t//.andIf(NumericUtil.tryParseInt(search.getStatus()).compareTo(0) > 0, " + enumName + ".STATUS.equal(search.getStatus()))\n" + |
| | | "\t\t\t\t// 状态列表\n" + |
| | | "\t\t\t\t//.andIf(ListUtil.isNotNullOrEmpty(search.getListStatus()), " + enumName + ".STATUS.in(search.getListStatus()))\n" + |
| | | "\t\t\t\t// id列表\n" + |
| | | "\t\t\t\t.andIf(ListUtil.isNotNullOrEmpty(search.getListId()), " + enumName + ".ID.in(search.getListId()))\n" + |
| | | "\t\t\t\t// 关键字\n" + |
| | | "\t\t\t\t//.andIf(StringUtil.isNotNullOrEmpty(search.getKeywords()), " + enumName + ".NAME.like(search.getKeywords()))\n" + |
| | | "\t\t\t\t// 创建时间-起始\n" + |
| | | "\t\t\t\t.andIf(NumericUtil.tryParseLong(search.getCreateTimeStart()).compareTo(0L) > 0, " + enumName + ".CREATE_TIME.greaterOrEqual(search.getCreateTimeStart()))\n" + |
| | | "\t\t\t\t// 创建时间-截止\n" + |
| | | "\t\t\t\t.andIf(NumericUtil.tryParseLong(search.getCreateTimeEnd()).compareTo(0L) > 0, " + enumName + ".CREATE_TIME.lessOrEqual(search.getCreateTimeEnd()))\n" + |
| | | "\t\t\t\t;\n" + |
| | | "\t\tif (ListUtil.isNotNullOrEmpty(search.getOrderBy())) {\n" + |
| | | "\t\t\tList<String> listOrderBy = new ArrayList<>();\n" + |
| | | "\t\t\tfor (OrderByDTO item : search.getOrderBy()) {\n" + |
| | | "\t\t\t\tEOrderBy orderBy = EOrderBy.getByValue(NumericUtil.tryParseInt(item.getOrderBy(), 1));\n" + |
| | | "\t\t\t\tif (Objects.isNull(orderBy)) {\n" + |
| | | "\t\t\t\t\tcontinue;\n" + |
| | | "\t\t\t\t}\n" + |
| | | "\t\t\t\tswitch (orderBy) {\n" + |
| | | "\t\t\t\t\tcase ID:\n" + |
| | | "\t\t\t\t\t\tlistOrderBy.add(" + enumName + ".ID.getColumnName() +\n" + |
| | | "\t\t\t\t\t\t\t\t(item.getIsAsc() ? ConstantFactory.SQL_ASC : ConstantFactory.SQL_DESC)\n" + |
| | | "\t\t\t\t\t\t);\n" + |
| | | "\t\t\t\t\t\tbreak;\n" + |
| | | "\n" + |
| | | "\t\t\t\t\tcase CREATE_TIME:\n" + |
| | | "\t\t\t\t\t\tlistOrderBy.add(" + enumName + ".CREATE_TIME.getColumnName() +\n" + |
| | | "\t\t\t\t\t\t\t\t(item.getIsAsc() ? ConstantFactory.SQL_ASC : ConstantFactory.SQL_DESC)\n" + |
| | | "\t\t\t\t\t\t);\n" + |
| | | "\t\t\t\t\t\tbreak;\n" + |
| | | "\n" + |
| | | "\t\t\t\t\tcase UPDATE_TIME:\n" + |
| | | "\t\t\t\t\t\tlistOrderBy.add(" + enumName + ".UPDATE_TIME.getColumnName() +\n" + |
| | | "\t\t\t\t\t\t\t\t(item.getIsAsc() ? ConstantFactory.SQL_ASC : ConstantFactory.SQL_DESC)\n" + |
| | | "\t\t\t\t\t\t);\n" + |
| | | "\t\t\t\t\t\tbreak;\n" + |
| | | "\n" + |
| | | "\t\t\t\t\tdefault:\n" + |
| | | "\t\t\t\t\t\tbreak;\n" + |
| | | "\t\t\t\t}\n" + |
| | | "\t\t\t}\n" + |
| | | "\t\t\tbuilder.orderBy(ListUtil.toArray(listOrderBy));\n" + |
| | | "\t\t}\n" + |
| | | "\t\treturn super.getPageList(builder);\n" + |
| | | "\t} else {\n" + |
| | | "\t\t\tbuilder.orderBy(" + enumName + ".ID.getColumnName() + ConstantFactory.SQL_DESC);\n" + |
| | | "\t\t}"); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.DAO + daoName + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.DAO, daoName + ".java", content.toString()); |
| | | } |
| | | |
| | | private static Boolean IsNoCreate(String name, Integer isPK, Integer isIdentity) { |
| | | if (Objects.equals(isPK, 1) || Objects.equals(isIdentity, 1) || NO_CREATE_COLUMNS.stream().anyMatch(c -> c.equalsIgnoreCase(name))) { |
| | | return Boolean.TRUE; |
| | | } |
| | | return Boolean.FALSE; |
| | | } |
| | | |
| | | private static Boolean IsNoModify(String name) { |
| | | if (NO_UPDATE_COLUMNS.stream().anyMatch(c -> c.equalsIgnoreCase(name))) { |
| | | return Boolean.TRUE; |
| | | } |
| | | return Boolean.FALSE; |
| | | } |
| | | |
| | | private static void genCreateDto(List<Map<String, Object>> columns, String tabName, String tableDesc) { |
| | | if (ListUtil.isNullOrEmpty(columns)) { |
| | | return; |
| | | } |
| | | String tableName = columns.get(0).get("objName").toString(); |
| | | String modelName = underline2Camel(tableName, true); |
| | | String poName = modelName + SuffixSet.PO; |
| | | String createDTOName = SuffixSet.Create + modelName; |
| | | boolean isData = Objects.equals(SET_PO_Getter, 2); |
| | | String tableNamePackage = tableName.toLowerCase().replaceAll("_", ""); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.Request + "." + tableNamePackage); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (isData) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("import lombok.Data;"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | boolean hasBigDecimal = false; |
| | | List<String> listColumn = new ArrayList<>(); |
| | | List<String> listGetterSetter = new ArrayList<>(); |
| | | for (Map<String, Object> column : columns) { |
| | | String name = column.get("name").toString(); |
| | | String desc = String.valueOf(column.get("description")); |
| | | String type = column.get("type").toString(); |
| | | int length = NumericUtil.tryParseInt(column.get("length"), 0); |
| | | String defValue = String.valueOf(column.get("default")); |
| | | int isPK = NumericUtil.tryParseInt(column.get("isPK"), 0); |
| | | int isIdentity = NumericUtil.tryParseInt(column.get("isIdentity"), 0); |
| | | int isNullable = NumericUtil.tryParseInt(column.get("isNullable"), 0); |
| | | |
| | | String javaType = getJavaType(type); |
| | | if ("BigDecimal".equalsIgnoreCase(javaType)) { |
| | | hasBigDecimal = true; |
| | | } |
| | | String propName = underline2Camel(name); |
| | | String columnNameUpper = underline2Camel(name, true); |
| | | |
| | | // 主键和自增列不加入新增接口的入参 |
| | | if (GenCodeMysql.IsNoCreate(name, isPK, isIdentity)) { |
| | | continue; |
| | | } |
| | | listColumn.add(SET_PRO_DESC_TPL.replaceAll("\\{\\#\\=desc\\}", desc)); |
| | | //追加列 |
| | | listColumn.add(String.format("\tprivate %s %s;", javaType, propName)); |
| | | |
| | | listGetterSetter.add(""); |
| | | listGetterSetter.add(String.format("\tpublic %s get%s() {", javaType, columnNameUpper)); |
| | | listGetterSetter.add(String.format("\t\treturn this.%s;", propName)); |
| | | listGetterSetter.add("\t}"); |
| | | listGetterSetter.add(String.format("\tpublic void set%s(%s %s) {", columnNameUpper, javaType, propName)); |
| | | listGetterSetter.add(String.format("\t\tthis.%s = %s;", propName, propName)); |
| | | listGetterSetter.add("\t}"); |
| | | } |
| | | if (hasBigDecimal) { |
| | | content.append("import java.math.BigDecimal;"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (isData) { |
| | | content.append("@Data"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("public class "); |
| | | content.append(createDTOName); |
| | | content.append(" {"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(StringUtil.join(listColumn, ConstantFactory.STR_NEWLINE)); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(StringUtil.join(listGetterSetter, ConstantFactory.STR_NEWLINE)); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.RequestDTO + tableNamePackage + "/" + createDTOName + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.RequestDTO + tableNamePackage + "/", createDTOName + ".java", content.toString()); |
| | | } |
| | | |
| | | private static void genModifyDto(List<Map<String, Object>> columns, String tabName, String tableDesc) { |
| | | if (ListUtil.isNullOrEmpty(columns)) { |
| | | return; |
| | | } |
| | | String tableName = columns.get(0).get("objName").toString(); |
| | | String modelName = underline2Camel(tableName, true); |
| | | String poName = modelName + SuffixSet.PO; |
| | | String modifyDTOName = SuffixSet.Modify + modelName; |
| | | boolean isData = Objects.equals(SET_PO_Getter, 2); |
| | | String tableNamePackage = tableName.toLowerCase().replaceAll("_", ""); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.Request + "." + tableNamePackage); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (isData) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("import lombok.Data;"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | boolean hasBigDecimal = false; |
| | | List<String> listColumn = new ArrayList<>(); |
| | | List<String> listGetterSetter = new ArrayList<>(); |
| | | for (Map<String, Object> column : columns) { |
| | | String name = column.get("name").toString(); |
| | | String desc = String.valueOf(column.get("description")); |
| | | String type = column.get("type").toString(); |
| | | int length = NumericUtil.tryParseInt(column.get("length"), 0); |
| | | String defValue = String.valueOf(column.get("default")); |
| | | int isPK = NumericUtil.tryParseInt(column.get("isPK"), 0); |
| | | int isIdentity = NumericUtil.tryParseInt(column.get("isIdentity"), 0); |
| | | int isNullable = NumericUtil.tryParseInt(column.get("isNullable"), 0); |
| | | |
| | | String javaType = getJavaType(type); |
| | | if ("BigDecimal".equalsIgnoreCase(javaType)) { |
| | | hasBigDecimal = true; |
| | | } |
| | | String propName = underline2Camel(name); |
| | | String columnNameUpper = underline2Camel(name, true); |
| | | |
| | | // 主键和自增列不加入新增接口的入参 |
| | | if (GenCodeMysql.IsNoModify(name)) { |
| | | continue; |
| | | } |
| | | listColumn.add(SET_PRO_DESC_TPL.replaceAll("\\{\\#\\=desc\\}", desc)); |
| | | //追加列 |
| | | listColumn.add(String.format("\tprivate %s %s;", javaType, propName)); |
| | | |
| | | listGetterSetter.add(""); |
| | | listGetterSetter.add(String.format("\tpublic %s get%s() {", javaType, columnNameUpper)); |
| | | listGetterSetter.add(String.format("\t\treturn this.%s;", propName)); |
| | | listGetterSetter.add("\t}"); |
| | | listGetterSetter.add(String.format("\tpublic void set%s(%s %s) {", columnNameUpper, javaType, propName)); |
| | | listGetterSetter.add(String.format("\t\tthis.%s = %s;", propName, propName)); |
| | | listGetterSetter.add("\t}"); |
| | | } |
| | | if (hasBigDecimal) { |
| | | content.append("import java.math.BigDecimal;"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (isData) { |
| | | content.append("@Data"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("public class "); |
| | | content.append(modifyDTOName); |
| | | content.append(" {"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(StringUtil.join(listColumn, ConstantFactory.STR_NEWLINE)); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(StringUtil.join(listGetterSetter, ConstantFactory.STR_NEWLINE)); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.RequestDTO + tableNamePackage + "/" + modifyDTOName + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.RequestDTO + tableNamePackage + "/", modifyDTOName + ".java", content.toString()); |
| | | } |
| | | |
| | | private static void genConvertMapper(String tableName, String tableDesc) { |
| | | String modelName = underline2Camel(tableName, true); |
| | | String poName = modelName + SuffixSet.PO; |
| | | String voName = modelName + SuffixSet.VO; |
| | | String createDTOName = SuffixSet.Create + modelName; |
| | | String modifyDTOName = SuffixSet.Modify + modelName; |
| | | String convertMapperName = modelName + "Mapper"; |
| | | String tableNamePackage = tableName.toLowerCase().replaceAll("_", ""); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.CONVERT_MAPPER + ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | |
| | | content.append("import "); |
| | | content.append(PackageSet.Request + "." + tableNamePackage + "."); |
| | | content.append(createDTOName + ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | |
| | | content.append("import "); |
| | | content.append(PackageSet.Request + "." + tableNamePackage + "."); |
| | | content.append(modifyDTOName + ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | |
| | | content.append("import "); |
| | | content.append(PackageSet.PO + "."); |
| | | content.append(poName + ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | |
| | | content.append("import "); |
| | | content.append(PackageSet.VO + "."); |
| | | content.append(voName + ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | |
| | | if (SET_CONVERT_IMPORT.length > 0) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | for (String s : SET_CONVERT_IMPORT) { |
| | | content.append(s); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | } |
| | | |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("@Mapper"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("public interface "); |
| | | content.append(convertMapperName); |
| | | content.append(" {"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" " + convertMapperName + " INSTANCE = Mappers.getMapper(" + convertMapperName + ".class);\n" + |
| | | "\n" + |
| | | " " + poName + " toCreate(" + createDTOName + " request);\n" + |
| | | " " + poName + " toModify(" + modifyDTOName + " request);\n" + |
| | | "\n" + |
| | | " " + voName + " toVo(" + poName + " item);\n" + |
| | | " List<" + voName + "> toVo(List<" + poName + "> list);"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.COMVERT_MAPPER + convertMapperName + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.COMVERT_MAPPER, convertMapperName + ".java", content.toString()); |
| | | } |
| | | |
| | | private static void genService(String tableName, String tableDesc) { |
| | | String modelName = underline2Camel(tableName, true); |
| | | String poName = modelName + SuffixSet.PO; |
| | | String serviceName = modelName + SuffixSet.SERVICE; |
| | | String daoName = modelName + SuffixSet.DAO; |
| | | String createDTOName = SuffixSet.Create + modelName; |
| | | String modifyDTOName = SuffixSet.Modify + modelName; |
| | | String responseDTOName = modelName + "VO"; |
| | | String searchName = "Search" + modelName; |
| | | String tableNamePackage = tableName.toLowerCase().replaceAll("_", ""); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.SERVICE); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (SET_Service_IMPORT.length > 0) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | for (String s : SET_Service_IMPORT) { |
| | | content.append(s); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | } |
| | | content.append("import "); |
| | | content.append(PackageSet.DAO); |
| | | content.append("."); |
| | | content.append(daoName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.PO); |
| | | content.append("."); |
| | | content.append(poName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.Request); |
| | | content.append("." + tableNamePackage + "."); |
| | | content.append(createDTOName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.Request); |
| | | content.append("." + tableNamePackage + "."); |
| | | content.append(modifyDTOName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.SEARCH); |
| | | content.append("."); |
| | | content.append(searchName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.VO); |
| | | content.append("."); |
| | | content.append(responseDTOName + ";\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("@Service\n"); |
| | | content.append("public class "); |
| | | content.append(serviceName); |
| | | content.append(" extends BaseService {\n"); |
| | | content.append(" @Autowired\n" + |
| | | " private " + daoName + " dao;\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" public ExecutedResult<Long> create(" + createDTOName + " request) {\n" + |
| | | " // 转换po\n" + |
| | | " " + poName + " item = CopierUtil.mapTo(request, " + poName + ".class);\n" + |
| | | " // 设置主键id\n" + |
| | | " //item.setId(SnowFlakeUtil.getId());\n" + |
| | | " // 设置状态\n" + |
| | | " //item.setStatus(EState.NORMAL.getValue());\n" + |
| | | " // 设置记录创建时间\n" + |
| | | " item.setCreateTime(LocalDateTimeUtil.nowTimeStamp());\n" + |
| | | " // 是否删除(逻辑删除)初始值\n" + |
| | | " //item.setIsDelete(EYesOrNo.NO.getValue());\n" + |
| | | "\n" + |
| | | " Boolean result = this.dao.add(item);\n" + |
| | | " if (BooleanUtils.isFalse(result)) {\n" + |
| | | " return ExecutedResult.failed(\"创建[" + tableDesc + "]失败。\");\n" + |
| | | " }\n" + |
| | | " return ExecutedResult.success(item.getId());\n" + |
| | | " }\n" + |
| | | "\n" + |
| | | " public ExecutedResult<String> modify(" + modifyDTOName + " request) {\n" + |
| | | " // 验证记录是否存在\n" + |
| | | " ExecutedResult<" + poName + "> checkExists = this.check4Id(request.getId());\n" + |
| | | " if (checkExists.isFailed()) {\n" + |
| | | " return ExecutedResult.failed(checkExists.getMsg());\n" + |
| | | " }\n" + |
| | | " // 转换po\n" + |
| | | " " + poName + " item = CopierUtil.mapTo(request, " + poName + ".class);\n" + |
| | | "\n" + |
| | | " Boolean result = this.dao.modify(item);\n" + |
| | | " if (BooleanUtils.isFalse(result)) {\n" + |
| | | " return ExecutedResult.failed(\"编辑[" + tableDesc + "]失败。\");\n" + |
| | | " }\n" + |
| | | " return ExecutedResult.success();\n" + |
| | | " }\n" + |
| | | "\n" + |
| | | " public ExecutedResult<" + responseDTOName + "> get(Long id) {\n" + |
| | | " " + responseDTOName + " result = new " + responseDTOName + "();\n" + |
| | | "\n" + |
| | | " " + poName + " find = dao.getById(id);\n" + |
| | | " if (null != find) {\n" + |
| | | " result = CopierUtil.mapTo(find, " + responseDTOName + ".class);\n" + |
| | | " }\n" + |
| | | " return ExecutedResult.success(result);\n" + |
| | | " }\n" + |
| | | "\n" + |
| | | "// public ExecutedResult<String> stop(Long id) {\n" + |
| | | "// // 验证记录是否存在\n" + |
| | | "// ExecutedResult<" + poName + "> checkExists = this.check4Id(id);\n" + |
| | | "// if (checkExists.isFailed()) {\n" + |
| | | "// return ExecutedResult.failed(checkExists.getMsg());\n" + |
| | | "// }\n" + |
| | | "// " + poName + " item = new " + poName + "();\n" + |
| | | "// item.setId(id);\n" + |
| | | "// item.setStatus(EState.DISABLED.getValue());\n" + |
| | | "//\n" + |
| | | "// Boolean result = this.dao.modify(item);\n" + |
| | | "// if (BooleanUtils.isFalse(result)) {\n" + |
| | | "// return ExecutedResult.failed(\"停用[" + tableDesc + "]失败。\");\n" + |
| | | "// }\n" + |
| | | "// return ExecutedResult.success();\n" + |
| | | "// }\n" + |
| | | "//\n" + |
| | | "// public ExecutedResult<String> enable(Long id) {\n" + |
| | | "// // 验证记录是否存在\n" + |
| | | "// ExecutedResult<" + poName + "> checkExists = this.check4Id(id);\n" + |
| | | "// if (checkExists.isFailed()) {\n" + |
| | | "// return ExecutedResult.failed(checkExists.getMsg());\n" + |
| | | "// }\n" + |
| | | "// " + poName + " item = new " + poName + "();\n" + |
| | | "// item.setId(id);\n" + |
| | | "// item.setStatus(EState.NORMAL.getValue());\n" + |
| | | "//\n" + |
| | | "// Boolean result = this.dao.modify(item);\n" + |
| | | "// if (BooleanUtils.isFalse(result)) {\n" + |
| | | "// return ExecutedResult.failed(\"启用[" + tableDesc + "]失败。\");\n" + |
| | | "// }\n" + |
| | | "// return ExecutedResult.success();\n" + |
| | | "// }\n" + |
| | | "//\n" + |
| | | "// public ExecutedResult<String> setSort(ReqSetSort request) {\n" + |
| | | "// // 验证记录是否存在\n" + |
| | | "// ExecutedResult<" + poName + "> checkExists = this.check4Id(request.getId());\n" + |
| | | "// if (checkExists.isFailed()) {\n" + |
| | | "// return ExecutedResult.failed(checkExists.getMsg());\n" + |
| | | "// }\n" + |
| | | "// " + poName + " item = new " + poName + "();\n" + |
| | | "// item.setId(request.getId());\n" + |
| | | "// item.setSort(request.getSort());\n" + |
| | | "//\n" + |
| | | "// Boolean result = this.dao.modify(item);\n" + |
| | | "// if (BooleanUtils.isFalse(result)) {\n" + |
| | | "// return ExecutedResult.failed(\"设置[" + tableDesc + "]排序值失败。\");\n" + |
| | | "// }\n" + |
| | | "// return ExecutedResult.success();\n" + |
| | | "// }\n" + |
| | | "//\n" + |
| | | "// public ExecutedResult<String> remove(Long id) {\n" + |
| | | "// Boolean result = this.dao.remove(id);\n" + |
| | | "// if (BooleanUtils.isFalse(result)) {\n" + |
| | | "// return ExecutedResult.failed(\"删除[" + tableDesc + "]失败。\");\n" + |
| | | "// }\n" + |
| | | "// return ExecutedResult.success();\n" + |
| | | "// }\n" + |
| | | "//\n" + |
| | | "// public ExecutedResult<String> removeList(List<Long> ids) {\n" + |
| | | "// Boolean result = this.dao.removeByListId(ids);\n" + |
| | | "// if (BooleanUtils.isFalse(result)) {\n" + |
| | | "// return ExecutedResult.failed(\"删除[" + tableDesc + "]失败。\");\n" + |
| | | "// }\n" + |
| | | "// return ExecutedResult.success();\n" + |
| | | "// }\n" + |
| | | "\n" + |
| | | " public ExecutedResult<List<" + responseDTOName + ">> getList(List<Long> listId) {\n" + |
| | | " List<" + responseDTOName + "> result = new ArrayList<>();\n" + |
| | | "\n" + |
| | | " List<" + poName + "> list = this.dao.getListById(listId);\n" + |
| | | " if (ListUtil.isNotNullOrEmpty(list)) {\n" + |
| | | " result = CopierUtil.mapTo(list, " + responseDTOName + ".class);\n" + |
| | | " }\n" + |
| | | " return ExecutedResult.success(result);\n" + |
| | | " }\n" + |
| | | "\n" + |
| | | " public ExecutedResult<PagerResult<" + responseDTOName + ">> search(" + searchName + " search) {\n" + |
| | | " // 处理创建时间范围-查询参数\n" + |
| | | " Tuple<String, String> createTimeRange = ParameterUtil.getTimeRange(search.getCreateTimeRange());\n" + |
| | | " if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem1())) {\n" + |
| | | " search.setCreateTimeStart(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem1()).getTime());\n" + |
| | | " }\n" + |
| | | " if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem2())) {\n" + |
| | | " search.setCreateTimeEnd(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem2()).getTime());\n" + |
| | | " }\n" + |
| | | "\n" + |
| | | " PagerResult<" + poName + "> pageList = dao.search(search);\n" + |
| | | " List<" + responseDTOName + "> listVo = new ArrayList<>();\n" + |
| | | " List<" + poName + "> list = pageList.getList();\n" + |
| | | " if (ListUtil.isNotNullOrEmpty(list)) {\n" + |
| | | " pageList.setLastId(list.get(list.size() - 1).getId());\n" + |
| | | " // 转换vo\n" + |
| | | " listVo = CopierUtil.mapTo(list, " + responseDTOName + ".class);\n" + |
| | | " }\n" + |
| | | " PagerResult<" + responseDTOName + "> result = new PagerResult<>(pageList.getLimit(), pageList.getPage(), pageList.getTotal(), listVo);\n" + |
| | | " result.setLastId(pageList.getLastId());\n" + |
| | | " return ExecutedResult.success(result);\n" + |
| | | " }\n" + |
| | | "\n" + |
| | | " protected ExecutedResult<" + poName + "> check4Id(Long id) {\n" + |
| | | " " + poName + " exists = dao.getById(id);\n" + |
| | | " if (Objects.isNull(exists)) {\n" + |
| | | " return ExecutedResult.failed(\"[" + tableDesc + "]不存在:\" + id);\n" + |
| | | " }\n" + |
| | | " return ExecutedResult.success(exists);\n" + |
| | | " }\n"); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.SERVICE + serviceName + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.SERVICE, serviceName + ".java", content.toString()); |
| | | } |
| | | |
| | | private static void genController(String tableName, String tableDesc) { |
| | | String apiName = underline2Camel(tableName, false); |
| | | String modelName = underline2Camel(tableName, true); |
| | | String poName = modelName + SuffixSet.PO; |
| | | String voName = modelName + SuffixSet.VO; |
| | | String serviceName = modelName + SuffixSet.SERVICE; |
| | | String ControllerName = modelName + SuffixSet.Controller; |
| | | String createDTOName = SuffixSet.Create + modelName; |
| | | String modifyDTOName = SuffixSet.Modify + modelName; |
| | | String responseDTOName = modelName + "VO"; |
| | | String searchName = "Search" + modelName; |
| | | String tableNamePackage = tableName.toLowerCase().replaceAll("_", ""); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append(SET_BEFORE_TEXT); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("package "); |
| | | content.append(PackageSet.Controller); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | if (SET_Controller_IMPORT.length > 0) { |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | for (String s : SET_Controller_IMPORT) { |
| | | content.append(s); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | } |
| | | } |
| | | content.append("import "); |
| | | content.append(PackageSet.SERVICE); |
| | | content.append("."); |
| | | content.append(serviceName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.Request); |
| | | content.append("." + tableNamePackage + "."); |
| | | content.append(createDTOName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.Request); |
| | | content.append("." + tableNamePackage + "."); |
| | | content.append(modifyDTOName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.SEARCH); |
| | | content.append("."); |
| | | content.append(searchName + ";\n"); |
| | | content.append("import "); |
| | | content.append(PackageSet.VO); |
| | | content.append("."); |
| | | content.append(responseDTOName + ";\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc) |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("@RestController\n" + |
| | | "@RequestMapping(value = \"" + apiName + "\")\n"); |
| | | content.append("public class "); |
| | | content.append(ControllerName); |
| | | content.append(" extends BasicController {\n"); |
| | | content.append(" @Autowired\n" + |
| | | " private " + serviceName + " service;\n"); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_METHOD_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", "创建[" + tableDesc + "]") |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" @PostMapping(value = \"create\")\n" + |
| | | " public ExecutedResult<Long> create(@RequestBody " + createDTOName + " request) {\n" + |
| | | " //#region 参数验证\n" + |
| | | " ParameterValidator validator = new ParameterValidator()\n" + |
| | | " // 非空\n" + |
| | | " //.addNotNullOrEmpty(ParameterUtil.named(\"名称\"), request.getName())\n" + |
| | | " // 限制最大长度\n" + |
| | | " //.addLengthMax(ParameterUtil.named(\"名称\"), request.getName(), ConstantFactory.LENGTH_MAX50)\n" + |
| | | " ;\n" + |
| | | " ParameterValidateResult result = validator.validate();\n" + |
| | | " if (result.getIsFiled()) {\n" + |
| | | " return failed(result.getErrorMsg());\n" + |
| | | " }\n" + |
| | | " //#endregion\n" + |
| | | " return this.service.create(request);\n" + |
| | | " }\n" + |
| | | "\n"); |
| | | content.append(SET_METHOD_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", "编辑[" + tableDesc + "]") |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" @PostMapping(value = \"modify\")\n" + |
| | | " public ExecutedResult<String> modify(@RequestBody " + modifyDTOName + " request) {\n" + |
| | | " //#region 参数验证\n" + |
| | | " ParameterValidator validator = new ParameterValidator()\n" + |
| | | " // 必须大于0\n" + |
| | | " .addGreater(ParameterUtil.named(\"[" + tableDesc + "]id\"), request.getId(), 0L)\n" + |
| | | " // 非空\n" + |
| | | " //.addNotNullOrEmpty(ParameterUtil.named(\"名称\"), request.getName())\n" + |
| | | " // 限制最大长度\n" + |
| | | " //.addLengthMax(ParameterUtil.named(\"名称\"), request.getName(), ConstantFactory.LENGTH_MAX50)\n" + |
| | | " ;\n" + |
| | | " ParameterValidateResult result = validator.validate();\n" + |
| | | " if (result.getIsFiled()) {\n" + |
| | | " return failed(result.getErrorMsg());\n" + |
| | | " }\n" + |
| | | " //#endregion\n" + |
| | | " return this.service.modify(request);\n" + |
| | | " }\n\n"); |
| | | content.append(SET_METHOD_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", "获取[" + tableDesc + "]") |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" @GetMapping(value = \"get\")\n" + |
| | | " public ExecutedResult<" + voName + "> get(@RequestParam Long id) {\n" + |
| | | " return this.service.get(id);\n" + |
| | | " }\n\n"); |
| | | content.append(SET_METHOD_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", "根据id批量获取[" + tableDesc + "]") |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" @PostMapping(value = \"getList\")\n" + |
| | | " public ExecutedResult<List<" + voName + ">> getList(@RequestBody ReqListId request) {\n" + |
| | | " return this.service.getList(request.getListId());\n" + |
| | | " }\n\n"); |
| | | content.append(SET_METHOD_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", "查询[" + tableDesc + "]") |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(" @PostMapping(value = \"search\")\n" + |
| | | " public ExecutedResult<PagerResult<" + voName + ">> search(@RequestBody " + searchName + " request) {\n" + |
| | | " return this.service.search(request);\n" + |
| | | " }\n"); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.Controller + ControllerName + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.Controller, ControllerName + ".java", content.toString()); |
| | | } |
| | | |
| | | private static String genResultMap(Map<String, String> mapResultMap, Map<String, String> mapResultMapDesc) { |
| | | List<String> result = new ArrayList<>(); |
| | | for (Map.Entry<String, String> entry : mapResultMap.entrySet()) { |
| | | String name = entry.getKey(); |
| | | result.add("\t\t<!-- " + mapResultMapDesc.get(entry.getKey()) + " -->"); |
| | | result.add("\t\t<result property=\"" + entry.getValue() + "\" column=\"" + name + "\" />"); |
| | | } |
| | | return StringUtil.join(result, ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | |
| | | //#region gen MyBatis mapper |
| | | |
| | | private static void genMapperXml(List<Map<String, Object>> columns, String tableDesc) { |
| | | if (ListUtil.isNullOrEmpty(columns)) { |
| | | return; |
| | | } |
| | | String tableName = columns.get(0).get("objName").toString(); |
| | | String modelName = underline2Camel(tableName, true); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + |
| | | "<!DOCTYPE mapper\n" + |
| | | " PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n" + |
| | | " \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n"); |
| | | content.append("<!-- "); |
| | | content.append(tableDesc); |
| | | content.append(" mapper -->\n"); |
| | | content.append(String.format("<mapper namespace=\"%s.%s%s\">\n", PackageSet.MAPPER, modelName, SuffixSet.MAPPER)); |
| | | |
| | | Map<String, String> mapResultMap = new HashMap<>(); |
| | | Map<String, String> mapResultMapDesc = new HashMap<>(); |
| | | for (Map<String, Object> column : columns) { |
| | | String name = column.get("name").toString(); |
| | | String desc = String.valueOf(column.get("description")); |
| | | |
| | | String propName = underline2Camel(name); |
| | | //忽略最后修改时间这一列的新增和编辑 |
| | | if ("datachange_lasttime".equalsIgnoreCase(name)) { |
| | | propName = "dataChangeLastTime"; |
| | | } |
| | | |
| | | mapResultMap.put(name, propName); |
| | | mapResultMapDesc.put(name, desc); |
| | | } |
| | | |
| | | //追加 resultMap |
| | | content.append(String.format("\t<resultMap type=\"%s.%s%s\" id=\"resultMap\">\n", PackageSet.PO, modelName, SuffixSet.PO)); |
| | | content.append(genResultMap(mapResultMap, mapResultMapDesc)); |
| | | content.append("\n\t</resultMap>\n\n"); |
| | | |
| | | //追加 add 的方法 |
| | | content.append(String.format("\t<insert id=\"add\" parameterType=\"%s.%s%s\" keyProperty=\"id\" useGeneratedKeys=\"true\">\n", PackageSet.PO, modelName, SuffixSet.PO)); |
| | | content.append(genInsert(tableName, mapResultMap, mapResultMapDesc)); |
| | | content.append("\n\t</insert>\n\n"); |
| | | |
| | | //追加 addBatch 的方法 |
| | | content.append(String.format("\t<insert id=\"addBatch\" parameterType=\"%s.%s%s\" useGeneratedKeys=\"true\" keyProperty=\"id\">\n", PackageSet.PO, modelName, SuffixSet.PO)); |
| | | content.append(genInsertBath(tableName, mapResultMap, mapResultMapDesc)); |
| | | content.append("\n\t</insert>\n\n"); |
| | | |
| | | //追加 update 的方法 |
| | | content.append(String.format("\t<update id=\"update\" parameterType=\"%s.%s%s\" keyProperty=\"id\">\n", PackageSet.PO, modelName, SuffixSet.PO)); |
| | | content.append(genUpdate(tableName, mapResultMap, mapResultMapDesc)); |
| | | content.append("\n\t</update>\n\n"); |
| | | |
| | | //追加 updateBatch 的方法 |
| | | content.append("\t<update id=\"updateBatch\" parameterType=\"java.util.List\">\n"); |
| | | content.append(genUpdateBatch(tableName, mapResultMap, mapResultMapDesc)); |
| | | content.append("\n\t</update>\n\n"); |
| | | |
| | | //追加 delete 的方法 |
| | | content.append("\t<delete id=\"delete\">\n"); |
| | | content.append("\t\tDELETE FROM `"); |
| | | content.append(tableName); |
| | | content.append("` WHERE `id` = #{value}\n"); |
| | | content.append("\t</delete>\n\n"); |
| | | |
| | | //追加 deleteBatch 的方法 |
| | | content.append("\t<delete id=\"deleteBatch\" parameterType=\"java.util.List\">\n"); |
| | | content.append("\t\tDELETE FROM `"); |
| | | content.append(tableName); |
| | | content.append("` WHERE `id` IN"); |
| | | content.append("\t\t<!-- “collection”固定值为list,“item”为循环变量取的别名 -->\n"); |
| | | content.append("\t\t<foreach collection=\"list\" item=\"id\" open=\"(\" separator=\",\" close=\")\">\n"); |
| | | content.append("\t\t\t#{id}\n"); |
| | | content.append("\t\t</foreach>\n"); |
| | | content.append("\t</delete>\n\n"); |
| | | |
| | | //追加 get 的方法 |
| | | content.append("\t<select id=\"get\" resultMap=\"resultMap\">\n"); |
| | | content.append(genGet(tableName, mapResultMap, mapResultMapDesc)); |
| | | content.append("\n\t</select>\n\n"); |
| | | |
| | | //追加 getCount 的方法 |
| | | content.append(String.format("\t<select id=\"getCount\" resultType=\"Long\" parameterType=\"%s.Search%s\">\n", PackageSet.SEARCH, modelName)); |
| | | content.append(genGetCount(tableName)); |
| | | content.append("\n\t</select>\n\n"); |
| | | |
| | | //追加 getList 的方法 |
| | | content.append(String.format("\t<select id=\"getList\" resultMap=\"resultMap\" parameterType=\"%s.Search%s\">\n", PackageSet.SEARCH, modelName)); |
| | | content.append(genGetList(tableName, mapResultMap, mapResultMapDesc)); |
| | | content.append("\n\t</select>\n\n"); |
| | | |
| | | //追加 getListBySql 的方法 |
| | | content.append("\t<select id=\"getListBySql\" parameterType=\"java.util.Map\" resultType=\"HashMap\">\n"); |
| | | content.append("\t\t${sql}\n"); |
| | | content.append("\t</select>\n"); |
| | | |
| | | content.append("</mapper>"); |
| | | |
| | | if (new File(OutSet.MAPPERXML + modelName + SuffixSet.MAPPER + ".xml").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.MAPPERXML, modelName + SuffixSet.MAPPER + ".xml", content.toString()); |
| | | } |
| | | |
| | | private static void genMapper(List<Map<String, Object>> columns, String tableDesc) { |
| | | if (ListUtil.isNullOrEmpty(columns)) { |
| | | return; |
| | | } |
| | | String tableName = columns.get(0).get("objName").toString(); |
| | | String modelName = underline2Camel(tableName, true); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("package "); |
| | | content.append(PackageSet.MAPPER); |
| | | content.append(ConstantFactory.STR_SEMICOLON); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(String.format("import %s.%s%s;", PackageSet.PO, modelName, SuffixSet.PO)); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(SET_CLASS_DESC_TPL |
| | | .replaceAll("\\{\\#\\=desc\\}", tableDesc + " mapper") |
| | | .replaceAll("\\{\\#\\=author\\}", AUTHOR) |
| | | .replaceAll("\\{\\#\\=date\\}", LocalDateTimeUtil.todayStr().replaceAll("\\-", "/")) |
| | | ); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append(String.format("public interface %s%s extends BasicMapper<%s%s> {", modelName, SuffixSet.MAPPER, modelName, SuffixSet.PO)); |
| | | content.append(ConstantFactory.STR_NEWLINE); |
| | | content.append("}"); |
| | | |
| | | if (new File(OutSet.MAPPER + modelName + SuffixSet.MAPPER + ".java").exists()) { |
| | | return; |
| | | } |
| | | writeFile(OutSet.MAPPER, modelName + SuffixSet.MAPPER + ".java", content.toString()); |
| | | } |
| | | |
| | | private static String genInsert(String tableName, Map<String, String> mapResultMap, Map<String, String> mapResultMapDesc) { |
| | | List<String> result = new ArrayList<>(); |
| | | result.add("\t\tINSERT INTO `" + tableName + "`("); |
| | | |
| | | List<String> listColumn = new ArrayList<>(); |
| | | List<String> listValue = new ArrayList<>(); |
| | | for (Map.Entry<String, String> entry : mapResultMap.entrySet()) { |
| | | String name = entry.getKey(); |
| | | if (NO_CREATE_COLUMNS.stream().anyMatch(c -> c.equalsIgnoreCase(name))) { |
| | | continue; |
| | | } |
| | | listColumn.add("\t\t<!-- " + mapResultMapDesc.get(entry.getKey()) + " -->\n\t\t`" + name + "`"); |
| | | |
| | | listValue.add("\t\t#{" + entry.getValue() + "}"); |
| | | } |
| | | result.add(StringUtil.join(listColumn, ",\r\n")); |
| | | result.add("\t\t) VALUES("); |
| | | result.add(StringUtil.join(listValue, ",\r\n")); |
| | | result.add("\t);"); |
| | | return StringUtil.join(result, ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | private static String genInsertBath(String tableName, Map<String, String> mapResultMap, Map<String, String> mapResultMapDesc) { |
| | | List<String> result = new ArrayList<>(); |
| | | result.add("\t\tINSERT INTO `" + tableName + "`("); |
| | | |
| | | List<String> listColumn = new ArrayList<>(); |
| | | List<String> listValue = new ArrayList<>(); |
| | | for (Map.Entry<String, String> entry : mapResultMap.entrySet()) { |
| | | String name = entry.getKey(); |
| | | if (NO_CREATE_COLUMNS.stream().anyMatch(c -> c.equalsIgnoreCase(name))) { |
| | | continue; |
| | | } |
| | | listColumn.add("\t\t<!-- " + mapResultMapDesc.get(entry.getKey()) + " -->\n\t\t`" + name + "`"); |
| | | |
| | | listValue.add("\t\t\t#{item." + entry.getValue() + "}"); |
| | | } |
| | | result.add(StringUtil.join(listColumn, ",\r\n")); |
| | | result.add("\t\t) VALUES("); |
| | | result.add("\t\t<!-- “collection”固定值为list,“item”为循环变量取的别名 -->"); |
| | | result.add("\t\t<foreach collection=\"list\" item=\"item\" separator=\",\">"); |
| | | result.add("\t\t\t("); |
| | | result.add(StringUtil.join(listValue, ",\r\n")); |
| | | result.add("\t\t\t)"); |
| | | result.add("\t\t</foreach>"); |
| | | result.add("\t\t);"); |
| | | return StringUtil.join(result, ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | private static String genUpdate(String tableName, Map<String, String> mapResultMap, Map<String, String> mapResultMapDesc) { |
| | | List<String> result = new ArrayList<>(); |
| | | result.add("\t\tUPDATE `" + tableName + "`"); |
| | | result.add("\t\t<trim prefix=\"SET\" suffixOverrides=\",\">"); |
| | | for (Map.Entry<String, String> entry : mapResultMap.entrySet()) { |
| | | String name = entry.getKey(); |
| | | String propName = entry.getValue(); |
| | | if (NO_UPDATE_COLUMNS.stream().anyMatch(c -> c.equalsIgnoreCase(name))) { |
| | | continue; |
| | | } |
| | | result.add("\t\t\t<!-- " + mapResultMapDesc.get(entry.getKey()) + " -->"); |
| | | result.add("\t\t\t<if test=\"null != " + propName + "\">`" + name + "` = #{" + propName + "},</if>"); |
| | | } |
| | | result.add("\t\t</trim>"); |
| | | result.add("\t\tWHERE `id` = #{id}"); |
| | | return StringUtil.join(result, ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | private static String genUpdateBatch(String tableName, Map<String, String> mapResultMap, Map<String, String> mapResultMapDesc) { |
| | | List<String> result = new ArrayList<>(); |
| | | result.add("\t\t<!-- “collection”固定值为list,“item”为循环变量取的别名 -->"); |
| | | result.add("\t\t<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"\" close=\"\" separator=\";\">"); |
| | | result.add("\t\t\tUPDATE `" + tableName + "`"); |
| | | result.add("\t\t\t<trim prefix=\"SET\" suffixOverrides=\",\">"); |
| | | for (Map.Entry<String, String> entry : mapResultMap.entrySet()) { |
| | | String name = entry.getKey(); |
| | | String propName = entry.getValue(); |
| | | if (NO_UPDATE_COLUMNS.stream().anyMatch(c -> c.equalsIgnoreCase(name))) { |
| | | continue; |
| | | } |
| | | result.add("\t\t\t\t<!-- " + mapResultMapDesc.get(entry.getKey()) + " -->"); |
| | | result.add("\t\t\t\t<if test=\"null != item." + propName + "\">`" + name + "` = #{item." + propName + "},</if>"); |
| | | } |
| | | result.add("\t\t\t</trim>"); |
| | | result.add("\t\t\tWHERE `id` = #{id}"); |
| | | result.add("\t\t</foreach>"); |
| | | return StringUtil.join(result, ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | private static String genGet(String tableName, Map<String, String> mapResultMap, Map<String, String> mapResultMapDesc) { |
| | | List<String> result = new ArrayList<>(); |
| | | result.add("\t\t<![CDATA[SELECT"); |
| | | |
| | | List<String> listColumn = new ArrayList<>(); |
| | | for (Map.Entry<String, String> entry : mapResultMap.entrySet()) { |
| | | String name = entry.getKey(); |
| | | listColumn.add("\t\t\t`" + name + "`"); |
| | | } |
| | | result.add(StringUtil.join(listColumn, ",\r\n")); |
| | | result.add("\t\tFROM `" + tableName + "`]]>"); |
| | | result.add("\t\tWHERE `id` = #{id}"); |
| | | return StringUtil.join(result, ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | private static String genGetCount(String tableName) { |
| | | List<String> result = new ArrayList<>(); |
| | | result.add("\t\t<![CDATA[SELECT COUNT(*) FROM `" + tableName + "`]]>"); |
| | | result.add("\t\t<!-- 加上“WHERE”前缀,去掉第一个“AND” -->"); |
| | | result.add("\t\t<trim prefix=\"WHERE\" prefixOverrides=\"AND \">"); |
| | | |
| | | result.add("\t\t\t<!-- 自增id -->"); |
| | | result.add("\t\t\t<if test=\"null != id and id > 0\">"); |
| | | result.add("\t\t\t\tAND `id` = #{id}"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 自增id列表 -->"); |
| | | result.add("\t\t\t<if test=\"null != listId and listId.size() > 0\">"); |
| | | result.add("\t\t\t\tAND `id` IN"); |
| | | result.add("\t\t\t\t<foreach collection=\"listId\" item=\"item\" open=\"(\" separator=\",\" close=\")\">"); |
| | | result.add("\t\t\t\t\t#{item}"); |
| | | result.add("\t\t\t\t</foreach>"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 数据状态 -->"); |
| | | result.add("\t\t\t<if test=\"null != status and status > 0\">"); |
| | | result.add("\t\t\t\tAND `status` = #{status}"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 数据状态列表 -->"); |
| | | result.add("\t\t\t<if test=\"null != listStatus and listStatus.size() > 0\">"); |
| | | result.add("\t\t\t\tAND `status` IN"); |
| | | result.add("\t\t\t\t<foreach collection=\"listStatus\" item=\"item\" open=\"(\" separator=\",\" close=\")\">"); |
| | | result.add("\t\t\t\t\t#{item}"); |
| | | result.add("\t\t\t\t</foreach>"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 关键字模糊搜索 -->"); |
| | | result.add("\t\t\t<!-- <if test=\"null != keywords and '' != keywords.trim()\">"); |
| | | result.add("\t\t\t\tAND (`user_name` LIKE #{keywords} OR `nick_name` LIKE #{keywords})"); |
| | | result.add("\t\t\t</if> -->"); |
| | | |
| | | result.add("\t\t\t<!-- 数据创建时间-起始 -->"); |
| | | result.add("\t\t\t<if test=\"null != createTimeStart and createTimeStart > 0\">"); |
| | | result.add("\t\t\t\tAND `create_time` >= #{createTimeStart}"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 数据创建时间-截止 -->"); |
| | | result.add("\t\t\t<if test=\"null != createTimeEnd and createTimeEnd > 0\">"); |
| | | result.add("\t\t\t\tAND `create_time` <= #{createTimeEnd}"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t</trim>"); |
| | | return StringUtil.join(result, ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | private static String genGetList(String tableName, Map<String, String> mapResultMap, Map<String, String> mapResultMapDesc) { |
| | | List<String> result = new ArrayList<>(); |
| | | result.add("\t\t<![CDATA[SELECT"); |
| | | |
| | | List<String> listColumn = new ArrayList<>(); |
| | | for (Map.Entry<String, String> entry : mapResultMap.entrySet()) { |
| | | String name = entry.getKey(); |
| | | listColumn.add("\t\t`" + name + "`"); |
| | | } |
| | | result.add(StringUtil.join(listColumn, ",\r\n")); |
| | | result.add("\t\tFROM `" + tableName + "`]]>"); |
| | | result.add("\t\t<!-- 加上“WHERE”前缀,去掉第一个“AND” -->"); |
| | | result.add("\t\t<trim prefix=\"WHERE\" prefixOverrides=\"AND \">"); |
| | | |
| | | result.add("\t\t\t<!-- 自增id -->"); |
| | | result.add("\t\t\t<if test=\"null != id and id > 0\">"); |
| | | result.add("\t\t\t\tAND `id` = #{id}"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 自增id列表 -->"); |
| | | result.add("\t\t\t<if test=\"null != listId and listId.size() > 0\">"); |
| | | result.add("\t\t\t\tAND `id` IN"); |
| | | result.add("\t\t\t\t<foreach collection=\"listId\" item=\"item\" open=\"(\" separator=\",\" close=\")\">"); |
| | | result.add("\t\t\t\t\t#{item}"); |
| | | result.add("\t\t\t\t</foreach>"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 数据状态 -->"); |
| | | result.add("\t\t\t<if test=\"null != status and status > 0\">"); |
| | | result.add("\t\t\t\tAND `status` = #{status}"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 数据状态列表 -->"); |
| | | result.add("\t\t\t<if test=\"null != listStatus and listStatus.size() > 0\">"); |
| | | result.add("\t\t\t\tAND `status` IN"); |
| | | result.add("\t\t\t\t<foreach collection=\"listStatus\" item=\"item\" open=\"(\" separator=\",\" close=\")\">"); |
| | | result.add("\t\t\t\t\t#{item}"); |
| | | result.add("\t\t\t\t</foreach>"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 关键字模糊搜索 -->"); |
| | | result.add("\t\t\t<!-- <if test=\"null != keywords and '' != keywords.trim()\">"); |
| | | result.add("\t\t\t\tAND (`user_name` LIKE #{keywords} OR `nick_name` LIKE #{keywords})"); |
| | | result.add("\t\t\t</if> -->"); |
| | | |
| | | result.add("\t\t\t<!-- 数据创建时间-起始 -->"); |
| | | result.add("\t\t\t<if test=\"null != createTimeStart and createTimeStart > 0\">"); |
| | | result.add("\t\t\t\tAND `create_time` >= #{createTimeStart}"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t\t<!-- 数据创建时间-截止 -->"); |
| | | result.add("\t\t\t<if test=\"null != createTimeEnd and createTimeEnd > 0\">"); |
| | | result.add("\t\t\t\tAND `create_time` <= #{createTimeEnd}"); |
| | | result.add("\t\t\t</if>"); |
| | | |
| | | result.add("\t\t</trim>"); |
| | | result.add("\t\t<choose>"); |
| | | result.add("\t\t\t<when test=\"null!=orderBy and orderBy.trim()!=''\">"); |
| | | result.add("\t\t\t\tORDER BY ${orderBy}"); |
| | | result.add("\t\t\t</when>"); |
| | | result.add("\t\t\t<otherwise>"); |
| | | result.add("\t\t\t\tORDER BY id DESC"); |
| | | result.add("\t\t\t</otherwise>"); |
| | | result.add("\t\t</choose>"); |
| | | result.add("\t\t<if test=\"null!=pageSize and pageSize>0 and null!=lastRowNo and lastRowNo>=0\">"); |
| | | result.add("\t\t\tLIMIT ${lastRowNo}, ${pageSize}"); |
| | | result.add("\t\t</if>"); |
| | | return StringUtil.join(result, ConstantFactory.STR_NEWLINE); |
| | | } |
| | | |
| | | //#endregion |
| | | |
| | | |
| | | public static Map<String, String> getTables(String dbSchema) { |
| | | Map<String, String> result = new LinkedHashMap<>(); |
| | | String sql = " SELECT TABLE_NAME ,TABLE_COMMENT " + |
| | | " FROM information_schema.`TABLES` " + |
| | | " WHERE TABLE_SCHEMA = ? "; |
| | | try (PreparedStatement pStmt = CONN.prepareStatement(sql)) { |
| | | pStmt.setString(1, dbSchema); |
| | | try (ResultSet rs = pStmt.executeQuery()) { |
| | | while (rs.next()) { |
| | | String tableName = rs.getString("TABLE_NAME"); |
| | | String tableComment = rs.getString("TABLE_COMMENT"); |
| | | result.put(tableName, tableComment); |
| | | } |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static List<Map<String, Object>> getColumns(String tableName, String dbSchema) { |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | String sql = "SELECT `TABLE_NAME` AS'objName', `COLUMN_NAME`AS'name', `DATA_TYPE` AS'type', `CHARACTER_MAXIMUM_LENGTH` AS'length', `COLUMN_COMMENT` AS'description',\n" + |
| | | "`COLUMN_DEFAULT` AS'default',NUMERIC_PRECISION AS'numeric_precision',NUMERIC_SCALE AS'numeric_scale',DATETIME_PRECISION AS'datetime_precision',\n" + |
| | | "CASE `EXTRA` WHEN 'auto_increment' THEN CONVERT(1,UNSIGNED) ELSE CONVERT(0,UNSIGNED) END AS'isIdentity',\n" + |
| | | "CASE `COLUMN_KEY` WHEN 'PRI' THEN CONVERT(1,UNSIGNED) ELSE CONVERT(0,UNSIGNED) END AS'isPK', \n" + |
| | | "CASE `IS_NULLABLE` WHEN 'YES' THEN CONVERT(1,UNSIGNED) ELSE CONVERT(0,UNSIGNED) END AS'isNullable'\n" + |
| | | "FROM `information_schema`.`COLUMNS`\n" + |
| | | "WHERE `table_name` = ? AND `table_schema` = ?; "; |
| | | try (PreparedStatement pStmt = CONN.prepareStatement(sql)) { |
| | | pStmt.setString(1, tableName); |
| | | pStmt.setString(2, dbSchema); |
| | | try (ResultSet rs = pStmt.executeQuery()) { |
| | | //获取键名 |
| | | ResultSetMetaData md = rs.getMetaData(); |
| | | //获取列的数量 |
| | | int columnCount = md.getColumnCount(); |
| | | while (rs.next()) { |
| | | Map<String, Object> column = new HashMap<>(); |
| | | for (int i = 1; i <= columnCount; i++) { |
| | | String keyName = md.getColumnLabel(i); |
| | | Object value = rs.getObject(i); |
| | | column.put(keyName, value); |
| | | } |
| | | result.add(column); |
| | | } |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static List<Map<String, Object>> getListAll(String sql) { |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | try (PreparedStatement pStmt = CONN.prepareStatement(sql)) { |
| | | try (ResultSet rs = pStmt.executeQuery()) { |
| | | //获取键名 |
| | | ResultSetMetaData md = rs.getMetaData(); |
| | | //获取列的数量 |
| | | int columnCount = md.getColumnCount(); |
| | | while (rs.next()) { |
| | | Map<String, Object> column = new HashMap<>(); |
| | | for (int i = 1; i <= columnCount; i++) { |
| | | String keyName = md.getColumnLabel(i); |
| | | Object value = rs.getObject(i); |
| | | column.put(keyName, value); |
| | | } |
| | | result.add(column); |
| | | } |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static String getJavaType(String typeName) { |
| | | String result = "String"; |
| | | switch (typeName) { |
| | | case "int": |
| | | case "tinyint": |
| | | result = "Integer"; |
| | | break; |
| | | case "char": |
| | | case "varchar": |
| | | case "nvarchar": |
| | | case "text": |
| | | result = "String"; |
| | | break; |
| | | case "datetime": |
| | | case "timestamp": |
| | | result = "Timestamp"; |
| | | break; |
| | | case "bit": |
| | | result = "Boolean"; |
| | | break; |
| | | case "numeric": |
| | | case "decimal": |
| | | result = "BigDecimal"; |
| | | break; |
| | | case "bigint": |
| | | result = "Long"; |
| | | break; |
| | | case "date": |
| | | result = "Date"; |
| | | break; |
| | | |
| | | default: |
| | | break; |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static String underline2Camel(String line, boolean... firstIsUpperCase) { |
| | | String str = ""; |
| | | if (StringUtil.isNullOrEmpty(line)) { |
| | | return str; |
| | | } else { |
| | | StringBuilder sb = new StringBuilder(); |
| | | String[] strArr; |
| | | // 不包含下划线,且第二个参数是空的 |
| | | if (!line.contains("_") && firstIsUpperCase.length == 0) { |
| | | sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1)); |
| | | str = sb.toString(); |
| | | } else if (!line.contains("_") && firstIsUpperCase.length != 0) { |
| | | if (!firstIsUpperCase[0]) { |
| | | sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1)); |
| | | str = sb.toString(); |
| | | } else { |
| | | sb.append(line.substring(0, 1).toUpperCase()).append(line.substring(1)); |
| | | str = sb.toString(); |
| | | } |
| | | } else if (line.contains("_") && firstIsUpperCase.length == 0) { |
| | | strArr = line.split("_"); |
| | | for (String s : strArr) { |
| | | sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1)); |
| | | } |
| | | str = sb.toString(); |
| | | str = str.substring(0, 1).toLowerCase() + str.substring(1); |
| | | } else if (line.contains("_") && firstIsUpperCase.length != 0) { |
| | | strArr = line.split("_"); |
| | | for (String s : strArr) { |
| | | sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1)); |
| | | } |
| | | if (!firstIsUpperCase[0]) { |
| | | str = sb.toString(); |
| | | str = str.substring(0, 1).toLowerCase() + str.substring(1); |
| | | } else { |
| | | str = sb.toString(); |
| | | } |
| | | } |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | private static void writeFile(String path, String fileName, String content) { |
| | | File directory = new File(path); |
| | | //如果路径不存在,新建 |
| | | if (!directory.exists() && !directory.isDirectory()) { |
| | | directory.mkdirs(); |
| | | } |
| | | File columnFile = new File(path + fileName); |
| | | try { |
| | | columnFile.createNewFile(); |
| | | try (FileWriter fw = new FileWriter(columnFile.getAbsolutePath()); |
| | | PrintWriter pw = new PrintWriter(fw)) { |
| | | pw.println(content); |
| | | pw.flush(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public static Map<String, String> getPK(String tableName) { |
| | | Map<String, String> result = new LinkedHashMap<>(); |
| | | String sql = "SHOW INDEX FROM ? WHERE Key_name='PRIMARY'"; |
| | | try (PreparedStatement pStmt = CONN.prepareStatement(sql)) { |
| | | pStmt.setString(1, tableName); |
| | | try (ResultSet rs = pStmt.executeQuery()) { |
| | | while (rs.next()) { |
| | | String Key_name = rs.getString("Key_name"); |
| | | String Column_name = rs.getString("Column_name"); |
| | | result.put(Key_name, Column_name); |
| | | } |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static Map<String, String> getIndex(String tableName) { |
| | | Map<String, String> result = new LinkedHashMap<>(); |
| | | String sql = "SHOW INDEX FROM ? WHERE Key_name!='PRIMARY'"; |
| | | try (PreparedStatement pStmt = CONN.prepareStatement(sql)) { |
| | | pStmt.setString(1, tableName); |
| | | try (ResultSet rs = pStmt.executeQuery()) { |
| | | while (rs.next()) { |
| | | String Key_name = rs.getString("Key_name"); |
| | | String Column_name = rs.getString("Column_name"); |
| | | result.put(Key_name, Column_name); |
| | | } |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | } |