同步操作将从 jy/generate-code-plugin 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
$\color{#ff6600}{注:resources/template下的引擎模板代码不能格式化,切记,否则会导致生成的代码格式特别乱!!!}$
基于gradle构建工具并支持gradle的代码生成插件,生成代码支持类型如下:
数据库类型
支持框架及特点
支持语言
生成的代码,在mybatis版本中,是默认直接生成从dao层到controller层的CRUD,mybatis-plus则按照官方代码生成方式 目前暂时推荐使用创建独立的gradle文件,并在build.gradle或build.gradle.kts中引用即可。
为什么上传到maven仓库?第一次申请被拒绝了,而且也不是很熟悉发布流程,熟悉的可以考虑发布到maven仓库中。
开启swaggerUI如下(仅试用于springboot) java开启swaggerUI文档代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Value("${swagger.enable}")
private Boolean enable;// 此处为配置文件中的独立参数,方便部署而已,可去掉
@Bean
public Docket createRestApi() {
//添加head参数配置start
/*ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());*/
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enable)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.controller"))
.paths(PathSelectors.any())
.build();
//.globalOperationParameters(pars);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("RESTful API")
.description("swagger-bootstrap-ui")
.termsOfServiceUrl("http://localhost:9000/doc.html")
.version("1.0.0")
.contact(new Contact("author", "company website", "your email"))
.build();
}
}
kotlin开启swaggerUI文档代码
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.ApiInfo
import springfox.documentation.service.Contact
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2
@Configuration
@EnableSwagger2
class SwaggerConfiguration {
@Value("\${swagger.enable}")
private val enable // 此处为配置文件中的独立参数,方便部署而已,可去掉
: Boolean? = null
@Bean
fun createRestApi(): Docket {
return Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enable!!)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.controller"))
.paths(PathSelectors.any())
.build()
}
private fun apiInfo(): ApiInfo {
return ApiInfoBuilder()
.title("RESTful API")
.description("swagger-bootstrap-ui")
.termsOfServiceUrl("http://localhost:9000/doc.html")
.version("1.0.0")
.contact(Contact("author", "company website", "your email"))
.build()
}
}
// 指定上传的路径(上传到本地maven仓库)
def localMavenRepo = 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
uploadArchives {
repositories {
mavenDeployer {
repository(url: localMavenRepo)
pom.project {
name = project.name
packaging = 'jar'
description = 'code generate gradle task'
}
}
}
}
或者使用命令打全包
gradle jar
buildscript {
repositories {
mavenLocal()// 如果需要独立配置maven仓库地址的,使用
/*maven {
url uri(System.getProperty('user.home') + '/.m2/repository')//自定义插件已发布的仓库地址(本地maven仓库)
//url uri('自定义maven仓库地址')
}*/
//flatDir { dirs 'libs' }
}
dependencies {
//引入自定义task
classpath group: 'cn.plugins.generator', name: 'cn-plugins-generator', version: '1.0.0'
}
}
task generateCode(type: cn.plugins.generator.task.GenerateCodeTask) {
dbType = 'mysql' // 值为mysql或postgresql 由于mariadb为mysql的分支,所以是mariadb的,此处直接填写mysql即可
url = 'localhost'
port = '3306'
database = 'manager'
userName = 'root'
password = 'admin123456'
frameworkType = "mybatisplus" // mybatis或mybatisplus
tablePrefix = 'temp' // 不要带前缀下划线进来
tableName = 'temp_banner,temp_user,temp_user_role'// 多个表使用英文逗号隔开
projectPath = projectDir.path
packageName = 'com.example.gencode'
controller = "controller"
service = 'service'
mapper = 'mapper'
entity = 'entity'
xml = 'mapper'
languageType = 'java'// java或kotlin
author = 'jiangyun'
company = '测试公司'
apiPrefix = '/api'// 默认值:/api
enableGenerateDoc = true // 是否生成支持swaggerUI2文档
}
buildscript {
repositories {
mavenLocal()// 这块根据自定义maven地址而定,如果是发布的本地maven库而不是自定义的本地maven库,则直接这样使用即可,否则使用下面被注释的部分
/*maven {
url uri(System.getProperty('user.home') + '/.m2/repository')//自定义插件已发布的仓库地址
}*/
maven { url 'https://maven.aliyun.com/repository/public/' }// 必须有,否则会导致依赖失败而无法构建,除非本地maven仓库中包含有全部依赖
}
dependencies {
//引入自定义task
classpath group: 'cn.plugins.generator', name: 'cn-plugins-generator', version: '1.0.0'
}
}
task generateCode(type: cn.plugins.generator.task.GenerateCodeTask) {
dbType = 'mysql' // 值为mysql或postgresql 由于mariadb为mysql的分支,所以是mariadb的,此处直接填写mysql即可
url = 'localhost'
port = '3306'
database = 'manager'
userName = 'root'
password = 'admin123456'
frameworkType = "mybatisplus" // mybatis或mybatisplus
tablePrefix = 'temp' // 不要带前缀下划线进来
tableName = 'temp_banner,temp_user,temp_user_role'// 多个表使用英文逗号隔开
projectPath = projectDir.path
packageName = 'com.example.gencode'
controller = "controller"
service = 'service'
mapper = 'mapper'
entity = 'entity'
xml = 'mapper'
languageType = 'java'// java或kotlin
author = 'jiangyun'
company = '测试公司'
apiPrefix = '/api'// 默认值:/api
enableGenerateDoc = true // 是否生成支持swaggerUI2文档
}
apply from: 'generate.gradle'
引用gradle配置到主build.gradle配置文件中即可
想在springboot2.+的kotlin中怎么使用?目前spring官方默认给出的是使用kotlin编写DSL,不熟悉的人,就比较费劲了,文件也不再是以.gradle结束,而是.gradle.kts。 如下的脚本为kotlin编写DSL的使用方式,具体使用方式如下:
在工程目录下创建一个独立的叫generate.gradle.kts的文件,文件脚本内容如下:
import cn.plugins.generator.task.GenerateCodeTask
buildscript {
repositories {
mavenLocal()
maven {
setUrl("https://maven.aliyun.com/repository/public/")
}
dependencies {
//引入自定义task
classpath("cn.plugins.generator:cn-plugins-generator:1.0.0")
}
}
}
tasks.register<GenerateCodeTask>(name = "generateCode") {
dbType = "mysql"// 值为mysql或postgresql 由于mariadb为mysql的分支,所以是mariadb的,此处直接填写mysql即可
url = "localhost"
port = "3306"
database = "pagehelper-test"
userName = "root"
password = "admin1230"
frameworkType = "mybatis" // mybatis 或 mybatisplus
tablePrefix = "test" // 不要带前缀下划线进来
tableName = "test_login"// 多个表使用英文逗号隔开
projectPath = projectDir.path
packageName = "com.example.demo"
controller = "controller"
service = "service"
mapper = "mapper"
entity = "entity"
xml = "mapper"
languageType = "kotlin"// java或kotlin
author = "jy"
company = "测试公司"
apiPrefix = "/api"// 默认值:/api
enableGenerateDoc = true // 是否生成支持swaggerUI2文档
}
然后在build.gradle.kts文件中引用该gradle脚本:
apply(from = "generate.gradle.kts") // 特殊就在这个地方,这是kotlin编写DSL的脚本,与groovy还是有区别的 groovy的为:apply from: 'generate.gradle'
生成的kotlin代码
生成的java代码
生成的mapper xml代码
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。