package com.yqzx.generator.service;
|
|
import cn.hutool.core.util.StrUtil;
|
import com.yqzx.generator.config.DataSourceConfig;
|
import com.yqzx.generator.action.model.DbColumnType;
|
import com.yqzx.generator.action.model.Table;
|
import com.yqzx.generator.action.model.TableInfo;
|
import com.yqzx.generator.action.model.Template;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.sql.Connection;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.sql.Statement;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* @description: 表工具类
|
* @author: chaoyapeng
|
* @time: 2020/8/18 16:13
|
*/
|
@Slf4j
|
public class GeneratorService {
|
|
private DataSourceConfig dataSourceConfig;
|
|
public GeneratorService setDataSourceConfig(DataSourceConfig dataSourceConfig) {
|
this.dataSourceConfig = dataSourceConfig;
|
return this;
|
}
|
|
public GeneratorService setDataSourceConfig(String driverName, String username, String password, String url) {
|
DataSourceConfig dataSourceConfig = new DataSourceConfig();
|
dataSourceConfig.setDriverName(driverName);
|
dataSourceConfig.setUsername(username);
|
dataSourceConfig.setPassword(password);
|
dataSourceConfig.setUrl(url);
|
this.dataSourceConfig = dataSourceConfig;
|
return this;
|
}
|
|
/**
|
* 查询数据库表信息
|
*
|
* @param schema
|
* @return
|
*/
|
public List<Table> getTable(String schema) {
|
{
|
// 获取数据库连接,查询表结构信息
|
Connection conn = dataSourceConfig.getConn();
|
Statement sm = null;
|
List<Table> tables = new ArrayList<>();
|
try {
|
sm = conn.createStatement();
|
StringBuffer sql = new StringBuffer();
|
sql.append("select table_name as tablename,table_comment as tablecomment from information_schema.`tables` where table_schema = '");
|
sql.append(schema);
|
sql.append("'");
|
/*String sql = "select table_name as tablename,table_comment as tablecomment from information_schema.`tables` where table_schema = '"
|
+ schema + "'";*/
|
ResultSet rs = sm.executeQuery(sql.toString());
|
while (rs.next()) {
|
Table table = new Table();
|
table.setName(rs.getString(1));
|
table.setComment(rs.getString(2));
|
table.setLabel(table.getName() + "-" + table.getComment());
|
tables.add(table);
|
}
|
} catch (SQLException e) {
|
log.error(e.getMessage());
|
}
|
if (sm != null) {
|
try {
|
sm.close();
|
} catch (SQLException e) {
|
log.error(e.getMessage());
|
}
|
}
|
if (conn != null) {
|
try {
|
conn.close();
|
} catch (SQLException e) {
|
log.error(e.getMessage());
|
}
|
}
|
return tables;
|
}
|
}
|
|
/**
|
* 查询表结构信息
|
*
|
* @param tableName
|
* @return
|
*/
|
public List<TableInfo> getTableInfo(String tableName) {
|
Connection conn = dataSourceConfig.getConn();
|
Statement sm = null;
|
List<TableInfo> tableInfos = new ArrayList<>();
|
try {
|
sm = conn.createStatement();
|
StringBuffer sql = new StringBuffer();
|
sql.append("select column_name, data_type, column_comment from information_schema.columns where table_schema = (select database()) and table_name = '");
|
sql.append(tableName);
|
sql.append("'");
|
/*String sql = "select column_name, data_type, column_comment from information_schema.columns where table_schema = (select database()) and table_name = '"
|
+ tableName + "'";*/
|
ResultSet rs = sm.executeQuery(sql.toString());
|
while (rs.next()) {
|
TableInfo tableInfo = new TableInfo();
|
tableInfo.setName(rs.getString(1));
|
DbColumnType column = DbColumnType.get(rs.getString(2));
|
tableInfo.setType(column.getType());
|
tableInfo.setJdbcType(column.getJdbcType());
|
tableInfo.setComment(rs.getString(3));
|
tableInfo.setPropertyName(StrUtil.toCamelCase(tableInfo.getName()));
|
tableInfos.add(tableInfo);
|
}
|
if (tableInfos.size() > 0) {
|
TableInfo t = tableInfos.get(tableInfos.size() - 1);
|
tableInfos.remove(tableInfos.size() - 1);
|
t.setLast(true);
|
tableInfos.add(t);
|
}
|
} catch (SQLException e) {
|
log.error(e.getMessage());
|
}
|
if (sm != null) {
|
try {
|
sm.close();
|
} catch (SQLException e) {
|
log.error(e.getMessage());
|
}
|
}
|
if (conn != null) {
|
try {
|
conn.close();
|
} catch (SQLException e) {
|
log.error(e.getMessage());
|
}
|
}
|
return tableInfos;
|
}
|
|
/**
|
* 获取模板内容
|
*
|
* @return
|
*/
|
public List<Template> getTemplate() {
|
List<Template> templates = new ArrayList<>();
|
templates.add(create("controllerSwitch", "controller-模板"));
|
templates.add(create("serviceSwitch", "service-模板(包含Impl)"));
|
templates.add(create("mapperSwitch", "mapper-模板(包含xml和继承xml)"));
|
templates.add(create("modelSwitch", "model-模板"));
|
templates.add(create("querySwitch", "query-模板(列表查询参数,默认全部model内容)"));
|
return templates;
|
}
|
|
private static Template create(String name, String comment) {
|
Template template = new Template();
|
template.setName(name);
|
template.setComment(comment);
|
return template;
|
}
|
|
}
|