♻️ Refactoring code. 代码生成支持动态数据源维护

This commit is contained in:
冷冷 2020-03-14 17:57:46 +08:00
parent f0c530ee87
commit 4ed31d354e
9 changed files with 28 additions and 103 deletions

View File

@ -7,24 +7,6 @@ SET FOREIGN_KEY_CHECKS = 0;
USE `pig_codegen`;
/*
*
* * Copyright (c) 2019-2020, (wangiegie@gmail.com).
* * <p>
* * Licensed under the GNU Lesser General Public License 3.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* * <p>
* * https://www.gnu.org/licenses/lgpl.html
* * <p>
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
-- ----------------------------
-- Table structure for gen_datasource_conf
-- ----------------------------
@ -38,7 +20,6 @@ CREATE TABLE `gen_datasource_conf` (
`create_date` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_date` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新',
`del_flag` char(1) DEFAULT '0',
`tenant_id` int(11) DEFAULT NULL COMMENT '租户ID',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COMMENT='数据源表';
@ -53,7 +34,6 @@ CREATE TABLE `gen_form_conf` (
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`del_flag` char(1) DEFAULT '0',
`tenant_id` int(11) DEFAULT NULL COMMENT '所属租户',
PRIMARY KEY (`id`) USING BTREE,
KEY `table_name` (`table_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='表单配置';

View File

@ -189,24 +189,6 @@ CREATE TABLE `tenant_capacity` (
UNIQUE KEY `uk_tenant_id` (`tenant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租户容量信息表';
/*
*
* * Copyright (c) 2019-2020, (wangiegie@gmail.com).
* * <p>
* * Licensed under the GNU Lesser General Public License 3.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* * <p>
* * https://www.gnu.org/licenses/lgpl.html
* * <p>
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
-- ----------------------------
-- Table structure for tenant_info
-- ----------------------------

View File

@ -72,9 +72,4 @@ public class GenDatasourceConf extends Model<GenDatasourceConf> {
*/
@TableLogic
private String delFlag;
/**
* 租户ID
*/
private Integer tenantId;
}

View File

@ -72,9 +72,4 @@ public class GenFormConf extends Model<GenFormConf> {
*/
@ApiModelProperty(value = "删除标记")
private String delFlag;
/**
* 所属租户
*/
@ApiModelProperty(value = "所属租户", hidden = true)
private Integer tenantId;
}

View File

@ -25,6 +25,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.codegen.service.GenDatasourceConfService;
import com.pig4cloud.pig.codegen.entity.GenDatasourceConf;
import com.pig4cloud.pig.codegen.mapper.GenDatasourceConfMapper;
import com.pig4cloud.pig.common.datasource.support.DataSourceConstants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
@ -123,6 +124,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
dataSourceProperty.setUrl(conf.getUrl());
dataSourceProperty.setUsername(conf.getUsername());
dataSourceProperty.setPassword(conf.getPassword());
dataSourceProperty.setDriverClassName(DataSourceConstants.DS_DRIVER);
DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
dynamicRoutingDataSource.addDataSource(dataSourceProperty.getPollName(), dataSource);
}

View File

