A web application to generate Java source code with spring-boot and mybatis-plus

Overview

代码生成服务

基于人人开源项目二次开发,做了大量提升优化:

  1. 集成nacos配置中心
  2. 支持不重启代码生成服务动态刷新com.zaxxer.hikari.HikariDataSource数据连接池更换所需生成基础业务代码的数据库,意味着你可以不重启服务动态切换任意数据库:MySQL -> MySQL, MySQL -> Oracle, Oracle-> PostgreSQL, PostgreSQL -> MySQL, ...
  3. 支持不重启代码生成服务动态刷新数据库基础数据类型和java基础包装类型的映射关系,见配置文件:generator.properties
  4. 生成代码的BaseResult类去这里下载
  5. JDK8 or latest required
  6. LocalDateTime, LocalDate的序列化、反序列化至少需要jackson-jsr310以上maven依赖模块, springboot 版本大于等于 springboot 2.4.x不需要

核心配置

资源结构:

image-20210911233940569

  • bootstrap.yml
  1. 从配置中心加载配置文件元数据
  2. 提前去nacos中的创建对应的命名空间,配置信息参看如下:
spring:
  application:
    name: lejing-generator
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        group: DEFAULT_GROUP
        namespace: b3bf69ba-f696-4479-aae1-94807a56eb90
        refresh-enabled: true
        file-extension: yml
        shared-configs:
          - application.yml
        extension-configs:
          - data-id: datasource.yml
            group: DEFAULT_GROUP
            refresh: true
          - data-id: generator.properties
            group: DEFAULT_GROUP
            refresh: true

说明:

(1)spring-cloud-alibaba-starters版本:2021.1

(2)nacos版本: 2.0.3

参考图:

image-20210911235730813

image-20210911235851616

完成以上操作,你的nacos已经配置好了, 接下来你只需要动态变更datasource.ymlgenerator.properties里面的配置数据即可

  • datasource.yml
  1. 做为nacos配置文件的dataId动态切换数据库
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.40.132:3306/lejing_job?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
#指定要生成代码的数据库类型, 可选值: MYSQL, ORACLE, SQLSERVER, POSTGRESQL, MONGODB
code:
  generator:
    db-type: MYSQL

#mongodb:
#  host: localhost
#  port: 27017
#  auth: false #是否使用密码验证
#  username: admin
#  password: 123456
#  source: 123456
#  database: mongodb_test
  • generator.properties
#
# 代码生成配置信息
#
#域名|组织名取反
mainPath=cn.alphahub
#包名
package=cn.alphahub.mall
#模块名称
moduleName=sys
#作者名字
author=Weasley J
#作者邮箱
email[email protected]
#表前缀(指定表前缀生成代码的java类名不会包含表前缀, 不指定表前缀表示类名包含表前缀, 如: sys_)
tablePrefix=
#代码生成后下载的zip包名称
codeZipFileName=lejing-job
#数据库的数据类型给与Java的数据类型对应关系
enum=String
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
longblob=String
datetime=LocalDateTime
timestamp=LocalDateTime
date=LocalDate
time=LocalTime
NUMBER=Integer
INT=Integer
INTEGER=Integer
BINARY_INTEGER=Integer
LONG=String
FLOAT=Float
BINARY_FLOAT=Float
DOUBLE=Double
BINARY_DOUBLE=Double
DECIMAL=BigDecimal
CHAR=String
VARCHAR=String
VARCHAR2=String
NVARCHAR=String
NVARCHAR2=String
CLOB=String
BLOB=String
DATE=LocalDateTime
DATETIME=LocalDateTime
TIMESTAMP=LocalDateTime
TIMESTAMP(6)=LocalDateTime
int8=Long
int4=Integer
int2=Integer
numeric=BigDecimal
nvarchar=String

这个配置文件注释很细,相信你一看就懂,不做过多说明

项目说明

  • 在线生成domainxmlmapperservicecontroller、前端vue文件、jssql代码,减少70%以上的开发任务, 通常情况下删除生成的controller文件, 然后根据自己的业务场景编写对应的业务接口,持久层基于mybatis-plus
  • 整合smart-doc,执行项目的 mvn package 可直接输出Restful api,支持调试,你可能需要在你的项目中引入lejing-common/lejing-common-base-public模块和配置smart-doc.json文件,smart-doc是一个不错的api文档生成、管理工具。

