Mybatis

您所在的位置:网站首页 代码生成器代码 Mybatis

Mybatis

2023-08-10 03:00| 来源: 网络整理| 查看: 265

1.引入依赖: com.baomidou mybatis-plus-boot-starter 3.4.3.4 com.baomidou mybatis-plus-generator 3.5.1 org.apache.velocity velocity-engine-core 2.3 org.freemarker freemarker mysql mysql-connector-java runtime

解释一下为什么要引两个模板依赖:

🐰 因为plus代码生成器需要一个模板引擎,velocity和freemarker任选一个,velocity是生成器中默认使用的,根据你的选择引依赖。

2.写一个构造器类

随便创建一个类:像启动类那样有个psvm能跑就行

public class PracticeApplication { public static void main(String[] args) { 代码生成器。。。。; } }

接下来就是写生成逻辑了,很简单,看官网:plus–代码生成器

image-20211006150757526

这是最新版的,整体的结构就是这样的,下面把我的代码生成器拿出来,并介绍一下分别有什么用(其实官网都有),完整代码放在最后:

1.全局配置(GlobalConfig)

image-20211006152120624

create方法需要传入,数据库地址(如果你的MySQL版本为8,必需要在数据库地址后面加上时区, 像serverTimezone=Asia/Shanghai这个)、用户名、密码;它会在后台根据这三个参数自动构建DataSourceConfig,而不需要我们自己写了,如图:

image-20211006152458545

最新版的生成器使用了lambda表达式,反应式编程,点点点就行了。写起来非常方便

author指定作者

outputDir,指定生成的文件往哪输出

enableSwagger,支持swagge(非常nice,记得引swagger依赖)

commentDate 时间格式

fileOveride 覆盖之前生成的文件

globalConfig的效果如图:

image-20211006153239162

2. 包配置(PackageConfig)

image-20211006153717464

这个就是配置生成哪些包:

注意:配置xml的包的方法,官网是叫mapperXml,而实际代码中的方法叫xml()

image-20211006155444599

3. 策略配置(StrategyConfig)

image-20211006155301030

addInclude()就是指定为哪些表生成代码,有几个重载:

​ 一个String的表名任意多个表名(可变长参数):“user”, “user1”,…这样列表list

所谓策略配置,就是配置策略,配置细节

它是将service、mapper、controller、entity都放到了策略配置里面,以前的版本是在全局配置中

还有个注入配置,貌似不常用。。。。。

代码的最后:

.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute();

可以指定模板引擎,

execute()执行代码生成器,生成代码

根据实际情况配置不同选项,按照上面的来就很容易完成。建议还是看官网plus–代码生成器

效果图:

image-20211006161958575

完整代码: 2022.4.7日更新:

service实现类的路径规范;代码生成器3.5.2后,xml的位置配置由OutputFile.mapperXml变为OutputFile.xml,但本文使用的3.5.1,所以下面代码并未改动很多同学反映实体类lombok的注解是@Setter和@Getter,而不是@Data。今天更新了,这个需要我们自定义模板。代码生成器的jar下的templates下有不同引擎的模板文件,我们使用的Freemarker引擎模板,所以需要添加.ftl后缀的模板。只需要复制过来,@Setter和@Getter改为@Data即可 resource目录下的templates目录下创建名为myentity.java.ftl的文件,内容如下: package ${package.Entity}; import ${pkg}; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.experimental.Accessors; /** *

* ${table.comment!} *

* * @author ${author} * @since ${date} */ @Data @Accessors(chain = true) @TableName("${schemaName}${table.name}") @ApiModel(value = "${entity}对象", description = "${table.comment!}") public class ${entity} extends ${superEntityClass} public class ${entity} extends Model public class ${entity} implements Serializable { public class ${entity} { private static final long serialVersionUID = 1L; @ApiModelProperty("${field.comment}") /** * ${field.comment} */ @TableId(value = "${field.annotationColumnName}", type = IdType.AUTO) @TableId(value = "${field.annotationColumnName}", type = IdType.${idType}) @TableId("${field.annotationColumnName}") @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill}) @TableField(fill = FieldFill.${field.fill}) @TableField("${field.annotationColumnName}") @Version @TableLogic private ${field.propertyType} ${field.propertyName}; public ${field.propertyType} ${getprefix}${field.capitalName}() { return ${field.propertyName}; } public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) { public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { this.${field.propertyName} = ${field.propertyName}; return this; } public static final String ${field.name?upper_case} = "${field.name}"; @Override public Serializable pkVal() { return this.${keyPropertyName}; return null; } @Override public String toString() { return "${entity}{" + "${field.propertyName}=" + ${field.propertyName} + ", ${field.propertyName}=" + ${field.propertyName} + "}"; } }

代码生成完整代码:

package com.xp.practice.generator; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class UserGenerator { public static void main(String[] args) { List tables = new ArrayList(); tables.add("p_user"); tables.add("p_question"); tables.add("p_answer"); tables.add("p_correct"); FastAutoGenerator.create("jdbc:mysql://localhost:3306/xpa","root","111111") .globalConfig(builder -> { builder.author("向培") //作者 .outputDir(System.getProperty("user.dir")+"\\src\\main\\java") //输出路径(写到java目录) .enableSwagger() //开启swagger .commentDate("yyyy-MM-dd") .fileOverride(); //开启覆盖之前生成的文件 }) .packageConfig(builder -> { builder.parent("com.xp") .moduleName("practice") .entity("entity") .service("service") .serviceImpl("service.impl") .controller("controller") .mapper("mapper") .xml("mapper") .pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"\\src\\main\\resources\\mapper")); }) .strategyConfig(builder -> { builder.addInclude(tables) .addTablePrefix("p_") .serviceBuilder() .formatServiceFileName("%sService") .formatServiceImplFileName("%sServiceImpl") .entityBuilder() .enableLombok() .logicDeleteColumnName("deleted") .enableTableFieldAnnotation() .controllerBuilder() // 映射路径使用连字符格式,而不是驼峰 .enableHyphenStyle() .formatFileName("%sController") .enableRestStyle() .mapperBuilder() //生成通用的resultMap .enableBaseResultMap() .superClass(BaseMapper.class) .formatMapperFileName("%sMapper") .enableMapperAnnotation() .formatXmlFileName("%sMapper"); }) .templateConfig(new Consumer() { @Override public void accept(TemplateConfig.Builder builder) { // 实体类使用我们自定义模板 builder.entity("templates/myentity.java"); } }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3