From 0c493bad143fbaf3b93eb9aa210f26cecad34c65 Mon Sep 17 00:00:00 2001 From: liuyes Date: Wed, 20 Nov 2019 08:20:58 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E5=A4=9A=E6=95=B0=E6=8D=AE=E6=BA=90=E7=A4=BA=E4=BE=8B=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-visual/pigx-dynamic-datasource/README.md | 91 +++++++++ .../pigx-dynamic-datasource-api/pom.xml | 27 +++ .../pigx/dynamic/feign/packge-info.java | 6 + .../pigx-dynamic-datasource-biz/pom.xml | 99 ++++++++++ .../PigxDynamicDatasourceApplication.java | 25 +++ .../dynamic/controller/TestController.java | 96 ++++++++++ .../pigx/dynamic/mapper/TestMapper.java | 50 +++++ .../com/pig4cloud/pigx/dynamic/vo/UserVO.java | 10 + .../src/main/resources/bootstrap.yml | 16 ++ .../src/main/resources/logback-spring.xml | 70 +++++++ .../src/main/resources/mapper/TestMapper.xml | 13 ++ pigx-visual/pigx-dynamic-datasource/pom.xml | 179 ++++++++++++++++++ pigx-visual/pom.xml | 1 + 13 files changed, 683 insertions(+) create mode 100644 pigx-visual/pigx-dynamic-datasource/README.md create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/pom.xml create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/src/main/java/com/pig4cloud/pigx/dynamic/feign/packge-info.java create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/pom.xml create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/PigxDynamicDatasourceApplication.java create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/controller/TestController.java create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/mapper/TestMapper.java create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/vo/UserVO.java create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/bootstrap.yml create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/logback-spring.xml create mode 100644 pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/mapper/TestMapper.xml create mode 100644 pigx-visual/pigx-dynamic-datasource/pom.xml diff --git a/pigx-visual/pigx-dynamic-datasource/README.md b/pigx-visual/pigx-dynamic-datasource/README.md new file mode 100644 index 000000000..79907b1b0 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/README.md @@ -0,0 +1,91 @@ +## 快速入门 +### 创建测试数据库 +``` +create database `test1` default character set utf8mb4 collate utf8mb4_general_ci; +create database `test2` default character set utf8mb4 collate utf8mb4_general_ci; +create database `test3` default character set utf8mb4 collate utf8mb4_general_ci; +``` +### 创建测试表 +``` + CREATE TABLE `user` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', + `username` varchar(255) DEFAULT NULL COMMENT '用户名', + `password` varchar(255) DEFAULT NULL COMMENT '密码', + PRIMARY KEY (`id`) USING BTREE + ) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4 COMMENT='用户测试表'; +``` +### 插入测试数据 +``` +use test1; +BEGIN; +INSERT INTO `user` VALUES (1, 'test1测试一', '123'); +INSERT INTO `user` VALUES (2, 'test1测试二', '123'); +INSERT INTO `user` VALUES (3, 'test1测试三', '123'); +COMMIT; +use test2; +BEGIN; +INSERT INTO `user` VALUES (1, 'test2测试一', '123'); +INSERT INTO `user` VALUES (2, 'test2测试二', '123'); +INSERT INTO `user` VALUES (3, 'test2测试三', '123'); +COMMIT; +use test3; +BEGIN; +INSERT INTO `user` VALUES (1, 'test3测试一', '123'); +INSERT INTO `user` VALUES (2, 'test3测试二', '123'); +INSERT INTO `user` VALUES (3, 'test3测试三', '123'); +COMMIT; +``` +### Nocas新增配置 +``` +## pigx-dynamic-datasource-biz.yml +## spring security 配置 +security: + oauth2: + client: + client-id: ENC(ltJPpR50wT0oIY9kfOe1Iw==) + client-secret: ENC(ltJPpR50wT0oIY9kfOe1Iw==) + scope: server + +# 数据源 +spring: + datasource: + dynamic: + primary: test1 #设置默认的数据源 + druid: + filters: stat + datasource: + test1: + username: ${MYSQL-USER:root} + password: ${MYSQL-USER:root} + url: jdbc:mysql://${MYSQL-HOST:pigx-mysql}:${MYSQL-PORT:3306}/test1?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true + driver-class-name: com.mysql.cj.jdbc.Driver + test2: + username: ${MYSQL-USER:root} + password: ${MYSQL-USER:root} + url: jdbc:mysql://${MYSQL-HOST:pigx-mysql}:${MYSQL-PORT:3306}/test2?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true + driver-class-name: com.mysql.cj.jdbc.Driver + test3: + username: ${MYSQL-USER:root} + password: ${MYSQL-USER:root} + url: jdbc:mysql://${MYSQL-HOST:pigx-mysql}:${MYSQL-PORT:3306}/test3?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true + driver-class-name: com.mysql.cj.jdbc.Driver +``` +### 启动以下应用 ++ PigxNacosApplication ++ PigxSentinelApplication ++ PigxAuthApplication ++ PigxAdminApplication ++ PigxGatewayApplication ++ PigxDynamicDatasourceApplication +### Postman看效果 +``` +http://localhost:6060/test/getTest1 +http://localhost:6060/test/getTest2?current=1&size=5 +http://localhost:6060/test/getTest3?current=1&size=5 +``` +![test1数据库](http://i1.fuimg.com/704023/83e168e45242e135.png "test1数据库") +![test2数据库](http://i1.fuimg.com/704023/cf8c42fbc3512c32.png "test2数据库") +![test3数据库](http://i1.fuimg.com/704023/534483515e3f7cd0.png "test3数据库") + +### 联系我 +QQ:55215085 \ No newline at end of file diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/pom.xml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/pom.xml new file mode 100644 index 000000000..d44172a36 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.pig4cloud + pigx-dynamic-datasource + 3.5.0 + + + pigx-dynamic-datasource-api + + + + + com.pig4cloud + pigx-common-core + + + + com.baomidou + mybatis-plus-extension + + + \ No newline at end of file diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/src/main/java/com/pig4cloud/pigx/dynamic/feign/packge-info.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/src/main/java/com/pig4cloud/pigx/dynamic/feign/packge-info.java new file mode 100644 index 000000000..b497b9232 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/src/main/java/com/pig4cloud/pigx/dynamic/feign/packge-info.java @@ -0,0 +1,6 @@ +/* + @author pigx archetype + *

