ruoyi-common/src/main/java/com/ruoyi/common/helper/DataBaseHelper.java
@@ -23,7 +23,7 @@ /** * è·åå½åæ°æ®åºç±»å */ public static DataBaseType getDataBasyType() { public static DataBaseType getDataBaseType() { DynamicRoutingDataSource ds = (DynamicRoutingDataSource) SpringUtils.getBean(DataSource.class); DataSource dataSource = ds.determineDataSource(); try { @@ -36,23 +36,23 @@ } public static boolean isMySql() { return DataBaseType.MY_SQL == getDataBasyType(); return DataBaseType.MY_SQL == getDataBaseType(); } public static boolean isOracle() { return DataBaseType.ORACLE == getDataBasyType(); return DataBaseType.ORACLE == getDataBaseType(); } public static boolean isPostgerSql() { return DataBaseType.POSTGRE_SQL == getDataBasyType(); return DataBaseType.POSTGRE_SQL == getDataBaseType(); } public static boolean isSqlServer() { return DataBaseType.SQL_SERVER == getDataBasyType(); return DataBaseType.SQL_SERVER == getDataBaseType(); } public static String findInSet(Object var1, String var2) { DataBaseType dataBasyType = getDataBasyType(); DataBaseType dataBasyType = getDataBaseType(); String var = Convert.toStr(var1); if (dataBasyType == DataBaseType.SQL_SERVER) { // charindex(',100,' , ',0,100,101,') <> 0 ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java
@@ -1,8 +1,6 @@ package com.ruoyi.generator.mapper; import com.baomidou.mybatisplus.annotation.InterceptorIgnore; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.core.mapper.BaseMapperPlus; import com.ruoyi.generator.domain.GenTable; @@ -18,15 +16,15 @@ @InterceptorIgnore(dataPermission = "true") public interface GenTableMapper extends BaseMapperPlus<GenTableMapper, GenTable, GenTable> { Page<GenTable> selectPageDbTableList(@Param("page") Page<GenTable> page, @Param(Constants.WRAPPER) Wrapper<Object> queryWrapper); Page<GenTable> selectPageDbTableList(@Param("page") Page<GenTable> page, @Param("genTable") GenTable genTable); /** * æ¥è¯¢æ®åºå表 * * @param queryWrapper æ¥è¯¢æ¡ä»¶ * @param genTable æ¥è¯¢æ¡ä»¶ * @return æ°æ®åºè¡¨éå */ List<GenTable> selectDbTableList(@Param(Constants.WRAPPER) Wrapper<Object> queryWrapper); List<GenTable> selectDbTableList(GenTable genTable); /** * æ¥è¯¢æ®åºå表 ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/OracleGenTableColumnMapper.java
ÎļþÒÑɾ³ý ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/OracleGenTableMapper.java
ÎļþÒÑɾ³ý ruoyi-generator/src/main/java/com/ruoyi/generator/service/BaseGenTableServiceImpl.java
ÎļþÒÑɾ³ý ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java
@@ -2,12 +2,15 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.IoUtil; import com.baomidou.mybatisplus.core.conditions.Wrapper; import cn.hutool.core.lang.Dict; import cn.hutool.core.util.ObjectUtil; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.GenConstants; import com.ruoyi.common.core.domain.PageQuery; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.exception.ServiceException; @@ -29,6 +32,8 @@ import org.apache.velocity.app.Velocity; import org.springframework.stereotype.Service; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; @@ -43,10 +48,11 @@ * * @author Lion Li */ @DS("#header.datasource") @Slf4j @RequiredArgsConstructor @Service public class GenTableServiceImpl extends BaseGenTableServiceImpl { public class GenTableServiceImpl implements IGenTableService { private final GenTableMapper baseMapper; private final GenTableColumnMapper genTableColumnMapper; @@ -107,7 +113,7 @@ @Override public TableDataInfo<GenTable> selectPageDbTableList(GenTable genTable, PageQuery pageQuery) { Page<GenTable> page = baseMapper.selectPageDbTableList(pageQuery.build(), this.buildDbTableQueryWrapper(genTable)); Page<GenTable> page = baseMapper.selectPageDbTableList(pageQuery.build(), genTable); return TableDataInfo.build(page); } @@ -119,22 +125,7 @@ */ @Override public List<GenTable> selectDbTableList(GenTable genTable) { return baseMapper.selectDbTableList(this.buildDbTableQueryWrapper(genTable)); } private Wrapper<Object> buildDbTableQueryWrapper(GenTable genTable) { Map<String, Object> params = genTable.getParams(); QueryWrapper<Object> wrapper = Wrappers.query(); wrapper.apply("table_schema = (select database())") .notLike("table_name", "xxl_job_") .notLike("table_name", "gen_") .notInSql("table_name", "select table_name from gen_table") .like(StringUtils.isNotBlank(genTable.getTableName()), "lower(table_name)", StringUtils.lowerCase(genTable.getTableName())) .like(StringUtils.isNotBlank(genTable.getTableComment()), "lower(table_comment)", StringUtils.lowerCase(genTable.getTableComment())) .between(params.get("beginTime") != null && params.get("endTime") != null, "create_time", params.get("beginTime"), params.get("endTime")) .orderByDesc("create_time"); return wrapper; return baseMapper.selectDbTableList(genTable); } /** @@ -252,6 +243,21 @@ } /** * çæä»£ç ï¼ä¸è½½æ¹å¼ï¼ * * @param tableName 表åç§° * @return æ°æ® */ @Override public byte[] downloadCode(String tableName) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); generatorCode(tableName, zip); IoUtil.close(zip); return outputStream.toByteArray(); } /** * çæä»£ç ï¼èªå®ä¹è·¯å¾ï¼ * * @param tableName 表åç§° @@ -339,10 +345,26 @@ } /** * æ¥è¯¢è¡¨ä¿¡æ¯å¹¶çæä»£ç * æ¹éçæä»£ç ï¼ä¸è½½æ¹å¼ï¼ * * @param tableNames 表æ°ç» * @return æ°æ® */ @Override public void generatorCode(String tableName, ZipOutputStream zip) { public byte[] downloadCode(String[] tableNames) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); for (String tableName : tableNames) { generatorCode(tableName, zip); } IoUtil.close(zip); return outputStream.toByteArray(); } /** * æ¥è¯¢è¡¨ä¿¡æ¯å¹¶çæä»£ç */ private void generatorCode(String tableName, ZipOutputStream zip) { // æ¥è¯¢è¡¨ä¿¡æ¯ GenTable table = baseMapper.selectGenTableByName(tableName); // 设置主åè¡¨ä¿¡æ¯ @@ -375,6 +397,60 @@ } /** * ä¿®æ¹ä¿ååæ°æ ¡éª * * @param genTable ä¸å¡ä¿¡æ¯ */ @Override public void validateEdit(GenTable genTable) { if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) { String options = JsonUtils.toJsonString(genTable.getParams()); Dict paramsObj = JsonUtils.parseMap(options); if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_CODE))) { throw new ServiceException("æ ç¼ç åæ®µä¸è½ä¸ºç©º"); } else if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_PARENT_CODE))) { throw new ServiceException("æ ç¶ç¼ç åæ®µä¸è½ä¸ºç©º"); } else if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_NAME))) { throw new ServiceException("æ åç§°åæ®µä¸è½ä¸ºç©º"); } else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) { if (StringUtils.isEmpty(genTable.getSubTableName())) { throw new ServiceException("å ³èå表ç表åä¸è½ä¸ºç©º"); } else if (StringUtils.isEmpty(genTable.getSubTableFkName())) { throw new ServiceException("åè¡¨å ³èçå¤é®åä¸è½ä¸ºç©º"); } } } } /** * 设置主é®åä¿¡æ¯ * * @param table ä¸å¡è¡¨ä¿¡æ¯ */ public void setPkColumn(GenTable table) { for (GenTableColumn column : table.getColumns()) { if (column.isPk()) { table.setPkColumn(column); break; } } if (ObjectUtil.isNull(table.getPkColumn())) { table.setPkColumn(table.getColumns().get(0)); } if (GenConstants.TPL_SUB.equals(table.getTplCategory())) { for (GenTableColumn column : table.getSubTable().getColumns()) { if (column.isPk()) { table.getSubTable().setPkColumn(column); break; } } if (ObjectUtil.isNull(table.getSubTable().getPkColumn())) { table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0)); } } } /** * 设置主åè¡¨ä¿¡æ¯ * * @param table ä¸å¡è¡¨ä¿¡æ¯ @@ -386,4 +462,41 @@ } } /** * 设置代ç çæå ¶ä»éé¡¹å¼ * * @param genTable 设置åççæå¯¹è±¡ */ public void setTableFromOptions(GenTable genTable) { Dict paramsObj = JsonUtils.parseMap(genTable.getOptions()); if (ObjectUtil.isNotNull(paramsObj)) { String treeCode = paramsObj.getStr(GenConstants.TREE_CODE); String treeParentCode = paramsObj.getStr(GenConstants.TREE_PARENT_CODE); String treeName = paramsObj.getStr(GenConstants.TREE_NAME); String parentMenuId = paramsObj.getStr(GenConstants.PARENT_MENU_ID); String parentMenuName = paramsObj.getStr(GenConstants.PARENT_MENU_NAME); genTable.setTreeCode(treeCode); genTable.setTreeParentCode(treeParentCode); genTable.setTreeName(treeName); genTable.setParentMenuId(parentMenuId); genTable.setParentMenuName(parentMenuName); } } /** * è·å代ç çæå°å * * @param table ä¸å¡è¡¨ä¿¡æ¯ * @param template æ¨¡æ¿æä»¶è·¯å¾ * @return çæå°å */ public static String getGenPath(GenTable table, String template) { String genPath = table.getGenPath(); if (StringUtils.equals(genPath, "/")) { return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table); } return genPath + File.separator + VelocityUtils.getFileName(template, table); } } ruoyi-generator/src/main/java/com/ruoyi/generator/service/OracleGenTableServiceImpl.java
ÎļþÒÑɾ³ý ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml
@@ -30,9 +30,41 @@ </resultMap> <select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult"> select column_name, (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else null end) as is_required, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type <if test="@com.ruoyi.common.helper.DataBaseHelper@isMySql()"> select column_name, (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else null end) as is_required, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type from information_schema.columns where table_schema = (select database()) and table_name = (#{tableName}) order by ordinal_position </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isOracle()"> select lower(temp.column_name) as column_name, (case when (temp.nullable = 'N' and temp.constraint_type != 'P') then '1' else null end) as is_required, (case when temp.constraint_type = 'P' then '1' else '0' end) as is_pk, temp.column_id as sort, temp.comments as column_comment, (case when temp.constraint_type = 'P' then '1' else '0' end) as is_increment, lower(temp.data_type) as column_type from ( select col.column_id, col.column_name,col.nullable, col.data_type, colc.comments, uc.constraint_type, row_number() over (partition by col.column_name order by uc.constraint_type desc) as row_flg from user_tab_columns col left join user_col_comments colc on colc.table_name = col.table_name and colc.column_name = col.column_name left join user_cons_columns ucc on ucc.table_name = col.table_name and ucc.column_name = col.column_name left join user_constraints uc on uc.constraint_name = ucc.constraint_name where col.table_name = upper(#{tableName}) ) temp WHERE temp.row_flg = 1 ORDER BY temp.column_id </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isPostgerSql()"> </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isSqlServer()"> </if> </select> </mapper> ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml
@@ -54,30 +54,125 @@ </resultMap> <select id="selectPageDbTableList" resultMap="GenTableResult"> <if test="@com.ruoyi.common.helper.DataBaseHelper@isMySql()"> select table_name, table_comment, create_time, update_time from information_schema.tables ${ew.getCustomSqlSegment} AND table_name NOT LIKE 'qrtz_%' AND table_name NOT LIKE 'gen_%' AND table_name NOT IN (select table_name from gen_table) <if test="genTable.tableName != null and genTable.tableName != ''"> AND lower(table_name) like lower(concat('%', #{genTable.tableName}, '%')) </if> <if test="genTable.tableComment != null and genTable.tableComment != ''"> AND lower(table_comment) like lower(concat('%', #{genTable.tableComment}, '%')) </if> order by create_time desc </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isOracle()"> select lower(dt.table_name) as table_name, dtc.comments as table_comment, uo.created as create_time, uo.last_ddl_time as update_time from user_tables dt, user_tab_comments dtc, user_objects uo where dt.table_name = dtc.table_name and dt.table_name = uo.object_name and uo.object_type = 'TABLE' AND dt.table_name NOT LIKE 'QRTZ_%' AND dt.table_name NOT LIKE 'GEN_%' AND lower(dt.table_name) NOT IN (select table_name from gen_table) <if test="genTable.tableName != null and genTable.tableName != ''"> AND lower(dt.table_name) like lower(concat(concat('%', #{genTable.tableName}), '%')) </if> <if test="genTable.tableComment != null and genTable.tableComment != ''"> AND lower(dtc.comments) like lower(concat(concat('%', #{genTable.tableComment}), '%')) </if> order by create_time desc </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isPostgerSql()"> </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isSqlServer()"> </if> </select> <select id="selectDbTableList" resultMap="GenTableResult"> <if test="@com.ruoyi.common.helper.DataBaseHelper@isMySql()"> select table_name, table_comment, create_time, update_time from information_schema.tables ${ew.getCustomSqlSegment} AND table_name NOT LIKE 'qrtz_%' AND table_name NOT LIKE 'gen_%' AND table_name NOT IN (select table_name from gen_table) <if test="tableName != null and tableName != ''"> AND lower(table_name) like lower(concat('%', #{tableName}, '%')) </if> <if test="tableComment != null and tableComment != ''"> AND lower(table_comment) like lower(concat('%', #{tableComment}, '%')) </if> order by create_time desc </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isOracle()"> select lower(dt.table_name) as table_name, dtc.comments as table_comment, uo.created as create_time, uo.last_ddl_time as update_time from user_tables dt, user_tab_comments dtc, user_objects uo where dt.table_name = dtc.table_name and dt.table_name = uo.object_name and uo.object_type = 'TABLE' AND dt.table_name NOT LIKE 'QRTZ_%' AND dt.table_name NOT LIKE 'GEN_%' AND lower(dt.table_name) NOT IN (select table_name from gen_table) <if test="tableName != null and tableName != ''"> AND lower(dt.table_name) like lower(concat(concat('%', #{tableName}), '%')) </if> <if test="tableComment != null and tableComment != ''"> AND lower(dtc.comments) like lower(concat(concat('%', #{tableComment}), '%')) </if> order by create_time desc </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isPostgerSql()"> </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isSqlServer()"> </if> </select> <select id="selectDbTableListByNames" resultMap="GenTableResult"> <if test="@com.ruoyi.common.helper.DataBaseHelper@isMySql()"> select table_name, table_comment, create_time, update_time from information_schema.tables where table_name NOT LIKE 'xxl_job_%' and table_name NOT LIKE 'gen_%' and table_schema = (select database()) and table_name in <foreach collection="array" item="name" open="(" separator="," close=")"> #{name} </foreach> </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isOracle()"> select lower(dt.table_name) as table_name, dtc.comments as table_comment, uo.created as create_time, uo.last_ddl_time as update_time from user_tables dt, user_tab_comments dtc, user_objects uo where dt.table_name = dtc.table_name and dt.table_name = uo.object_name and uo.object_type = 'TABLE' AND dt.table_name NOT LIKE 'XXL_JOB_%' AND dt.table_name NOT LIKE 'GEN_%' AND dt.table_name NOT IN (select table_name from gen_table) and lower(dt.table_name) in <foreach collection="array" item="name" open="(" separator="," close=")"> #{name} </foreach> </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isPostgerSql()"> </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isSqlServer()"> </if> </select> <select id="selectTableByName" parameterType="String" resultMap="GenTableResult"> <if test="@com.ruoyi.common.helper.DataBaseHelper@isMySql()"> select table_name, table_comment, create_time, update_time from information_schema.tables where table_comment <![CDATA[ <> ]]> '' and table_schema = (select database()) and table_name = #{tableName} </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isOracle()"> select lower(dt.table_name) as table_name, dtc.comments as table_comment, uo.created as create_time, uo.last_ddl_time as update_time from user_tables dt, user_tab_comments dtc, user_objects uo where dt.table_name = dtc.table_name and dt.table_name = uo.object_name and uo.object_type = 'TABLE' AND dt.table_name NOT LIKE 'XXL_JOB_%' AND dt.table_name NOT LIKE 'GEN_%' AND dt.table_name NOT IN (select table_name from gen_table) and lower(dt.table_name) = #{tableName} </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isPostgerSql()"> </if> <if test="@com.ruoyi.common.helper.DataBaseHelper@isSqlServer()"> </if> </select> <select id="selectGenTableById" parameterType="Long" resultMap="GenTableResult"> ruoyi-generator/src/main/resources/mapper/generator/OracleGenTableColumnMapper.xml
ÎļþÒÑɾ³ý ruoyi-generator/src/main/resources/mapper/generator/OracleGenTableMapper.xml
ÎļþÒÑɾ³ý