image-20210912002146693

  • 生成的接口:获取xx分页列表、获取xx详情、保存xx、修改xx、批量删除xx

image-20210228214229671

image-20210228214348571

本地部署

  • 通过git下载源码:
git clone https://github.com/Weasley-J/lejing-mall
  • 安装配置好nacos
  1. 创建对应的命名空间,直接从项目文件lejing-generator/src/main/resources/bootstrap.yml里面复制对应的参数即可。

image-20210912000740199

  1. 创建命名空间对应的配置文件,元数据直接从lejing-generator/src/main/resources/下面复制,改成自己的业务数据库。

image-20210912001057849

  • 修改application.yml,更新MySQL账号和密码、数据库名称,修改成你自己的业务数据库连接配置。
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.40.132:3306/lejing_job?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
#指定要生成代码的数据库类型, 可选值: MYSQL, ORACLE, SQLSERVER, POSTGRESQL, MONGODB
code:
  generator:
    db-type: MYSQL

#mongodb:
#  host: localhost
#  port: 27017
#  auth: false #是否使用密码验证
#  username: admin
#  password: 123456
#  source: 123456
#  database: mongodb_test
  • 修改generator.properties文件,重点修改下面这几项:
#
# 代码生成配置信息
#
#域名|组织名取反
mainPath=cn.alphahub
#包名
package=cn.alphahub.mall
#模块名称
moduleName=coupon
#作者
author=Weasley J
#email
email[email protected]
#表前缀(指定表前缀生成代码的java类名不会包含表前缀, 不指定表前缀表示类名包含表前缀, 如: sms_)
tablePrefix=sms_
#代码生成后下载的zip包名称
codeZipFileName=lejing-coupon
# ...
  • Eclipse、IDEA运行CodeGeneratorApplication.java,则可启动项目
  • 项目访问路径:http://localhost:8080

演示效果图: image-20210912001738403

知识点分享

  1. nacos里面的dataIdxxx.properties的配置文件如何转为项目中java代码里面的Properties对象
代码生成配置信息 * * @return nacos中存储的Properties配置文件 */ public Properties getGeneratorProperties() { Properties queryProperties = new Properties(); queryProperties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); queryProperties.put(PropertyKeyConst.NAMESPACE, namespace); try { ConfigService configService = NacosFactory.createConfigService(queryProperties); String config = configService.getConfig(dataId, group, 3000L); if (StringUtils.isNotBlank(config)) { Properties properties = new Properties(); properties.load(new StringReader(config)); return properties; } else { return PropertiesLoaderUtils.loadAllProperties("generator.properties"); } } catch (Exception e) { throw new BizException("获取配置文件失败,", e); } } } ">
import cn.alphahub.mall.generator.utils.BizException;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Service;

import java.io.StringReader;
import java.util.Properties;

/**
 * 代码生成核心类
 *
 * @author Mark [email protected], lwj
 */
@Data
@Slf4j
@Service
@RefreshScope
public class SysGeneratorService {

    @Value("${spring.cloud.nacos.server-addr:127.0.0.1:8848}")
    private String serverAddr;

    @Value("${spring.cloud.nacos.config.namespace:}")
    private String namespace;

    private String dataId = "generator.properties";

    private String group = "DEFAULT_GROUP";

    /**
     * 将nacos中存储的Properties配置文件取出来
     * 

代码生成配置信息 * * @return nacos中存储的Properties配置文件 */ public Properties getGeneratorProperties() { Properties queryProperties = new Properties(); queryProperties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); queryProperties.put(PropertyKeyConst.NAMESPACE, namespace); try { ConfigService configService = NacosFactory.createConfigService(queryProperties); String config = configService.getConfig(dataId, group, 3000L); if (StringUtils.isNotBlank(config)) { Properties properties = new Properties(); properties.load(new StringReader(config)); return properties; } else { return PropertiesLoaderUtils.loadAllProperties("generator.properties"); } } catch (Exception e) { throw new BizException("获取配置文件失败,", e); } } }

上面的getGeneratorProperties()方法用来将动态监听generator.properties配置信息的改变,并且将其转换为java.util.Properties对象供其他业务类调用,读取方式使用NacosFactory直接实时读取最新的配置数据,其中的PropertiesLoaderUtils类为spring提供的工具类,直接读取classpath下的xxx.properties文件然后返回java.util.Properties对象。

