add
This commit is contained in:
@ -18,6 +18,7 @@
|
||||
<module>qiaoba-module-monitor</module>
|
||||
<module>qiaoba-module-tenant</module>
|
||||
<module>qiaoba-module-demo</module>
|
||||
<module>qiaoba-module-generator</module>
|
||||
</modules>
|
||||
|
||||
<description>qiaoba-modules: 新建的模块, 统一放在这个模块下面</description>
|
||||
|
34
qiaoba-modules/qiaoba-module-generator/pom.xml
Normal file
34
qiaoba-modules/qiaoba-module-generator/pom.xml
Normal file
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>qiaoba-modules</artifactId>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>qiaoba-module-generator</artifactId>
|
||||
|
||||
<description> 代码生成 </description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<artifactId>qiaoba-common-base</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<artifactId>qiaoba-common-datasource</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<artifactId>qiaoba-common-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-engine-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,20 @@
|
||||
package com.qiaoba.module.generator.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 代码生成 Web层
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/26 9:09
|
||||
*/
|
||||
@Validated
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/tool/gen")
|
||||
public class GenController {
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.qiaoba.module.generator.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 数据库表信息
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/26 9:21
|
||||
*/
|
||||
@Data
|
||||
public class TableDto implements Serializable {
|
||||
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 表描述
|
||||
*/
|
||||
private String tableComment;
|
||||
|
||||
/**
|
||||
* 数据库类型 Mysql/Oracle/PgSql/Sql Server
|
||||
*/
|
||||
private String dbType;
|
||||
|
||||
/**
|
||||
* 模式
|
||||
*/
|
||||
private String schema;
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.qiaoba.module.generator.entity.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 数据库表信息
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/26 9:21
|
||||
*/
|
||||
@Data
|
||||
public class DbTableVo implements Serializable {
|
||||
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 表描述
|
||||
*/
|
||||
private String tableComment;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.qiaoba.module.generator.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.qiaoba.module.generator.entity.dto.TableDto;
|
||||
import com.qiaoba.module.generator.entity.vo.DbTableVo;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* 数据库表 数据层
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/26 9:24
|
||||
*/
|
||||
public interface TableMapper {
|
||||
|
||||
/**
|
||||
* 查询数据库表
|
||||
*
|
||||
* @param page 分页
|
||||
* @param dto 查询参数
|
||||
* @return list
|
||||
*/
|
||||
Page<DbTableVo> selectPageDbTableList(Page page, @Param("dto") TableDto dto);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.qiaoba.module.generator.service;
|
||||
|
||||
import com.qiaoba.common.database.entity.PageQuery;
|
||||
import com.qiaoba.common.database.entity.TableDataInfo;
|
||||
import com.qiaoba.module.generator.entity.dto.TableDto;
|
||||
import com.qiaoba.module.generator.entity.vo.DbTableVo;
|
||||
|
||||
/**
|
||||
* 数据库表 服务层
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/26 9:24
|
||||
*/
|
||||
public interface TableService {
|
||||
|
||||
/**
|
||||
* 分页查询数据库表
|
||||
*
|
||||
* @param dto 查询条件
|
||||
* @param pageQuery 分页信息
|
||||
* @return list
|
||||
*/
|
||||
TableDataInfo<DbTableVo> selectPageDbTableList(TableDto dto, PageQuery pageQuery);
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.qiaoba.module.generator.service.impl;
|
||||
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.database.config.DynamicDataSourceConfig;
|
||||
import com.qiaoba.common.database.entity.PageQuery;
|
||||
import com.qiaoba.common.database.entity.TableDataInfo;
|
||||
import com.qiaoba.module.generator.entity.dto.TableDto;
|
||||
import com.qiaoba.module.generator.entity.vo.DbTableVo;
|
||||
import com.qiaoba.module.generator.mapper.TableMapper;
|
||||
import com.qiaoba.module.generator.service.TableService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 数据库表 服务层实现
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/26 10:12
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TableServiceImpl implements TableService {
|
||||
|
||||
private final TableMapper tableMapper;
|
||||
|
||||
@Override
|
||||
public TableDataInfo<DbTableVo> selectPageDbTableList(TableDto dto, PageQuery pageQuery) {
|
||||
String dbType = DynamicDataSourceConfig.TENANT_DATASOURCE_TYPE_MAP.get(BaseContext.getTenantId());
|
||||
dto.setDbType(dbType);
|
||||
dto.setSchema(selectSchema());
|
||||
return TableDataInfo.build(tableMapper.selectPageDbTableList(pageQuery.build(), dto));
|
||||
}
|
||||
|
||||
private String selectSchema() {
|
||||
// PgSQL 需要设置 schema
|
||||
//DataBaseEnum.POSTGRE_SQL.getType().equals(dbType)
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.qiaoba.module.generator.mapper.TableMapper">
|
||||
|
||||
<resultMap type="com.qiaoba.module.generator.entity.vo.DbTableVo" id="DbTableVoResult">
|
||||
<result property="tableName" column="table_name"/>
|
||||
<result property="tableComment" column="table_comment"/>
|
||||
<result property="createTime" column="create_time"/>
|
||||
<result property="updateTime" column="update_time"/>
|
||||
</resultMap>
|
||||
|
||||
<select id="selectPageDbTableList" resultMap="DbTableVoResult">
|
||||
<if test="dto.dbType=='MySQL'.toString()">
|
||||
select table_name, table_comment, create_time, update_time from information_schema.tables
|
||||
where table_schema = (select database())
|
||||
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="dto.tableName != null and dto.tableName != ''">
|
||||
AND lower(table_name) like lower(concat('%', #{dto.tableName}, '%'))
|
||||
</if>
|
||||
<if test="dto.tableComment != null and dto.tableComment != ''">
|
||||
AND lower(table_comment) like lower(concat('%', #{dto.tableComment}, '%'))
|
||||
</if>
|
||||
order by create_time desc
|
||||
</if>
|
||||
|
||||
<if test="dto.dbType=='Oracle'.toString()">
|
||||
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)
|
||||
<if test="dto.tableName != null and dto.tableName != ''">
|
||||
AND lower(dt.table_name) like lower(concat(concat('%', #{dto.tableName}), '%'))
|
||||
</if>
|
||||
<if test="dto.tableComment != null and dto.tableComment != ''">
|
||||
AND lower(dtc.comments) like lower(concat(concat('%', #{dto.tableComment}), '%'))
|
||||
</if>
|
||||
order by create_time desc
|
||||
</if>
|
||||
|
||||
<if test="dto.dbType=='PostgreSQL'.toString()">
|
||||
select table_name, table_comment, create_time, update_time
|
||||
from (
|
||||
SELECT c.relname AS table_name,
|
||||
obj_description(c.oid) AS table_comment,
|
||||
CURRENT_TIMESTAMP AS create_time,
|
||||
CURRENT_TIMESTAMP AS update_time
|
||||
FROM pg_class c
|
||||
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE (c.relkind = ANY (ARRAY ['r'::"char", 'p'::"char"]))
|
||||
AND c.relname != 'spatial_%'::text
|
||||
AND n.nspname = #{dto.schema}::name
|
||||
AND n.nspname <![CDATA[ <> ]]> ''::name
|
||||
) list_table
|
||||
where 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="dto.tableName != null and dto.tableName != ''">
|
||||
AND lower(table_name) like lower(concat('%', #{dto.tableName}, '%'))
|
||||
</if>
|
||||
<if test="dto.tableComment != null and dto.tableComment != ''">
|
||||
AND lower(table_comment) like lower(concat('%', #{dto.tableComment}, '%'))
|
||||
</if>
|
||||
order by create_time desc
|
||||
</if>
|
||||
</select>
|
||||
</mapper>
|
@ -24,8 +24,16 @@ public class SysTenantDatasourceParam implements Serializable {
|
||||
|
||||
private String isPrimary;
|
||||
|
||||
private String ip;
|
||||
|
||||
public SysTenantDatasourceParam(String tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
|
||||
public SysTenantDatasourceParam(String tenantId, String isPrimary) {
|
||||
this.tenantId = tenantId;
|
||||
this.isPrimary = isPrimary;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,15 @@ public interface SysTenantDatasourceService {
|
||||
*/
|
||||
SysTenantDatasource selectPrimary(String tenantId);
|
||||
|
||||
/**
|
||||
* 通过IP查询
|
||||
*
|
||||
* @param tenantId tenantId
|
||||
* @param ip ip
|
||||
* @return obj
|
||||
*/
|
||||
SysTenantDatasource selectByIp(String tenantId, String ip);
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
|
@ -1,8 +1,10 @@
|
||||
package com.qiaoba.module.tenant.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.enums.BaseEnum;
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.database.entity.DynamicDataSource;
|
||||
import com.qiaoba.common.database.service.DynamicDatasourceService;
|
||||
import com.qiaoba.module.tenant.entity.SysTenantDatasource;
|
||||
@ -11,10 +13,7 @@ import com.qiaoba.module.tenant.service.SysTenantDatasourceService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 动态数据源接口
|
||||
@ -35,7 +34,7 @@ public class DynamicDatasourceServiceImpl implements DynamicDatasourceService {
|
||||
List<String> tenantIds = sysTenantDatasourceService.selectTenantIds();
|
||||
for (String tenantId : tenantIds) {
|
||||
List<SysTenantDatasource> datasourceList = sysTenantDatasourceService.selectList(new SysTenantDatasourceParam(tenantId));
|
||||
datasourceMap.put(tenantId, transform(datasourceList));
|
||||
datasourceMap.put(tenantId, transformList(datasourceList));
|
||||
}
|
||||
return datasourceMap;
|
||||
}
|
||||
@ -51,17 +50,30 @@ public class DynamicDatasourceServiceImpl implements DynamicDatasourceService {
|
||||
sysTenantDatasourceService.setBackupDatasourceExcludeId(tenantId, datasourceId);
|
||||
}
|
||||
|
||||
private List<DynamicDataSource> transform(List<SysTenantDatasource> datasourceList) {
|
||||
@Override
|
||||
public DynamicDataSource selectByIp(String tenantId, String ip) {
|
||||
SysTenantDatasource sysTenantDatasource = sysTenantDatasourceService.selectByIp(tenantId, ip);
|
||||
if (Objects.isNull(sysTenantDatasource)) {
|
||||
throw new ServiceException(StrUtil.format("未找到数据源,查询方式: {}", ip));
|
||||
}
|
||||
return transform(sysTenantDatasource);
|
||||
}
|
||||
|
||||
private List<DynamicDataSource> transformList(List<SysTenantDatasource> datasourceList) {
|
||||
List<DynamicDataSource> dynamicDataSourceList = new ArrayList<>();
|
||||
for (SysTenantDatasource datasource : datasourceList) {
|
||||
DynamicDataSource dynamicDataSource = BeanUtil.copyProperties(datasource, DynamicDataSource.class);
|
||||
dynamicDataSource.setInitialSize(datasource.getInitCount());
|
||||
dynamicDataSource.setMinIdle(datasource.getMinCount());
|
||||
dynamicDataSource.setMaxActive(datasource.getMaxCount());
|
||||
dynamicDataSource.setDriver(DataBaseEnum.getDriver(datasource.getType()));
|
||||
dynamicDataSource.setUrl(DataBaseEnum.getUrl(datasource.getType(), datasource.getIp(), datasource.getPort(), datasource.getName()));
|
||||
dynamicDataSourceList.add(dynamicDataSource);
|
||||
dynamicDataSourceList.add(transform(datasource));
|
||||
}
|
||||
return dynamicDataSourceList;
|
||||
}
|
||||
|
||||
private DynamicDataSource transform(SysTenantDatasource datasource) {
|
||||
DynamicDataSource dynamicDataSource = BeanUtil.copyProperties(datasource, DynamicDataSource.class);
|
||||
dynamicDataSource.setInitialSize(datasource.getInitCount());
|
||||
dynamicDataSource.setMinIdle(datasource.getMinCount());
|
||||
dynamicDataSource.setMaxActive(datasource.getMaxCount());
|
||||
dynamicDataSource.setDriver(DataBaseEnum.getDriver(datasource.getType()));
|
||||
dynamicDataSource.setUrl(DataBaseEnum.getUrl(datasource.getType(), datasource.getIp(), datasource.getPort(), datasource.getName()));
|
||||
return dynamicDataSource;
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,13 @@ public class SysTenantDatasourceServiceImpl implements SysTenantDatasourceServic
|
||||
return sysTenantDatasourceMapper.selectOne(paramToWrapper(new SysTenantDatasourceParam(tenantId, BaseEnum.YES.getCode())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysTenantDatasource selectByIp(String tenantId, String ip) {
|
||||
SysTenantDatasourceParam param = new SysTenantDatasourceParam(tenantId);
|
||||
param.setIp(ip);
|
||||
return sysTenantDatasourceMapper.selectOne(paramToWrapper(param));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insert(SysTenantDatasource sysTenantDatasource) {
|
||||
return sysTenantDatasourceMapper.insert(sysTenantDatasource);
|
||||
@ -69,6 +76,7 @@ public class SysTenantDatasourceServiceImpl implements SysTenantDatasourceServic
|
||||
QueryWrapper<SysTenantDatasource> wrapper = new QueryWrapper<>();
|
||||
wrapper.lambda()
|
||||
.eq(StrUtil.isNotBlank(param.getTenantId()), SysTenantDatasource::getTenantId, param.getTenantId())
|
||||
.eq(StrUtil.isNotBlank(param.getIp()), SysTenantDatasource::getIp, param.getIp())
|
||||
.eq(StrUtil.isNotBlank(param.getIsPrimary()), SysTenantDatasource::getIsPrimary, param.getIsPrimary());
|
||||
return wrapper;
|
||||
}
|
||||
|
Reference in New Issue
Block a user