@ -120,8 +120,7 @@ public class CodeGenUtils {
String className = tableToJava(tableEntity.getTableName(), tablePrefix);
tableEntity.setCaseClassName(className);
tableEntity.setLowerClassName(StringUtils.uncapitalize(className));
//获取需要在swagger文档中隐藏的属性字段
List<Object> hiddenColumns = config.getList("hiddenColumn");
//列信息
List<ColumnEntity> columnList = new ArrayList<>();
for (Map<String, String> column : columns) {
@ -132,12 +131,7 @@ public class CodeGenUtils {
columnEntity.setExtra(column.get("extra"));
columnEntity.setNullable("NO".equals(column.get("isNullable")));
columnEntity.setColumnType(column.get("columnType"));
//隐藏不需要的在接口文档中展示的字段
if (hiddenColumns.contains(column.get("columnName"))) {
columnEntity.setHidden(Boolean.TRUE);
} else {
columnEntity.setHidden(Boolean.FALSE);
}
columnEntity.setHidden(Boolean.FALSE);
//列名转换成Java属性名
String attrName = columnToJava(columnEntity.getColumnName());
columnEntity.setCaseAttrName(attrName);

View File

@ -26,7 +26,7 @@ moduleName=generator
author=pig code generator
#\u8868\u524D\u7F00(\u7C7B\u540D\u4E0D\u4F1A\u5305\u542B\u8868\u524D\u7F00)
tablePrefix=tb_
tablePrefix=table_
#\u7C7B\u578B\u8F6C\u6362\uFF0C\u914D\u7F6E\u4FE1\u606F
tinyint=Integer
@ -50,6 +50,3 @@ longtext=String
date=LocalDateTime
datetime=LocalDateTime
timestamp=LocalDateTime
# hidden columns in Swagger Models
hiddenColumn=tenant_id

View File

@ -29,6 +29,5 @@
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
<result property="delFlag" column="del_flag"/>
<result property="tenantId" column="tenant_id"/>
</resultMap>
</mapper>

View File

@ -18,11 +18,9 @@
package com.pig4cloud.pig.common.core.mybatis;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.exception.CheckedException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
@ -32,7 +30,11 @@ import org.springframework.web.method.support.ModelAndViewContainer;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author lengleng
@ -71,8 +73,8 @@ public class SqlFilterArgumentResolver implements HandlerMethodArgumentResolver
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
String ascs = request.getParameter("ascs");
String descs = request.getParameter("descs");
String[] ascs = request.getParameterValues("ascs");
String[] descs = request.getParameterValues("descs");
String current = request.getParameter("current");
String size = request.getParameter("size");
@ -85,52 +87,31 @@ public class SqlFilterArgumentResolver implements HandlerMethodArgumentResolver
page.setSize(Long.parseLong(size));
}
// 过滤 asc 条件
List<OrderItem> ascList = sqlInject(ascs, "asc");
// 过滤 desc条件
List<OrderItem> descList = sqlInject(descs, "desc");
List<OrderItem> orderItemList = new ArrayList<>();
if (CollUtil.isNotEmpty(ascList)) {
orderItemList.addAll(ascList);
}
Optional.ofNullable(ascs).ifPresent(s -> orderItemList.addAll(Arrays.stream(s)
.filter(sqlInjectPredicate())
.map(OrderItem::asc).collect(Collectors.toList())));
Optional.ofNullable(descs).ifPresent(s -> orderItemList.addAll(Arrays.stream(s)
.filter(sqlInjectPredicate())
.map(OrderItem::desc).collect(Collectors.toList())));
page.addOrder(orderItemList);
if (CollUtil.isNotEmpty(descList)) {
orderItemList.addAll(descList);
}
page.setOrders(orderItemList);
return page;
}
/**
* SQL注入过滤
* 判断用户输入里面有没有关键字
*
* @param str 待验证的字符串
* @return 返回标准的order 属性
* @return Predicate
*/
private static List<OrderItem> sqlInject(String str, String type) {
if (StrUtil.isBlank(str)) {
return null;
}
//转换成小写
String inStr = str.toLowerCase();
//判断是否包含非法字符
for (String keyword : KEYWORDS) {
if (inStr.contains(keyword)) {
log.error("查询包含非法字符 {}", keyword);
throw new CheckedException(keyword + "包含非法字符");
private Predicate<String> sqlInjectPredicate() {
return sql -> {
for (String keyword : KEYWORDS) {
if (StrUtil.containsIgnoreCase(sql, keyword)) {
return false;
}
}
}
List<OrderItem> orderItemList = new ArrayList<>();
for (String in : str.split(StrUtil.COMMA)) {
if ("asc".equals(type)) {
orderItemList.add(OrderItem.asc(in));
} else {
orderItemList.add(OrderItem.desc(in));
}
}
return orderItemList;
return true;
};
}
}