+ * feign client 存放目录,注意 @EnablePigxFeignClients 的扫描范围 + */ +package com.pig4cloud.pigx.dynamic.feign; \ No newline at end of file diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/pom.xml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/pom.xml new file mode 100644 index 000000000..481d203cd --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + com.pig4cloud + pigx-dynamic-datasource + 3.5.0 + + + pigx-dynamic-datasource-biz + + + + + org.springframework.boot + spring-boot-starter-undertow + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.pig4cloud + pigx-common-data + + + com.baomidou + mybatis-plus-boot-starter + + + druid-spring-boot-starter + com.alibaba + 1.1.20 + + + + com.baomidou + dynamic-datasource-spring-boot-starter + 2.5.6 + + + mysql + mysql-connector-java + + + com.pig4cloud + pigx-common-log + + + + com.pig4cloud + pigx-common-security + + + + com.pig4cloud + pigx-common-sentinel + + + + com.pig4cloud + pigx-dynamic-datasource-api + 3.5.0 + + + + com.pig4cloud + pigx-common-swagger + + + + org.springframework.boot + spring-boot-starter-test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/PigxDynamicDatasourceApplication.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/PigxDynamicDatasourceApplication.java new file mode 100644 index 000000000..e572a2d4e --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/PigxDynamicDatasourceApplication.java @@ -0,0 +1,25 @@ +package com.pig4cloud.pigx.dynamic; + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; +import com.pig4cloud.pigx.common.security.annotation.EnablePigxFeignClients; +import com.pig4cloud.pigx.common.security.annotation.EnablePigxResourceServer; +import com.pig4cloud.pigx.common.swagger.annotation.EnablePigxSwagger2; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.client.SpringCloudApplication; + +/** + * @author pigx archetype + *

+ * 项目启动类 + */ +@EnablePigxSwagger2 +@SpringCloudApplication +@EnablePigxFeignClients +@EnablePigxResourceServer +@EnableAutoConfiguration(exclude={DruidDataSourceAutoConfigure.class}) +public class PigxDynamicDatasourceApplication { + public static void main(String[] args) { + SpringApplication.run(PigxDynamicDatasourceApplication.class, args); + } +} diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/controller/TestController.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/controller/TestController.java new file mode 100644 index 000000000..8f6208e7d --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/controller/TestController.java @@ -0,0 +1,96 @@ +/* + * + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + * + */ + +package com.pig4cloud.pigx.dynamic.controller; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.log.annotation.SysLog; +import com.pig4cloud.pigx.dynamic.mapper.TestMapper; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.Map; + +/** + * @author liuyes + * @date 2019/11/19 + */ +@RestController +@AllArgsConstructor +@RequestMapping("/test") +@Api(value = "test", tags = "动态多数据源示例模块") +public class TestController { + + private TestMapper testMapper; + + /** + * 获取test1数据库用户信息 + * + * @return 用户信息 + */ + @ApiOperation(value = "查询test1数据库用户信息", notes = "查询test1数据库用户信息") + @SysLog("查询test1数据库用户信息" ) + @PostMapping("/getTest1") + public R getTest1() { + List> list = testMapper.getTest1(); + if (list == null) { + return R.failed(null, "查询失败!"); + } + return R.ok(list,"操作成功!"); + } + + /** + * 获取test2数据库用户信息 + * + * @return 用户信息 + */ + @ApiOperation(value = "查询test2数据库用户信息", notes = "查询test2数据库用户信息") + @SysLog("查询test2数据库用户信息" ) + @PostMapping("/getTest2") + public R getTest2(Page page) { + IPage>> list = testMapper.getTest2(page); + if (list == null) { + return R.failed(null, "查询失败!"); + } + return R.ok(list,"操作成功!"); + } + /** + * 获取test3数据库用户信息 + * + * @return 用户信息 + */ + @ApiOperation(value = "查询test3数据库用户信息", notes = "查询test3数据库用户信息") + @SysLog("查询test3数据库用户信息" ) + @PostMapping("/getTest3") + public R getTest3(Page page) { + IPage>> list = testMapper.getTest3(page); + if (list == null) { + return R.failed(null, "查询失败!"); + } + return R.ok(list,"操作成功!"); + } + +} diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/mapper/TestMapper.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/mapper/TestMapper.java new file mode 100644 index 000000000..4a4bbfa3d --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/mapper/TestMapper.java @@ -0,0 +1,50 @@ +/* + * + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + * + */ + +package com.pig4cloud.pigx.dynamic.mapper; + +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.apache.ibatis.annotations.Select; + +import java.util.List; +import java.util.Map; + +/** + *

+ * test Mapper 接口 + *

+ * + * @author liuyes + * @date 2019/11/19 + */ +public interface TestMapper { + + @Select("SELECT * FROM user") + @DS("test1") + List> getTest1(); + + @DS("test2") + IPage>> getTest2(Page page); + + @DS("test3") + IPage>> getTest3(Page page); + +} diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/vo/UserVO.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/vo/UserVO.java new file mode 100644 index 000000000..0950a4b28 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/vo/UserVO.java @@ -0,0 +1,10 @@ +package com.pig4cloud.pigx.dynamic.vo; + +import lombok.Data; + +@Data +public class UserVO { + private Integer id; + private String username; + private String password; +} diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/bootstrap.yml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..05389d5f5 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/bootstrap.yml @@ -0,0 +1,16 @@ +server: + port: 6060 + +spring: + application: + name: @artifactId@ + cloud: + nacos: + discovery: + server-addr: ${NACOS-HOST:pigx-register}:${NACOS-PORT:8848} + config: + server-addr: ${spring.cloud.nacos.discovery.server-addr} + file-extension: yml + shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + profiles: + active: dev diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/logback-spring.xml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/logback-spring.xml new file mode 100644 index 000000000..964ca8735 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/logback-spring.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + + + + + + ${log.path}/debug.log + + ${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz + 50MB + 30 + + + %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n + + + + + + ${log.path}/error.log + + ${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz + 50MB + 30 + + + %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n + + + ERROR + + + + + + + + + + + + + + + + + diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/mapper/TestMapper.xml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/mapper/TestMapper.xml new file mode 100644 index 000000000..fcbe041f9 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/mapper/TestMapper.xml @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/pigx-visual/pigx-dynamic-datasource/pom.xml b/pigx-visual/pigx-dynamic-datasource/pom.xml new file mode 100644 index 000000000..bbb59f65c --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pom.xml @@ -0,0 +1,179 @@ + + + + pigx-visual + com.pig4cloud + 3.5.0 + + + 4.0.0 + + pigx-dynamic-datasource + pom + pigx-dynamic-datasource + + + 2.1.10.RELEASE + Greenwich.SR3 + 2.1.0.RELEASE + Cairo-SR8 + UTF-8 + 1.8 + 1.8 + 5.0.5 + 3.5.0 + 0.0.9 + 2.1.1 + 2.10.2 + 2.1.6 + 2.3.6.RELEASE + + + + + pigx-dynamic-datasource-api + pigx-dynamic-datasource-biz + + + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + com.github.ulisesbocchio + jasypt-spring-boot-starter + ${jasypt.version} + + + + org.springframework.boot + spring-boot-starter-actuator + + + + de.codecentric + spring-boot-admin-starter-client + ${spring-boot-admin.version} + + + + org.projectlombok + lombok + provided + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + io.spring.platform + platform-bom + ${spring-platform.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + com.pig4cloud + pigx-common-bom + ${pigx.bom.version} + pom + import + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + org.springframework.security.oauth + spring-security-oauth2 + ${security.oauth.version} + + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot.version} + + + + spring-boot-starter-tomcat + org.springframework.boot + + + + + + + + ${project.name} + + + src/main/resources + true + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + repackage + + + + + + + + + maven-compiler-plugin + 3.8.0 + + ${maven.compiler.target} + ${maven.compiler.source} + UTF-8 + true + + + + pl.project13.maven + git-commit-id-plugin + 2.2.5 + + + + diff --git a/pigx-visual/pom.xml b/pigx-visual/pom.xml index 20bcf2785..af477a99e 100644 --- a/pigx-visual/pom.xml +++ b/pigx-visual/pom.xml @@ -42,5 +42,6 @@ pigx-sso-client-demo pigx-sentinel-dashboard pigx-tx-manager + pigx-dynamic-datasource From 475b0b1c8f1bb90c7cc94d58e8e2e478026971ff Mon Sep 17 00:00:00 2001 From: liuyes Date: Wed, 20 Nov 2019 08:29:09 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-visual/pigx-dynamic-datasource/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pigx-visual/pigx-dynamic-datasource/README.md b/pigx-visual/pigx-dynamic-datasource/README.md index 79907b1b0..4ac44ffb0 100644 --- a/pigx-visual/pigx-dynamic-datasource/README.md +++ b/pigx-visual/pigx-dynamic-datasource/README.md @@ -86,6 +86,9 @@ http://localhost:6060/test/getTest3?current=1&size=5 ![test1数据库](http://i1.fuimg.com/704023/83e168e45242e135.png "test1数据库") ![test2数据库](http://i1.fuimg.com/704023/cf8c42fbc3512c32.png "test2数据库") ![test3数据库](http://i1.fuimg.com/704023/534483515e3f7cd0.png "test3数据库") - +### 切换数据源 +``` +请参考:com/pig4cloud/pigx/dynamic/mapper/TestMapper.java +``` ### 联系我 QQ:55215085 \ No newline at end of file From 882b1c8a64c2492959894cd00c1067e033815d26 Mon Sep 17 00:00:00 2001 From: liuyes Date: Fri, 20 Dec 2019 20:44:03 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0pigx-code=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=EF=BC=8C=E4=BD=BF=E7=94=A8dynamic-datasource=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=A4=9A=E6=95=B0=E6=8D=AE=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-visual/pigx-code/Dockerfile | 15 + pigx-visual/pigx-code/pom.xml | 107 ++++++ .../pigx/code/PigxCodeApplication.java | 44 +++ .../JdbcDynamicDataSourceProperties.java | 21 ++ .../config/JdbcDynamicDataSourceProvider.java | 47 +++ .../code/controller/GenDsConfController.java | 141 ++++++++ .../controller/GenFormConfController.java | 127 +++++++ .../code/controller/GeneratorController.java | 74 +++++ .../pigx/code/entity/ColumnEntity.java | 70 ++++ .../pig4cloud/pigx/code/entity/GenConfig.java | 59 ++++ .../pigx/code/entity/GenDatasourceConf.java | 79 +++++ .../pigx/code/entity/GenFormConf.java | 79 +++++ .../pigx/code/entity/TableEntity.java | 55 +++ .../code/mapper/GenDatasourceConfMapper.java | 30 ++ .../pigx/code/mapper/GenFormConfMapper.java | 33 ++ .../pigx/code/mapper/GeneratorMapper.java | 59 ++++ .../service/GenDatasourceConfService.java | 44 +++ .../pigx/code/service/GenFormConfService.java | 40 +++ .../pigx/code/service/GeneratorService.java | 48 +++ .../impl/GenDatasourceConfServiceImpl.java | 68 ++++ .../service/impl/GenFormConfServiceImpl.java | 98 ++++++ .../service/impl/GeneratorServiceImpl.java | 106 ++++++ .../pig4cloud/pigx/code/util/GenUtils.java | 312 ++++++++++++++++++ .../src/main/resources/bootstrap.yml | 16 + .../src/main/resources/generator.properties | 37 +++ .../resources/mapper/GenFormConfMapper.xml | 33 ++ .../main/resources/mapper/GeneratorMapper.xml | 23 ++ .../resources/template/Controller.java.vm | 110 ++++++ .../main/resources/template/Entity.java.vm | 56 ++++ .../main/resources/template/Mapper.java.vm | 31 ++ .../src/main/resources/template/Mapper.xml.vm | 35 ++ .../main/resources/template/Service.java.vm | 31 ++ .../resources/template/ServiceImpl.java.vm | 34 ++ .../src/main/resources/template/api.js.vm | 56 ++++ .../src/main/resources/template/crud.js.vm | 17 + .../src/main/resources/template/index.vue.vm | 121 +++++++ .../src/main/resources/template/menu.sql.vm | 16 + pigx-visual/pom.xml | 1 + 38 files changed, 2373 insertions(+) create mode 100644 pigx-visual/pigx-code/Dockerfile create mode 100644 pigx-visual/pigx-code/pom.xml create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/PigxCodeApplication.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/config/JdbcDynamicDataSourceProperties.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/config/JdbcDynamicDataSourceProvider.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GenDsConfController.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GenFormConfController.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GeneratorController.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/ColumnEntity.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenConfig.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenDatasourceConf.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenFormConf.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/TableEntity.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GenDatasourceConfMapper.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GenFormConfMapper.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GeneratorMapper.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GenDatasourceConfService.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GenFormConfService.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GeneratorService.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GenDatasourceConfServiceImpl.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GenFormConfServiceImpl.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GeneratorServiceImpl.java create mode 100644 pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/util/GenUtils.java create mode 100644 pigx-visual/pigx-code/src/main/resources/bootstrap.yml create mode 100644 pigx-visual/pigx-code/src/main/resources/generator.properties create mode 100644 pigx-visual/pigx-code/src/main/resources/mapper/GenFormConfMapper.xml create mode 100644 pigx-visual/pigx-code/src/main/resources/mapper/GeneratorMapper.xml create mode 100644 pigx-visual/pigx-code/src/main/resources/template/Controller.java.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/Entity.java.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/Mapper.java.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/Mapper.xml.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/Service.java.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/ServiceImpl.java.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/api.js.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/crud.js.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/index.vue.vm create mode 100644 pigx-visual/pigx-code/src/main/resources/template/menu.sql.vm diff --git a/pigx-visual/pigx-code/Dockerfile b/pigx-visual/pigx-code/Dockerfile new file mode 100644 index 000000000..fed99aa53 --- /dev/null +++ b/pigx-visual/pigx-code/Dockerfile @@ -0,0 +1,15 @@ +FROM anapsix/alpine-java:8_server-jre_unlimited + +MAINTAINER wangiegie@gmail.com + +RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime + +RUN mkdir -p /pigx-code + +WORKDIR /pigx-code + +EXPOSE 5003 + +ADD ./pigx-visual/pigx-code/target/pigx-code.jar ./ + +CMD sleep 120;java -Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom -jar pigx-code.jar diff --git a/pigx-visual/pigx-code/pom.xml b/pigx-visual/pigx-code/pom.xml new file mode 100644 index 000000000..1ffaa6de9 --- /dev/null +++ b/pigx-visual/pigx-code/pom.xml @@ -0,0 +1,107 @@ + + + 4.0.0 + + + com.pig4cloud + pigx-visual + 3.5.0 + + + pigx-code + jar + + 多数据源代码生成模块 + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.pig4cloud + pigx-common-data + + + + com.baomidou + dynamic-datasource-spring-boot-starter + 2.5.6 + + + + com.baomidou + mybatis-plus-boot-starter + + + mysql + mysql-connector-java + + + + com.alibaba + druid-spring-boot-starter + + + + com.pig4cloud + pigx-common-core + + + + com.pig4cloud + pigx-common-swagger + + + + com.pig4cloud + pigx-common-security + + + + com.pig4cloud + pigx-common-sentinel + + + com.pig4cloud + pigx-common-log + + + + velocity + org.apache.velocity + ${velocity.version} + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-undertow + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + com.spotify + docker-maven-plugin + + + + diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/PigxCodeApplication.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/PigxCodeApplication.java new file mode 100644 index 000000000..18def63af --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/PigxCodeApplication.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code; + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; +import com.pig4cloud.pigx.common.security.annotation.EnablePigxFeignClients; +import com.pig4cloud.pigx.common.security.annotation.EnablePigxResourceServer; +import com.pig4cloud.pigx.common.swagger.annotation.EnablePigxSwagger2; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.client.SpringCloudApplication; + +/** + * @author lengleng + * @date 2018/07/29 + * 代码生成模块 + */ +@EnablePigxSwagger2 +@SpringCloudApplication +@EnablePigxFeignClients +@EnablePigxResourceServer +@EnableAutoConfiguration(exclude={DruidDataSourceAutoConfigure.class}) +public class PigxCodeApplication { + + public static void main(String[] args) { + SpringApplication.run(PigxCodeApplication.class, args); + } + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/config/JdbcDynamicDataSourceProperties.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/config/JdbcDynamicDataSourceProperties.java new file mode 100644 index 000000000..20b157fd6 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/config/JdbcDynamicDataSourceProperties.java @@ -0,0 +1,21 @@ +package com.pig4cloud.pigx.code.config; + +import lombok.AllArgsConstructor; +import lombok.Data; +import org.jasypt.encryption.StringEncryptor; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +@Data +@Component +@ConfigurationProperties("spring.datasource.dynamic") +public class JdbcDynamicDataSourceProperties { + private final StringEncryptor stringEncryptor; + private String username; + private String password; + private String url; + private String driverClassName; + private String sql; + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/config/JdbcDynamicDataSourceProvider.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/config/JdbcDynamicDataSourceProvider.java new file mode 100644 index 000000000..198a10ae9 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/config/JdbcDynamicDataSourceProvider.java @@ -0,0 +1,47 @@ +package com.pig4cloud.pigx.code.config; + +import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider; +import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider; +import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty; +import lombok.AllArgsConstructor; +import org.jasypt.encryption.StringEncryptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; + +@Configuration +@AllArgsConstructor +public class JdbcDynamicDataSourceProvider { + private final StringEncryptor stringEncryptor; + private final JdbcDynamicDataSourceProperties properties; + + @Bean + public DynamicDataSourceProvider dynamicDataSourceProvider() { + return new AbstractJdbcDataSourceProvider(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword()) { + @Override + protected Map executeStmt(Statement statement) throws SQLException { + Map map = new HashMap<>(); + ResultSet rs = statement.executeQuery(properties.getSql()); + while (rs.next()) { + String name = rs.getString("name"); + String username = rs.getString("username"); + String password = rs.getString("password"); + String url = rs.getString("url"); +// String driver = rs.getString("driver"); + DataSourceProperty property = new DataSourceProperty(); + property.setUsername(username); + property.setPassword(stringEncryptor.decrypt(password)); + property.setUrl(url); +// property.setDriverClassName(driver); + map.put(name, property); + } + return map; + } + }; + } +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GenDsConfController.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GenDsConfController.java new file mode 100644 index 000000000..a43ad053c --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GenDsConfController.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ +package com.pig4cloud.pigx.code.controller; + +import com.baomidou.dynamic.datasource.DynamicDataSourceCreator; +import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; +import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.code.entity.GenDatasourceConf; +import com.pig4cloud.pigx.code.service.GenDatasourceConfService; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.log.annotation.SysLog; +import lombok.AllArgsConstructor; +import org.jasypt.encryption.StringEncryptor; +import org.springframework.web.bind.annotation.*; + +import javax.sql.DataSource; + + +/** + * 数据源管理 + * + * @author lengleng + * @date 2019-03-31 16:00:20 + */ +@RestController +@AllArgsConstructor +@RequestMapping("/dsconf") +public class GenDsConfController { + private final GenDatasourceConfService datasourceConfService; + private final StringEncryptor stringEncryptor; + private DataSource dataSource; + private DynamicDataSourceCreator dataSourceCreator; + + /** + * 分页查询 + * + * @param page 分页对象 + * @param datasourceConf 数据源表 + * @return + */ + @GetMapping("/page") + public R getSysDatasourceConfPage(Page page, GenDatasourceConf datasourceConf) { + return R.ok(datasourceConfService.page(page, Wrappers.query(datasourceConf))); + } + + /** + * 查询全部数据源 + * + * @return + */ + @GetMapping("/list") + public R list() { + return R.ok(datasourceConfService.list()); + } + + + /** + * 通过id查询数据源表 + * + * @param id id + * @return R + */ + @GetMapping("/{id}") + public R getById(@PathVariable("id") Integer id) { + return R.ok(datasourceConfService.getById(id)); + } + + /** + * 新增数据源表 + * + * @param datasourceConf 数据源表 + * @return R + */ + @SysLog("新增数据源表") + @PostMapping + public R save(@RequestBody GenDatasourceConf datasourceConf) { + DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; + DataSourceProperty dataSourceProperty = new DataSourceProperty(); + dataSourceProperty.setPollName(datasourceConf.getName()); + dataSourceProperty.setUrl(datasourceConf.getUrl()); + dataSourceProperty.setUsername(datasourceConf.getUsername()); + dataSourceProperty.setPassword(datasourceConf.getPassword()); + DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty); + ds.addDataSource(dataSourceProperty.getPollName(), dataSource); + return R.ok(datasourceConfService.saveDsByEnc(datasourceConf)); + } + + /** + * 修改数据源表 + * + * @param sysDatasourceConf 数据源表 + * @return R + */ + @SysLog("修改数据源表") + @PutMapping + public R updateById(@RequestBody GenDatasourceConf sysDatasourceConf) { + DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; + //先移除 + ds.removeDataSource(datasourceConfService.getById(sysDatasourceConf.getId()).getName()); + //再添加 + DataSourceProperty dataSourceProperty = new DataSourceProperty(); + dataSourceProperty.setPollName(sysDatasourceConf.getName()); + dataSourceProperty.setUrl(sysDatasourceConf.getUrl()); + dataSourceProperty.setUsername(sysDatasourceConf.getUsername()); + dataSourceProperty.setPassword(sysDatasourceConf.getPassword()); + DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty); + ds.addDataSource(dataSourceProperty.getPollName(), dataSource); + return R.ok(datasourceConfService.updateDsByEnc(sysDatasourceConf)); + } + + /** + * 通过id删除数据源表 + * + * @param id id + * @return R + */ + @SysLog("删除数据源表") + @DeleteMapping("/{id}") + public R removeById(@PathVariable Integer id) { + DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; + ds.removeDataSource(datasourceConfService.getById(id).getName()); + return R.ok(datasourceConfService.removeById(id)); + } + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GenFormConfController.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GenFormConfController.java new file mode 100644 index 000000000..a2243bf2b --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GenFormConfController.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.controller; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.code.entity.GenFormConf; +import com.pig4cloud.pigx.code.service.GenFormConfService; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.log.annotation.SysLog; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + + +/** + * 表单管理 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +@RestController +@AllArgsConstructor +@RequestMapping("/form") +@Api(value = "form", tags = "表单管理") +public class GenFormConfController { + private final GenFormConfService genRecordService; + + /** + * 分页查询 + * + * @param page 分页对象 + * @param formConf 生成记录 + * @return + */ + @ApiOperation(value = "分页查询", notes = "分页查询") + @GetMapping("/page") + public R getGenFormConfPage(Page page, GenFormConf formConf) { + return R.ok(genRecordService.page(page, Wrappers.query(formConf))); + } + + + /** + * 通过id查询生成记录 + * + * @param id id + * @return R + */ + @ApiOperation(value = "通过id查询", notes = "通过id查询") + @GetMapping("/{id}") + public R getById(@PathVariable("id") Integer id) { + return R.ok(genRecordService.getById(id)); + } + + /** + * 通过id查询生成记录 + * + * @param dsId 数据源ID + * @param tableName tableName + * @return R + */ + @ApiOperation(value = "通过tableName查询表单信息") + @GetMapping("/info") + public R form(Integer dsId, String tableName) { + return R.ok(genRecordService.getForm(dsId, tableName)); + } + + /** + * 新增生成记录 + * + * @param formConf 生成记录 + * @return R + */ + @ApiOperation(value = "新增生成记录", notes = "新增生成记录") + @SysLog("新增生成记录") + @PostMapping + @PreAuthorize("@pms.hasPermission('gen_form_add')") + public R save(@RequestBody GenFormConf formConf) { + return R.ok(genRecordService.save(formConf)); + } + + /** + * 修改生成记录 + * + * @param formConf 生成记录 + * @return R + */ + @ApiOperation(value = "修改生成记录", notes = "修改生成记录") + @SysLog("修改生成记录") + @PutMapping + @PreAuthorize("@pms.hasPermission('gen_form_edit')") + public R updateById(@RequestBody GenFormConf formConf) { + return R.ok(genRecordService.updateById(formConf)); + } + + /** + * 通过id删除生成记录 + * + * @param id id + * @return R + */ + @ApiOperation(value = "通过id删除生成记录", notes = "通过id删除生成记录") + @SysLog("通过id删除生成记录") + @DeleteMapping("/{id}") + @PreAuthorize("@pms.hasPermission('gen_form_del')") + public R removeById(@PathVariable Integer id) { + return R.ok(genRecordService.removeById(id)); + } + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GeneratorController.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GeneratorController.java new file mode 100644 index 000000000..be97fa249 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/controller/GeneratorController.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.controller; + +import cn.hutool.core.io.IoUtil; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.code.entity.GenConfig; +import com.pig4cloud.pigx.code.service.GeneratorService; +import com.pig4cloud.pigx.common.core.util.R; +import lombok.AllArgsConstructor; +import lombok.SneakyThrows; +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpHeaders; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * 代码生成器 + * + * @author lengleng + * @date 2018-07-30 + */ +@RestController +@AllArgsConstructor +@RequestMapping("/generator") +public class GeneratorController { + private final GeneratorService generatorService; + + /** + * 列表 + * + * @param tableName 参数集 + * @param id 数据源编号 + * @return 数据库表 + */ + @GetMapping("/page") + public R getPage(Page page, String tableName, String name, HttpServletRequest request) { + name = StringUtils.isEmpty(name) ? "master" : name; + request.getSession().setAttribute("dataSourceName", name); + return R.ok(generatorService.getPage(page, tableName)); + } + + /** + * 生成代码 + */ + @SneakyThrows + @PostMapping("/code") + public void generatorCode(@RequestBody GenConfig genConfig, HttpServletResponse response) { + byte[] data = generatorService.generatorCode(genConfig); + response.reset(); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s.zip", genConfig.getTableName())); + response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length)); + response.setContentType("application/octet-stream; charset=UTF-8"); + + IoUtil.write(response.getOutputStream(), Boolean.TRUE, data); + } +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/ColumnEntity.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/ColumnEntity.java new file mode 100644 index 000000000..92b0854a5 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/ColumnEntity.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.entity; + +import lombok.Data; + +/** + * @author lengleng + * @date 2018/07/29 + * 列属性: https://blog.csdn.net/lkforce/article/details/79557482 + */ +@Data +public class ColumnEntity { + /** + * 列表 + */ + private String columnName; + /** + * 数据类型 + */ + private String dataType; + /** + * 备注 + */ + private String comments; + + /** + * 驼峰属性 + */ + private String caseAttrName; + /** + * 普通属性 + */ + private String lowerAttrName; + /** + * 属性类型 + */ + private String attrType; + /** + * 其他信息 + */ + private String extra; + /** + * 字段类型 + */ + private String columnType; + /** + * 是否可以为空 + */ + private Boolean nullable; + /** + * 是否隐藏 + */ + private Boolean hidden; +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenConfig.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenConfig.java new file mode 100644 index 000000000..c23aa006c --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenConfig.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.entity; + +import lombok.Data; + +/** + * @author lengleng + * @date 2018/8/2 + * 生成配置 + */ +@Data +public class GenConfig { + /** + * 数据源ID + */ + private Integer id; + /** + * 包名 + */ + private String packageName; + /** + * 作者 + */ + private String author; + /** + * 模块名称 + */ + private String moduleName; + /** + * 表前缀 + */ + private String tablePrefix; + + /** + * 表名称 + */ + private String tableName; + + /** + * 表备注 + */ + private String comments; +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenDatasourceConf.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenDatasourceConf.java new file mode 100644 index 000000000..d2965821f --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenDatasourceConf.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ +package com.pig4cloud.pigx.code.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.time.LocalDateTime; + +/** + * 数据源表 + * + * @author lengleng + * @date 2019-03-31 16:00:20 + */ +@Data +@TableName("gen_datasource_conf") +@EqualsAndHashCode(callSuper = true) +public class GenDatasourceConf extends Model { + private static final long serialVersionUID = 1L; + + /** + * 主键 + */ + @TableId + private Integer id; + /** + * 名称 + */ + private String name; + /** + * jdbcurl + */ + private String url; + /** + * 用户名 + */ + private String username; + /** + * 密码 + */ + private String password; + /** + * 创建时间 + */ + private LocalDateTime createDate; + /** + * 更新 + */ + private LocalDateTime updateDate; + /** + * 删除标记 + */ + @TableLogic + private String delFlag; + /** + * 租户ID + */ + private Integer tenantId; + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenFormConf.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenFormConf.java new file mode 100644 index 000000000..4c6ae7d56 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/GenFormConf.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.time.LocalDateTime; + +/** + * 生成记录 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +@Data +@TableName("gen_form_conf") +@EqualsAndHashCode(callSuper = true) +@ApiModel(value = "生成记录") +public class GenFormConf extends Model { + private static final long serialVersionUID = 1L; + + /** + * ID + */ + @TableId + @ApiModelProperty(value = "ID") + private Integer id; + /** + * 表名称 + */ + @ApiModelProperty(value = "表名称") + private String tableName; + /** + * 表单信息 + */ + @ApiModelProperty(value = "表单信息") + private String formInfo; + /** + * 创建时间 + */ + @ApiModelProperty(value = "创建时间") + private LocalDateTime createTime; + /** + * 修改时间 + */ + @ApiModelProperty(value = "修改时间") + private LocalDateTime updateTime; + /** + * 删除标记 + */ + @ApiModelProperty(value = "删除标记") + private String delFlag; + /** + * 所属租户 + */ + @ApiModelProperty(value = "所属租户", hidden = true) + private Integer tenantId; +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/TableEntity.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/TableEntity.java new file mode 100644 index 000000000..4f0cf207c --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/entity/TableEntity.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.entity; + +import lombok.Data; + +import java.util.List; + +/** + * @author lengleng + * @date 2018/07/29 + * 表属性: https://blog.csdn.net/lkforce/article/details/79557482 + */ +@Data +public class TableEntity { + /** + * 名称 + */ + private String tableName; + /** + * 备注 + */ + private String comments; + /** + * 主键 + */ + private ColumnEntity pk; + /** + * 列名 + */ + private List columns; + /** + * 驼峰类型 + */ + private String caseClassName; + /** + * 普通类型 + */ + private String lowerClassName; +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GenDatasourceConfMapper.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GenDatasourceConfMapper.java new file mode 100644 index 000000000..c761e1cbc --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GenDatasourceConfMapper.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ +package com.pig4cloud.pigx.code.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.pig4cloud.pigx.code.entity.GenDatasourceConf; + +/** + * 数据源表 + * + * @author lengleng + * @date 2019-03-31 16:00:20 + */ +public interface GenDatasourceConfMapper extends BaseMapper { + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GenFormConfMapper.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GenFormConfMapper.java new file mode 100644 index 000000000..145faf1a1 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GenFormConfMapper.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.mapper; + +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.pig4cloud.pigx.code.entity.GenFormConf; + +/** + * 生成记录 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +@DS("master") +public interface GenFormConfMapper extends BaseMapper { + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GeneratorMapper.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GeneratorMapper.java new file mode 100644 index 000000000..975b08d16 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/mapper/GeneratorMapper.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.mapper; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Map; + +/** + * 代码生成器 + * + * @author lengleng + * @date 2018-07-30 + */ +public interface GeneratorMapper { + + /** + * 分页查询表格 + * + * @param page + * @param tableName + * @return + */ + IPage>> queryList(Page page, @Param("tableName") String tableName); + + /** + * 查询表信息 + * + * @param tableName 表名称 + * @return + */ + Map queryTable(String tableName); + + /** + * 查询表列信息 + * + * @param tableName 表名称 + * @return + */ + List> queryColumns(String tableName); +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GenDatasourceConfService.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GenDatasourceConfService.java new file mode 100644 index 000000000..c441b5cec --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GenDatasourceConfService.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ +package com.pig4cloud.pigx.code.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.pig4cloud.pigx.code.entity.GenDatasourceConf; + +/** + * 数据源表 + * + * @author lengleng + * @date 2019-03-31 16:00:20 + */ +public interface GenDatasourceConfService extends IService { + /** + * 保存数据源并且加密 + * + * @param genDatasourceConf + * @return + */ + Boolean saveDsByEnc(GenDatasourceConf genDatasourceConf); + + /** + * 更新数据源 + * + * @param genDatasourceConf + * @return + */ + Boolean updateDsByEnc(GenDatasourceConf genDatasourceConf); +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GenFormConfService.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GenFormConfService.java new file mode 100644 index 000000000..c6468db33 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GenFormConfService.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.pig4cloud.pigx.code.entity.GenFormConf; + +/** + * 表单管理 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +public interface GenFormConfService extends IService { + + /** + * 获取表单信息 + * + * @param dsId 数据源ID + * @param tableName 表名称 + * @return + */ + String getForm(Integer dsId, String tableName); + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GeneratorService.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GeneratorService.java new file mode 100644 index 000000000..5186eb6cc --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/GeneratorService.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.service; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.code.entity.GenConfig; + +import java.util.List; +import java.util.Map; + +/** + * @author lengleng + * @date 2018/7/29 + */ +public interface GeneratorService { + /** + * 生成代码 + * + * @param tableNames 表名称 + * @return + */ + byte[] generatorCode(GenConfig tableNames); + + /** + * 分页查询表 + * + * @param page 分页信息 + * @param tableName 表名 + * @return + */ + IPage>> getPage(Page page, String tableName); +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GenDatasourceConfServiceImpl.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GenDatasourceConfServiceImpl.java new file mode 100644 index 000000000..49b6deea1 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GenDatasourceConfServiceImpl.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ +package com.pig4cloud.pigx.code.service.impl; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.pig4cloud.pigx.code.entity.GenDatasourceConf; +import com.pig4cloud.pigx.code.mapper.GenDatasourceConfMapper; +import com.pig4cloud.pigx.code.service.GenDatasourceConfService; +import lombok.AllArgsConstructor; +import org.jasypt.encryption.StringEncryptor; +import org.springframework.stereotype.Service; + +/** + * 数据源表 + * + * @author lengleng + * @date 2019-03-31 16:00:20 + */ +@Service +@AllArgsConstructor +public class GenDatasourceConfServiceImpl extends ServiceImpl implements GenDatasourceConfService { + private final StringEncryptor stringEncryptor; + + /** + * 保存数据源并且加密 + * + * @param genDatasourceConf + * @return + */ + @Override + public Boolean saveDsByEnc(GenDatasourceConf genDatasourceConf) { + genDatasourceConf.setPassword(stringEncryptor.encrypt(genDatasourceConf.getPassword())); + this.baseMapper.insert(genDatasourceConf); + return true; +// return dynamicDataSourceConfig.reload(); + } + + /** + * 更新数据源 + * + * @param genDatasourceConf + * @return + */ + @Override + public Boolean updateDsByEnc(GenDatasourceConf genDatasourceConf) { + if (StrUtil.isNotBlank(genDatasourceConf.getPassword())) { + genDatasourceConf.setPassword(stringEncryptor.encrypt(genDatasourceConf.getPassword())); + } + this.baseMapper.updateById(genDatasourceConf); + return true; +// return dynamicDataSourceConfig.reload(); + } +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GenFormConfServiceImpl.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GenFormConfServiceImpl.java new file mode 100644 index 000000000..7c033efd3 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GenFormConfServiceImpl.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ +package com.pig4cloud.pigx.code.service.impl; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.StrUtil; +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.pig4cloud.pigx.code.entity.ColumnEntity; +import com.pig4cloud.pigx.code.entity.GenFormConf; +import com.pig4cloud.pigx.code.mapper.GenFormConfMapper; +import com.pig4cloud.pigx.code.mapper.GeneratorMapper; +import com.pig4cloud.pigx.code.service.GenFormConfService; +import com.pig4cloud.pigx.code.util.GenUtils; +import lombok.AllArgsConstructor; +import lombok.SneakyThrows; +import org.apache.commons.lang.StringUtils; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; +import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; +import org.springframework.stereotype.Service; + +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * 表单管理 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +@Service +@AllArgsConstructor +public class GenFormConfServiceImpl extends ServiceImpl implements GenFormConfService { + private final GeneratorMapper generatorMapper; + + /** + * 1. 根据数据源、表名称,查询已配置表单信息 + * 2. 不存在调用模板生成 + * + * @param dsId 数据源ID + * @param tableName 表名称 + * @return + */ + @DS("pigxx") + @Override + @SneakyThrows + public String getForm(Integer dsId, String tableName) { + GenFormConf form = getOne(Wrappers.lambdaQuery() + .eq(GenFormConf::getTableName, tableName) + .orderByDesc(GenFormConf::getCreateTime), false); + + if (form != null) { + return form.getFormInfo(); + } + +// DynamicDataSourceContextHolder.setDataSourceType(dsId); + List> columns = generatorMapper.queryColumns(tableName); + //设置velocity资源加载器 + Properties prop = new Properties(); + prop.put("file.resource.loader.class", ClasspathResourceLoader.class.getName()); + Velocity.init(prop); + Template template = Velocity.getTemplate("template/crud.js.vm", CharsetUtil.UTF_8); + VelocityContext context = new VelocityContext(); + + List columnList = new ArrayList<>(); + for (Map column : columns) { + ColumnEntity columnEntity = new ColumnEntity(); + columnEntity.setComments(column.get("columnComment")); + columnEntity.setLowerAttrName(StringUtils.uncapitalize(GenUtils.columnToJava(column.get("columnName")))); + columnList.add(columnEntity); + } + context.put("columns", columnList); + StringWriter writer = new StringWriter(); + template.merge(context, writer); + return StrUtil.trim(StrUtil.removePrefix(writer.toString(), GenUtils.CRUD_PREFIX)); + } + +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GeneratorServiceImpl.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GeneratorServiceImpl.java new file mode 100644 index 000000000..72e94731f --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/service/impl/GeneratorServiceImpl.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.service.impl; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.StrUtil; +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.code.entity.GenConfig; +import com.pig4cloud.pigx.code.entity.GenFormConf; +import com.pig4cloud.pigx.code.mapper.GenFormConfMapper; +import com.pig4cloud.pigx.code.mapper.GeneratorMapper; +import com.pig4cloud.pigx.code.service.GeneratorService; +import com.pig4cloud.pigx.code.util.GenUtils; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +import java.io.ByteArrayOutputStream; +import java.util.List; +import java.util.Map; +import java.util.zip.ZipOutputStream; + +/** + * @author lengleng + * @date 2018-07-30 + *

+ * 代码生成器 + */ +@Service +@AllArgsConstructor +public class GeneratorServiceImpl implements GeneratorService { + private final GeneratorMapper generatorMapper; + private final GenFormConfMapper genFormConfMapper; + + /** + * 分页查询表 + * + * @param tableName 查询条件 + * @return + */ + @DS("#session.dataSourceName") + @Override + public IPage>> getPage(Page page, String tableName) { + return generatorMapper.queryList(page, tableName); + } + + /** + * 生成代码 + * + * @param genConfig 生成配置 + * @return + */ + @DS("#session.dataSourceName") + @Override + public byte[] generatorCode(GenConfig genConfig) { + //根据tableName 查询最新的表单配置 + List formConfList = genFormConfMapper.selectList(Wrappers.lambdaQuery() + .eq(GenFormConf::getTableName, genConfig.getTableName()) + .orderByDesc(GenFormConf::getCreateTime)); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ZipOutputStream zip = new ZipOutputStream(outputStream); + + String tableNames = genConfig.getTableName(); + for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) { + //查询表信息 + Map table = queryTable(tableName); + //查询列信息 + List> columns = queryColumns(tableName); + //生成代码 + if (CollUtil.isNotEmpty(formConfList)) { + GenUtils.generatorCode(genConfig, table, columns, zip, formConfList.get(0)); + } else { + GenUtils.generatorCode(genConfig, table, columns, zip, null); + } + } + IoUtil.close(zip); + return outputStream.toByteArray(); + } + + private Map queryTable(String tableName) { + return generatorMapper.queryTable(tableName); + } + + private List> queryColumns(String tableName) { + return generatorMapper.queryColumns(tableName); + } +} diff --git a/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/util/GenUtils.java b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/util/GenUtils.java new file mode 100644 index 000000000..8907bdc96 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/java/com/pig4cloud/pigx/code/util/GenUtils.java @@ -0,0 +1,312 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.code.util; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.StrUtil; +import com.pig4cloud.pigx.code.entity.ColumnEntity; +import com.pig4cloud.pigx.code.entity.GenConfig; +import com.pig4cloud.pigx.code.entity.GenFormConf; +import com.pig4cloud.pigx.code.entity.TableEntity; +import com.pig4cloud.pigx.common.core.constant.CommonConstants; +import com.pig4cloud.pigx.common.core.exception.CheckedException; +import lombok.SneakyThrows; +import lombok.experimental.UtilityClass; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.WordUtils; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; + +import java.io.File; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * 代码生成器 工具类 + * + * @author lengleng + * @date 2018-07-30 + */ +@Slf4j +@UtilityClass +public class GenUtils { + public final String CRUD_PREFIX = "export const tableOption ="; + private final String ENTITY_JAVA_VM = "Entity.java.vm"; + private final String MAPPER_JAVA_VM = "Mapper.java.vm"; + private final String SERVICE_JAVA_VM = "Service.java.vm"; + private final String SERVICE_IMPL_JAVA_VM = "ServiceImpl.java.vm"; + private final String CONTROLLER_JAVA_VM = "Controller.java.vm"; + private final String MAPPER_XML_VM = "Mapper.xml.vm"; + private final String MENU_SQL_VM = "menu.sql.vm"; + private final String INDEX_VUE_VM = "index.vue.vm"; + private final String API_JS_VM = "api.js.vm"; + private final String CRUD_JS_VM = "crud.js.vm"; + + private List getTemplates() { + List templates = new ArrayList<>(); + templates.add("template/Entity.java.vm"); + templates.add("template/Mapper.java.vm"); + templates.add("template/Mapper.xml.vm"); + templates.add("template/Service.java.vm"); + templates.add("template/ServiceImpl.java.vm"); + templates.add("template/Controller.java.vm"); + templates.add("template/menu.sql.vm"); + + templates.add("template/index.vue.vm"); + templates.add("template/api.js.vm"); + templates.add("template/crud.js.vm"); + return templates; + } + + /** + * 生成代码 + */ + @SneakyThrows + public void generatorCode(GenConfig genConfig, Map table, + List> columns, ZipOutputStream zip, GenFormConf formConf) { + //配置信息 + Configuration config = getConfig(); + boolean hasBigDecimal = false; + //表信息 + TableEntity tableEntity = new TableEntity(); + tableEntity.setTableName(table.get("tableName")); + + if (StrUtil.isNotBlank(genConfig.getComments())) { + tableEntity.setComments(genConfig.getComments()); + } else { + tableEntity.setComments(table.get("tableComment")); + } + + String tablePrefix; + if (StrUtil.isNotBlank(genConfig.getTablePrefix())) { + tablePrefix = genConfig.getTablePrefix(); + } else { + tablePrefix = config.getString("tablePrefix"); + } + + //表名转换成Java类名 + String className = tableToJava(tableEntity.getTableName(), tablePrefix); + tableEntity.setCaseClassName(className); + tableEntity.setLowerClassName(StringUtils.uncapitalize(className)); + //获取需要在swagger文档中隐藏的属性字段 + List hiddenColumns = config.getList("hiddenColumn"); + //列信息 + List columnList = new ArrayList<>(); + for (Map column : columns) { + ColumnEntity columnEntity = new ColumnEntity(); + columnEntity.setColumnName(column.get("columnName")); + columnEntity.setDataType(column.get("dataType")); + columnEntity.setComments(column.get("columnComment")); + 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); + } + //列名转换成Java属性名 + String attrName = columnToJava(columnEntity.getColumnName()); + columnEntity.setCaseAttrName(attrName); + columnEntity.setLowerAttrName(StringUtils.uncapitalize(attrName)); + + //列的数据类型,转换成Java类型 + String attrType = config.getString(columnEntity.getDataType(), "unknowType"); + columnEntity.setAttrType(attrType); + if (!hasBigDecimal && "BigDecimal".equals(attrType)) { + hasBigDecimal = true; + } + //是否主键 + if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) { + tableEntity.setPk(columnEntity); + } + + columnList.add(columnEntity); + } + tableEntity.setColumns(columnList); + + //没主键,则第一个字段为主键 + if (tableEntity.getPk() == null) { + tableEntity.setPk(tableEntity.getColumns().get(0)); + } + + //设置velocity资源加载器 + Properties prop = new Properties(); + prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); + Velocity.init(prop); + //封装模板数据 + Map map = new HashMap<>(16); + map.put("tableName", tableEntity.getTableName()); + map.put("pk", tableEntity.getPk()); + map.put("className", tableEntity.getCaseClassName()); + map.put("classname", tableEntity.getLowerClassName()); + map.put("pathName", tableEntity.getLowerClassName().toLowerCase()); + map.put("columns", tableEntity.getColumns()); + map.put("hasBigDecimal", hasBigDecimal); + map.put("datetime", DateUtil.now()); + + if (StrUtil.isNotBlank(genConfig.getComments())) { + map.put("comments", genConfig.getComments()); + } else { + map.put("comments", tableEntity.getComments()); + } + + if (StrUtil.isNotBlank(genConfig.getAuthor())) { + map.put("author", genConfig.getAuthor()); + } else { + map.put("author", config.getString("author")); + } + + if (StrUtil.isNotBlank(genConfig.getModuleName())) { + map.put("moduleName", genConfig.getModuleName()); + } else { + map.put("moduleName", config.getString("moduleName")); + } + + if (StrUtil.isNotBlank(genConfig.getPackageName())) { + map.put("package", genConfig.getPackageName()); + map.put("mainPath", genConfig.getPackageName()); + } else { + map.put("package", config.getString("package")); + map.put("mainPath", config.getString("mainPath")); + } + VelocityContext context = new VelocityContext(map); + + //获取模板列表 + List templates = getTemplates(); + for (String template : templates) { + // 如果是crud + if (template.contains(CRUD_JS_VM) && formConf != null) { + zip.putNextEntry(new ZipEntry(Objects + .requireNonNull(getFileName(template, tableEntity.getCaseClassName() + , map.get("package").toString(), map.get("moduleName").toString())))); + IoUtil.write(zip, StandardCharsets.UTF_8, false, CRUD_PREFIX + formConf.getFormInfo()); + zip.closeEntry(); + continue; + } + + + //渲染模板 + StringWriter sw = new StringWriter(); + Template tpl = Velocity.getTemplate(template, CharsetUtil.UTF_8); + tpl.merge(context, sw); + + //添加到zip + zip.putNextEntry(new ZipEntry(Objects + .requireNonNull(getFileName(template, tableEntity.getCaseClassName() + , map.get("package").toString(), map.get("moduleName").toString())))); + IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString()); + IoUtil.close(sw); + zip.closeEntry(); + } + } + + + /** + * 列名转换成Java属性名 + */ + public String columnToJava(String columnName) { + return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", ""); + } + + /** + * 表名转换成Java类名 + */ + private String tableToJava(String tableName, String tablePrefix) { + if (StringUtils.isNotBlank(tablePrefix)) { + tableName = tableName.replaceFirst(tablePrefix, ""); + } + return columnToJava(tableName); + } + + /** + * 获取配置信息 + */ + private Configuration getConfig() { + try { + return new PropertiesConfiguration("generator.properties"); + } catch (ConfigurationException e) { + throw new CheckedException("获取配置文件失败,", e); + } + } + + /** + * 获取文件名 + */ + private String getFileName(String template, String className, String packageName, String moduleName) { + String packagePath = CommonConstants.BACK_END_PROJECT + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator; + if (StringUtils.isNotBlank(packageName)) { + packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator; + } + + if (template.contains(ENTITY_JAVA_VM)) { + return packagePath + "entity" + File.separator + className + ".java"; + } + + if (template.contains(MAPPER_JAVA_VM)) { + return packagePath + "mapper" + File.separator + className + "Mapper.java"; + } + + if (template.contains(SERVICE_JAVA_VM)) { + return packagePath + "service" + File.separator + className + "Service.java"; + } + + if (template.contains(SERVICE_IMPL_JAVA_VM)) { + return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java"; + } + + if (template.contains(CONTROLLER_JAVA_VM)) { + return packagePath + "controller" + File.separator + className + "Controller.java"; + } + + if (template.contains(MAPPER_XML_VM)) { + return CommonConstants.BACK_END_PROJECT + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + className + "Mapper.xml"; + } + + if (template.contains(MENU_SQL_VM)) { + return className.toLowerCase() + "_menu.sql"; + } + + if (template.contains(INDEX_VUE_VM)) { + return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "views" + + File.separator + moduleName + File.separator + className.toLowerCase() + File.separator + "index.vue"; + } + + if (template.contains(API_JS_VM)) { + return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "api" + File.separator + className.toLowerCase() + ".js"; + } + + if (template.contains(CRUD_JS_VM)) { + return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "const" + + File.separator + "crud" + File.separator + className.toLowerCase() + ".js"; + } + + return null; + } +} diff --git a/pigx-visual/pigx-code/src/main/resources/bootstrap.yml b/pigx-visual/pigx-code/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..370893775 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/bootstrap.yml @@ -0,0 +1,16 @@ +server: + port: 5003 + +spring: + application: + name: @artifactId@ + cloud: + nacos: + discovery: + server-addr: ${NACOS-HOST:pigx-register}:${NACOS-PORT:8848} + config: + server-addr: ${spring.cloud.nacos.discovery.server-addr} + file-extension: yml + shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + profiles: + active: @profiles.active@ \ No newline at end of file diff --git a/pigx-visual/pigx-code/src/main/resources/generator.properties b/pigx-visual/pigx-code/src/main/resources/generator.properties new file mode 100644 index 000000000..dfe6d305a --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/generator.properties @@ -0,0 +1,37 @@ +#\u4EE3\u7801\u751F\u6210\u5668\uFF0C\u914D\u7F6E\u4FE1\u606F + +mainPath=com.pig4cloud.pigx +#\u5305\u540D +package=com.pig4cloud.pigx +moduleName=generator +#\u4F5C\u8005 +author=pigx code generator + +#\u8868\u524D\u7F00(\u7C7B\u540D\u4E0D\u4F1A\u5305\u542B\u8868\u524D\u7F00) +tablePrefix=tb_ + +#\u7C7B\u578B\u8F6C\u6362\uFF0C\u914D\u7F6E\u4FE1\u606F +tinyint=Integer +smallint=Integer +mediumint=Integer +int=Integer +integer=Integer +bigint=Long +float=Float +double=Double +decimal=BigDecimal +bit=Boolean + +char=String +varchar=String +tinytext=String +text=String +mediumtext=String +longtext=String + +date=LocalDateTime +datetime=LocalDateTime +timestamp=LocalDateTime + +# hidden columns in Swagger Models +hiddenColumn=tenant_id diff --git a/pigx-visual/pigx-code/src/main/resources/mapper/GenFormConfMapper.xml b/pigx-visual/pigx-code/src/main/resources/mapper/GenFormConfMapper.xml new file mode 100644 index 000000000..22270ac54 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/mapper/GenFormConfMapper.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + diff --git a/pigx-visual/pigx-code/src/main/resources/mapper/GeneratorMapper.xml b/pigx-visual/pigx-code/src/main/resources/mapper/GeneratorMapper.xml new file mode 100644 index 000000000..7ef05fc4d --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/mapper/GeneratorMapper.xml @@ -0,0 +1,23 @@ + + + + + + + + + + diff --git a/pigx-visual/pigx-code/src/main/resources/template/Controller.java.vm b/pigx-visual/pigx-code/src/main/resources/template/Controller.java.vm new file mode 100644 index 000000000..9eb5102e8 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/Controller.java.vm @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package ${package}.${moduleName}.controller; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.log.annotation.SysLog; +import ${package}.${moduleName}.entity.${className}; +import ${package}.${moduleName}.service.${className}Service; +import org.springframework.security.access.prepost.PreAuthorize; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.*; + + +/** + * ${comments} + * + * @author ${author} + * @date ${datetime} + */ +@RestController +@AllArgsConstructor +@RequestMapping("/${pathName}" ) +@Api(value = "${pathName}", tags = "${comments}管理") +public class ${className}Controller { + + private final ${className}Service ${classname}Service; + + /** + * 分页查询 + * @param page 分页对象 + * @param ${classname} ${comments} + * @return + */ + @ApiOperation(value = "分页查询", notes = "分页查询") + @GetMapping("/page" ) + public R get${className}Page(Page page, ${className} ${classname}) { + return R.ok(${classname}Service.page(page, Wrappers.query(${classname}))); + } + + + /** + * 通过id查询${comments} + * @param ${pk.lowerAttrName} id + * @return R + */ + @ApiOperation(value = "通过id查询", notes = "通过id查询") + @GetMapping("/{${pk.lowerAttrName}}" ) + public R getById(@PathVariable("${pk.lowerAttrName}" ) ${pk.attrType} ${pk.lowerAttrName}) { + return R.ok(${classname}Service.getById(${pk.lowerAttrName})); + } + + /** + * 新增${comments} + * @param ${classname} ${comments} + * @return R + */ + @ApiOperation(value = "新增${comments}", notes = "新增${comments}") + @SysLog("新增${comments}" ) + @PostMapping + @PreAuthorize("@pms.hasPermission('${moduleName}_${pathName}_add')" ) + public R save(@RequestBody ${className} ${classname}) { + return R.ok(${classname}Service.save(${classname})); + } + + /** + * 修改${comments} + * @param ${classname} ${comments} + * @return R + */ + @ApiOperation(value = "修改${comments}", notes = "修改${comments}") + @SysLog("修改${comments}" ) + @PutMapping + @PreAuthorize("@pms.hasPermission('${moduleName}_${pathName}_edit')" ) + public R updateById(@RequestBody ${className} ${classname}) { + return R.ok(${classname}Service.updateById(${classname})); + } + + /** + * 通过id删除${comments} + * @param ${pk.lowerAttrName} id + * @return R + */ + @ApiOperation(value = "通过id删除${comments}", notes = "通过id删除${comments}") + @SysLog("通过id删除${comments}" ) + @DeleteMapping("/{${pk.lowerAttrName}}" ) + @PreAuthorize("@pms.hasPermission('${moduleName}_${pathName}_del')" ) + public R removeById(@PathVariable ${pk.attrType} ${pk.lowerAttrName}) { + return R.ok(${classname}Service.removeById(${pk.lowerAttrName})); + } + +} diff --git a/pigx-visual/pigx-code/src/main/resources/template/Entity.java.vm b/pigx-visual/pigx-code/src/main/resources/template/Entity.java.vm new file mode 100644 index 000000000..eb9a71379 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/Entity.java.vm @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package ${package}.${moduleName}.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +#if(${hasBigDecimal}) +import java.math.BigDecimal; +#end +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * ${comments} + * + * @author ${author} + * @date ${datetime} + */ +@Data +@TableName("${tableName}") +@EqualsAndHashCode(callSuper = true) +@ApiModel(value = "${comments}") +public class ${className} extends Model<${className}> { +private static final long serialVersionUID = 1L; + + #foreach ($column in $columns) +/** + * $column.comments + */ + #if($column.columnName == $pk.columnName) +@TableId + #end +@ApiModelProperty(value="$column.comments"#if($column.hidden),hidden=$column.hidden#end) + private $column.attrType $column.lowerAttrName; + #end +} diff --git a/pigx-visual/pigx-code/src/main/resources/template/Mapper.java.vm b/pigx-visual/pigx-code/src/main/resources/template/Mapper.java.vm new file mode 100644 index 000000000..867859b51 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/Mapper.java.vm @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package ${package}.${moduleName}.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import ${package}.${moduleName}.entity.${className}; + +/** + * ${comments} + * + * @author ${author} + * @date ${datetime} + */ +public interface ${className}Mapper extends BaseMapper<${className}> { + +} diff --git a/pigx-visual/pigx-code/src/main/resources/template/Mapper.xml.vm b/pigx-visual/pigx-code/src/main/resources/template/Mapper.xml.vm new file mode 100644 index 000000000..864e3d9ea --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/Mapper.xml.vm @@ -0,0 +1,35 @@ + + + + + + + + + + #foreach($column in $columns) + #if($column.lowerAttrName==$pk.lowerAttrName) + + #else + + #end + #end + + diff --git a/pigx-visual/pigx-code/src/main/resources/template/Service.java.vm b/pigx-visual/pigx-code/src/main/resources/template/Service.java.vm new file mode 100644 index 000000000..5d808ef57 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/Service.java.vm @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package ${package}.${moduleName}.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import ${package}.${moduleName}.entity.${className}; + +/** + * ${comments} + * + * @author ${author} + * @date ${datetime} + */ +public interface ${className}Service extends IService<${className}> { + +} diff --git a/pigx-visual/pigx-code/src/main/resources/template/ServiceImpl.java.vm b/pigx-visual/pigx-code/src/main/resources/template/ServiceImpl.java.vm new file mode 100644 index 000000000..bce38eeea --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/ServiceImpl.java.vm @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ +package ${package}.${moduleName}.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import ${package}.${moduleName}.entity.${className}; +import ${package}.${moduleName}.mapper.${className}Mapper; +import ${package}.${moduleName}.service.${className}Service; +import org.springframework.stereotype.Service; + +/** + * ${comments} + * + * @author ${author} + * @date ${datetime} + */ +@Service +public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service { + +} diff --git a/pigx-visual/pigx-code/src/main/resources/template/api.js.vm b/pigx-visual/pigx-code/src/main/resources/template/api.js.vm new file mode 100644 index 000000000..e257ce767 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/api.js.vm @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +import request from '@/router/axios' + +export function fetchList(query) { + return request({ + url: '/${moduleName}/${pathName}/page', + method: 'get', + params: query + }) +} + +export function addObj(obj) { + return request({ + url: '/${moduleName}/${pathName}', + method: 'post', + data: obj + }) +} + +export function getObj(id) { + return request({ + url: '/${moduleName}/${pathName}/' + id, + method: 'get' + }) +} + +export function delObj(id) { + return request({ + url: '/${moduleName}/${pathName}/' + id, + method: 'delete' + }) +} + +export function putObj(obj) { + return request({ + url: '/${moduleName}/${pathName}', + method: 'put', + data: obj + }) +} diff --git a/pigx-visual/pigx-code/src/main/resources/template/crud.js.vm b/pigx-visual/pigx-code/src/main/resources/template/crud.js.vm new file mode 100644 index 000000000..0c9e51fa2 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/crud.js.vm @@ -0,0 +1,17 @@ +export const tableOption = { + "border": true, + "index": true, + "indexLabel": "序号", + "stripe": true, + "menuAlign": "center", + "align": "center", + "column": [ +#foreach ($column in $columns) + { + "type": "input", + "label": "$column.comments", + "prop": "$column.lowerAttrName" + }#if($foreach.hasNext),#end +#end + ] +} diff --git a/pigx-visual/pigx-code/src/main/resources/template/index.vue.vm b/pigx-visual/pigx-code/src/main/resources/template/index.vue.vm new file mode 100644 index 000000000..47fd26051 --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/index.vue.vm @@ -0,0 +1,121 @@ + + + + \ No newline at end of file diff --git a/pigx-visual/pigx-code/src/main/resources/template/menu.sql.vm b/pigx-visual/pigx-code/src/main/resources/template/menu.sql.vm new file mode 100644 index 000000000..c8a353d2a --- /dev/null +++ b/pigx-visual/pigx-code/src/main/resources/template/menu.sql.vm @@ -0,0 +1,16 @@ +-- 该脚本不要直接执行, 注意维护菜单的父节点ID 默认 父节点1000 + +-- 菜单SQL +insert into `sys_menu` ( `parent_id`, `path`, `permission`, `type`, `icon`, `del_flag`, `create_time`, `sort`, `update_time`, `name`) + values ( '1000', '/${moduleName}/${pathName}/index', '', '0', 'icon-bangzhushouji', '0', '2018-01-20 13:17:19', '8', '2018-07-29 13:38:19', '${comments}管理'); + +-- 按钮父菜单ID +set @parentId = @@identity; + +-- 菜单对应按钮SQL +insert into `sys_menu` ( `parent_id`, `permission`, `type`, `path`, `icon`, `del_flag`, `create_time`, `sort`, `update_time`, `name`) + SELECT @parentId, '${moduleName}_${pathName}_add', '1', null, '1', '0', '2018-05-15 21:35:18', '0', '2018-07-29 13:38:59', '${comments}新增'; +insert into `sys_menu` ( `parent_id`, `permission`, `type`, `path`, `icon`, `del_flag`, `create_time`, `sort`, `update_time`, `name`) + SELECT @parentId, '${moduleName}_${pathName}_edit', '1', null, '1', '0', '2018-05-15 21:35:18', '1', '2018-07-29 13:38:59', '${comments}修改'; +insert into `sys_menu` ( `parent_id`, `permission`, `type`, `path`, `icon`, `del_flag`, `create_time`, `sort`, `update_time`, `name`) + SELECT @parentId, '${moduleName}_${pathName}_del', '1', null, '1', '0', '2018-05-15 21:35:18', '2', '2018-07-29 13:38:59', '${comments}删除'; diff --git a/pigx-visual/pom.xml b/pigx-visual/pom.xml index af477a99e..dbdfa87e8 100644 --- a/pigx-visual/pom.xml +++ b/pigx-visual/pom.xml @@ -43,5 +43,6 @@ pigx-sentinel-dashboard pigx-tx-manager pigx-dynamic-datasource + pigx-code From 7687eb13481a173867fc7418189b5d60f2a54b2d Mon Sep 17 00:00:00 2001 From: liuyes Date: Fri, 20 Dec 2019 21:03:52 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-visual/pigx-code/README.md | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pigx-visual/pigx-code/README.md diff --git a/pigx-visual/pigx-code/README.md b/pigx-visual/pigx-code/README.md new file mode 100644 index 000000000..ea4825ff0 --- /dev/null +++ b/pigx-visual/pigx-code/README.md @@ -0,0 +1,45 @@ +## 使用说明 +``` +本模块是pigx-codegen的重写版本,使用dynamic-datasource实现,去除pigx-common-datasource依赖,功能未做扩展。 +``` +### pigx-code-dev.yml +``` +## spring security 配置 +security: + oauth2: + client: + client-id: ENC(gPFcUOmJm8WqM3k3eSqS0Q==) + client-secret: ENC(gPFcUOmJm8WqM3k3eSqS0Q==) + scope: server + resources: + static-locations: classpath:/static/,classpath:/views/ + +# 数据源 +spring: + datasource: + dynamic: + username: ${MYSQL-USER:root} + password: ${MYSQL-USER:root} + url: jdbc:mysql://${MYSQL-HOST:pigx-mysql}:${MYSQL-PORT:3306}/pigxx_codegen?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true + driverClassName: com.mysql.cj.jdbc.Driver + sql: select * from gen_datasource_conf + + +pigx: + tenant: + column: tenant_id + tables: + - sys_datasource_conf +``` +### 示例中的pigxx_codegen数据库及sql说明 +``` +pigxx_codegen数据库中gen_datasource_conf表必须存在一个name为master的数据库连接配置 + +INSERT INTO `gen_datasource_conf` VALUES ('8', 'master', 'jdbc:mysql://pigx-mysql:3306/pigxx_codegen?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true', 'root', 'ZkxR9PxP6sWvasdc9CG9Ww==', '2019-09-18 09:44:55', '2019-12-17 17:07:17', '0', null); +``` +### yml配置文件切换数据源 +``` +请参考:https://git.pig4cloud.com/pigx/pigx/src/liuyes_dev/pigx-visual/pigx-dynamic-datasource +``` +### 联系我 +QQ:55215085 \ No newline at end of file From be502be2b3bcb8d8dc97d3ee6adef2873cc70ccf Mon Sep 17 00:00:00 2001 From: liuyes Date: Fri, 20 Dec 2019 21:12:59 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-visual/pigx-code/README.md | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/pigx-visual/pigx-code/README.md b/pigx-visual/pigx-code/README.md index ea4825ff0..fc2b73f3a 100644 --- a/pigx-visual/pigx-code/README.md +++ b/pigx-visual/pigx-code/README.md @@ -37,6 +37,51 @@ pigxx_codegen数据库中gen_datasource_conf表必须存在一个name为master INSERT INTO `gen_datasource_conf` VALUES ('8', 'master', 'jdbc:mysql://pigx-mysql:3306/pigxx_codegen?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true', 'root', 'ZkxR9PxP6sWvasdc9CG9Ww==', '2019-09-18 09:44:55', '2019-12-17 17:07:17', '0', null); ``` +### 修改菜单的url +``` +开发平台菜单下的/gen全部改为/code +如 /gen/datasource 改为 /code/datasource +``` +### 修改动态路由 +``` +{ + "routeId": "pigx-codegen", + "routeName": "代码生成模块", + "predicates": [ + { + "args": { + "_genkey_0": "/gen/**" + }, + "name": "Path" + } + ], + "filters": [], + "uri": "lb://pigx-codegen", + "order": 0, + "createTime": "2019-10-16 16:44:41", + "updateTime": "2019-11-05 22:36:58", + "delFlag": "0" + }, +改为 +{ + "routeId": "pigx-code", + "routeName": "多数据源代码生成模块", + "predicates": [ + { + "args": { + "_genkey_0": "/code/**" + }, + "name": "Path" + } + ], + "filters": [], + "uri": "lb://pigx-code", + "order": 0, + "createTime": "2019-10-16 16:44:41", + "updateTime": "2019-11-05 22:36:58", + "delFlag": "0" + }, +``` ### yml配置文件切换数据源 ``` 请参考:https://git.pig4cloud.com/pigx/pigx/src/liuyes_dev/pigx-visual/pigx-dynamic-datasource