You might also like...

mall-swarm是一套微服务商城系统,采用了 Spring Cloud Hoxton & Alibaba、Spring Boot 2.3、Oauth2、MyBatis、Docker、Elasticsearch、Kubernetes等核心技术,同时提供了基于Vue的管理后台方便快速搭建系统。mall-swarm在电商业务的基础集成了注册中心、配置中心、监控中心、网关等系统功能。文档齐全,附带全套Spring Cloud教程。

mall-swarm是一套微服务商城系统,采用了 Spring Cloud Hoxton & Alibaba、Spring Boot 2.3、Oauth2、MyBatis、Docker、Elasticsearch、Kubernetes等核心技术,同时提供了基于Vue的管理后台方便快速搭建系统。mall-swarm在电商业务的基础集成了注册中心、配置中心、监控中心、网关等系统功能。文档齐全,附带全套Spring Cloud教程。

mall-swarm 友情提示 快速体验项目:在线访问地址。 全套学习教程:《mall学习教程》。 Spring Cloud全套教程:《SpringCloud学习教程》。 专属学习路线:学习不走弯路,整理了套非常不错的《mall专属学习路线》。 项目交流:想要加群交流项目的朋友,可以加入mall项目

Jan 3, 2023

开源论坛、问答系统,现有功能提问、回复、通知、最新、最热、消除零回复功能。功能持续更新中…… 技术栈 Spring、Spring Boot、MyBatis、MySQL/H2、Bootstrap

开源论坛、问答系统,现有功能提问、回复、通知、最新、最热、消除零回复功能。功能持续更新中…… 技术栈 Spring、Spring Boot、MyBatis、MySQL/H2、Bootstrap

码问社区 在线演示地址 www.mawen.co 功能列表 开源论坛、问答系统,现有功能提问、回复、通知、最新、最热、消除零回复功能。功能持续更新中…… 技术栈 技术 链接 Spring Boot http://projects.spring.io/spring-boot/#quick-start

Dec 30, 2022

Spring Boot starter for JustAuth Plus.

Spring Boot starter for JustAuth Plus.

Jun 23, 2022

Two Spring-boot applications registering themselves to an spring-boot-admin-server application as separate clients for the purpose of monitoring and managing the clients

Two Spring-boot applications registering themselves to an spring-boot-admin-server application as separate clients for the purpose of monitoring and managing the clients

Spring-boot-admin implementation with 1 Server and 2 clients Creating a Server application to monitor and manage Spring boot applications (clients) un

Dec 6, 2022

Spring Boot JdbcTemplate example with SQL Server: CRUD Rest API using Spring Data JDBC, Spring Web MVC

Spring Boot JdbcTemplate example with SQL Server: Build CRUD Rest API Build a Spring Boot CRUD Rest API example that uses Spring Data Jdbc to make CRU

Dec 20, 2022

The goal of the project is to create a web application using Java EE and database (PostgreSQL) without connecting a modern technology stack like spring boot and hibernate

The goal of the project is to create a web application using Java EE and database (PostgreSQL) without connecting a modern technology stack like spring boot and hibernate

About The Project SignIn page SignUp page Profile page The goal of the project is to create a web application using Java EE and database (PostgreSQL)

Mar 23, 2022

Spring Boot Migrator (SBM) - a tool for automated code migrations to upgrade or migrate to Spring Boot

Spring Boot Migrator (SBM) - a tool for automated code migrations to upgrade or migrate to Spring Boot

Spring Boot Migrator uses and is compatible to OpenRewrite, a powerful mass refactoring ecosystem for Java and other source code.

Jan 2, 2023

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login example with Spring Security, MySQL and JWT Appropriate Flow for User Login and Registration with JWT Spring Boot Rest Api Architect

Jan 5, 2023

Community extension to generate a Java client from the provided Camunda 7 OpenAPI descitpion and also warp it into Spring Boot

Camunda Engine OpenAPI REST Client Java and Spring Boot This community extension is a convenience wrapper around the generated Java client from the Ca

Dec 28, 2022
Releases(1.0.0)
Owner
Weasley
Keep going and never stop try.
Weasley
参考 DDD/Clean Architecture 设计理念,整合 Spring Boot/Spring Security/Mybatis Plus/Vavr 的 Spring Realworld 应用案例

