一、配置文件
1-配置文件: src/main/resources/generator.yml
2-配置生成表的基础信息:包路径 前缀规则等
# 代码生成
gen:
# 作者
author: ruoyi
# 默认生成包路径 system 需改成自己的模块名称 如 system monitor tool
packageName: com.ruoyi.system
# 自动去除表前缀,默认是false
autoRemovePre: false
# 表前缀(生成类名不会包含表前缀,多个用逗号分隔)
tablePrefix: sys_
配置类:GenConfig
路径 : src/main/java/com/ruoyi/generator/config/GenConfig.java
package com.ruoyi.generator.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 读取代码生成相关配置
*
* @author ruoyi
*/
@Component
@ConfigurationProperties(prefix = "gen")
@PropertySource(value = {"classpath:generator.yml"}, encoding = "UTF-8")
public class GenConfig {
/**
* 作者
*/
public static String author;
/**
* 生成包路径
*/
public static String packageName;
/**
* 自动去除表前缀,默认是false
*/
public static boolean autoRemovePre;
/**
* 表前缀(类名不会包含表前缀)
*/
public static String tablePrefix;
public static String getAuthor() {
return author;
}
@Value("${author}")
public void setAuthor(String author) {
GenConfig.author = author;
}
public static String getPackageName() {
return packageName;
}
//省略。。。。。。。。
}
二、导入表逻辑
功能:导入需要生成的表数据到以下2张表
gen_table 导入的表
gen_table_column 导入的表列名
页面位置:src/views/tool/gen/importTable.vue
2-1 导入按钮:
上面 导入按钮组件 ,在index页面中
按钮逻辑:
1-点击导入按钮,调用弹框页面show方法
2-show 展示导入选表页面,渲染所有表列表
//0-引入导入页面组件
import importTable from "./importTable";
export default {
name: "Gen",
components: { importTable },
//1-点击导入按钮,触发 openImportTable方法
<el-button
type="info"
plain
icon="el-icon-upload"
size="mini"
@click="openImportTable"
v-hasPermi="['tool:gen:import']"
>导入</el-button>
//2-js方法:openImportTable
/** 打开导入表弹窗 调用了import组件里面的show方法 */
openImportTable() {
this.$refs.import.show();
},
//3-import组件中show方法:调用后台接口: /db/list 查询数据,并展示弹框页面
show() {
this.getList();
this.visible = true;
},
// 查询表数据
getList() {
listDbTable(this.queryParams).then(res => {
if (res.code === 200) {
this.dbTableList = res.rows;
this.total = res.total;
}
});
},
后台查询列表中
根据 <if>条件 判断数据库类型,执行对应sql,查询数据库所有表
<if test="@com.ruoyi.common.helper.DataBaseHelper@isMySql()">:判断是myql
<if test="@com.ruoyi.common.helper.DataBaseHelper@isOracle()">:判断oracle查询对应库博
<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
where table_schema = (select database())
AND table_name NOT LIKE 'xxl_job_%' 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