Demo · 更多项目 · 参考资料 ms-spring-ddd-examples Unified Domain-driven Layered Architecture for MicroService Apps,试图探索一套切实可行的应用架构规范,可以复制、可以理解、可以落地、可以控制复杂性的指导

王下邀月熊 19 Sep 23, 2022
该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的线上博客项目/企业大型商城系统/前后端分离实践项目等,摆脱各种 hello world 入门案例的束缚,真正的掌握 Spring Boot 开发。

Spring Boot Projects 该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的前

十三 4.5k Dec 30, 2022
Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding

Everyone can develop projects independently, quickly and efficiently! What is spring-boot-plus? A easy-to-use, high-speed, high-efficient, feature-ric

geekidea 2.3k Dec 31, 2022
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Batch、Spring Cloud、Spring Cloud Alibaba、Spring Security & Spring Security OAuth2,博客Spring系列源码:https://mrbird.cc

Spring 系列教程 该仓库为个人博客https://mrbird.cc中Spring系列源码,包含Spring Boot、Spring Boot & Shiro、Spring Cloud,Spring Boot & Spring Security & Spring Security OAuth2

mrbird 24.8k Jan 6, 2023
【多模块微服务脚手架平台——Ancba】前后端分离架构SpringBoot 2.x、SpringCloud、SpringAdmin、Spring Security、Mybatis-plus、(Shiro)、JWT、Feign、Nacos、Knif4j等。

Ancba 打造Blog.Core项目的SpringBoot微服务版,但是更强大 ?? Ancba (Another New CLI By Alacrity) 另一个全新的敏捷脚手架(单体/模块化/微服务都可支持)。 核心知识点与进度 ?? 在 ..../resources/application-

ansonzhang 35 Nov 29, 2022
Guns基于SpringBoot 2,致力于做更简洁的后台管理系统,完美整合springmvc + shiro + mybatis-plus + beetl!Guns项目代码简洁,注释丰富,上手容易,同时Guns包含许多基础模块(用户管理,角色管理,部门管理,字典管理等10个模块),可以直接作为一个后台管理系统的脚手架!

Guns基于Spring Boot2,致力于做更简洁的后台管理系统。包含系统管理,代码生成,多数据库适配,SSO单点登录,工作流,短信,邮件发送,OAuth2登录,任务调度,持续集成,docker部署等功。支持Spring Cloud Alibaba微服务。社区活跃,版本迭代快,加群免费技术支持。

冯硕楠 3.6k Jan 5, 2023
以教学为目的的电商系统。包含ToB复杂业务、互联网高并发业务、缓存应用;DDD、微服务指导。模型驱动、数据驱动。了解大型服务进化路线,编码技巧、学习Linux,性能调优。Docker/k8s助力、监控、日志收集、中间件学习。前端技术、后端实践等。主要技术:SpringBoot+JPA+Mybatis-plus+Antd+Vue3。

简介 bcMall 是一个以教学为目的的电商系统。bcMall将为你展现一个典型的系统演进过程,所使用的主流技术完全开放。 它包含ToB复杂业务、互联网高并发业务、缓存应用;DDD、微服务指导。模型驱动、数据驱动。了解大型服务进化路线,编码技巧、学习Linux,性能调优。Docker/k8s助力、监

xjjdog 411 Jan 3, 2023
基于RuoYi-Vue集成 Lombok+Mybatis-Plus+Undertow+knife4j+Hutool+Feign 重写所有原生业务 定期与RuoYi-Vue同步

平台简介 RuoYi-Vue-Plus 是基于 RuoYi-Vue 针对 分布式集群 场景升级 定期与 RuoYi-Vue 同步 集成 Lock4j dynamic-datasource 等分布式场景解决方案 集成 Mybatis-Plus Lombok Hutool 等便捷开发工具 适配重写相关业

CrazyLionLi 110 Jan 4, 2023
SpringBoot SpringSecurity Jpa mybatis-plus websocket Redis camunda Vue3 Vite ant-design VbenAdmin vxe-table bpmn.js

SpringBoot SpringSecurity Jpa mybatis-plus websocket Redis camunda Vue3 Vite ant-design VbenAdmin vxe-table bpmn.js

zsvg 16 Dec 13, 2022