1.代码生成器(新的)
官网地址:https://baomidou.com/pages/779a6e/#%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8
适用版本:mybatis-plus-generator 3.5.1 及其以上版本,对历史版本不兼容!3.5.1 以下的请参考 代码生成器旧
1.1添加依赖
模板
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>最新版本</version> </dependency>
本地我们使用
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mybatisplus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatisplus</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- <dependency>--> <!-- <groupId>com.baomidou</groupId>--> <!-- <artifactId>mybatis-plus-boot-starter</artifactId>--> <!-- <version>3.3.1</version>--> <!-- </dependency>--> <!-- mybatis-plus核心包--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!-- mybatis-plus代码生成核心包 最低3.5.1--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version> </dependency> <!-- freemarker我们实现的功能使用的freemarker引擎代码--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!--防止打war包出错--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
生成代码如下:
package com.example; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import org.springframework.boot.test.context.SpringBootTest; import java.util.Collections; @SpringBootTest public class FastAutoGeneratorTest { public static void main(String[] args) { //设置数据库的账号密码及其地址 FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false", "root", "root") .globalConfig(builder -> { builder.author("yanwc") // 设置作者 // .enableSwagger() // 开启 swagger 模式(一种测试) .fileOverride() // 覆盖已生成文件(新文件生成抵消老文件) .outputDir("D://mybatisplus"); // 指定输出目录 }) .packageConfig(builder -> { builder.parent("com.yanwc") // 设置父包名 .moduleName("mybatis") // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://mybatisplus")); // 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder.addInclude("t_user") // 设置需要生成的表名(要生成的是数据库的那个表) .addTablePrefix("t_", "c_"); // 设置过滤表前缀 }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); } }
生成之后文件夹去查找相关的代码:
我们一一简单查看下其中的内容:
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.yanwc.mybatis.mapper.UserMapper"> </mapper>
UserController.java
package com.yanwc.mybatis.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; /** * <p> * 前端控制器 * </p> * * @author yanwc * @since 2022-10-16 */ @Controller @RequestMapping("/mybatis/user") public class UserController { }
User.java
package com.yanwc.mybatis.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; /** * <p> * * </p> * * @author yanwc * @since 2022-10-16 */ @TableName("t_user") public class User implements Serializable { private static final long serialVersionUID = 1L; /** * 主键ID */ @TableId(value = "uid", type = IdType.AUTO) private Long uid; /** * 姓名 */ private String userName; /** * 年龄 */ private Integer age; /** * 邮箱 */ private String email; /** * 是否删除 */ private Integer isDelect; /** * 性别 */ private Integer sex; public Long getUid() { return uid; } public void setUid(Long uid) { this.uid = uid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getIsDelect() { return isDelect; } public void setIsDelect(Integer isDelect) { this.isDelect = isDelect; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } @Override public String toString() { return "User{" + "uid=" + uid + ", userName=" + userName + ", age=" + age + ", email=" + email + ", isDelect=" + isDelect + ", sex=" + sex + "}"; } }
UserMapper.java
package com.yanwc.mybatis.mapper; import com.yanwc.mybatis.entity.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** * <p> * Mapper 接口 * </p> * * @author yanwc * @since 2022-10-16 */ public interface UserMapper extends BaseMapper<User> { }
IUserService.java
package com.yanwc.mybatis.service; import com.yanwc.mybatis.entity.User; import com.baomidou.mybatisplus.extension.service.IService; /** * <p> * 服务类 * </p> * * @author yanwc * @since 2022-10-16 */ public interface IUserService extends IService<User> { }
UserServiceImpl.java
package com.yanwc.mybatis.service.impl; import com.yanwc.mybatis.entity.User; import com.yanwc.mybatis.mapper.UserMapper; import com.yanwc.mybatis.service.IUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; /** * <p> * 服务实现类 * </p> * * @author yanwc * @since 2022-10-16 */ @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { }
这样我们简答的代码就实现了;
2.0 代码生成器配置新
官网地址:https://baomidou.com/pages/981406/#%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE-datasourceconfig
2.1基础配置
=
属性 | 说明 | 示例 |
url | jdbc 路径 | jdbc:mysql://127.0.0.1:3306/mybatis-plus |
username | 数据库账号 | root |
password | 数据库密码 | 123456 |
new DataSourceConfig.Builder("jdbc:mysql://127.0.0.1:3306/mybatis-plus","root","123456") .build();
2.2 可选配置
方法 | 说明 | 示例 |
dbQuery(IDbQuery) | 数据库查询 | |
schema(String) | 数据库 schema(部分数据库适用) | mybatis-plus |
typeConvert(ITypeConvert) | 数据库类型转换器 | new MySqlTypeConvert() |
keyWordsHandler(IKeyWordsHandler) | 数据库关键字处理器 | new MySqlKeyWordsHandler() |
new DataSourceConfig.Builder("jdbc:mysql://127.0.0.1:3306/mybatis-plus","root","123456") .dbQuery(new MySqlQuery()) .schema("mybatis-plus") .typeConvert(new MySqlTypeConvert()) .keyWordsHandler(new MySqlKeyWordsHandler()) .build();
2.3 全局配置(GlobalConfig)
方法 | 说明 | 示例 |
fileOverride | 覆盖已生成文件 | 默认值:false |
disableOpenDir | 禁止打开输出目录 | 默认值:true |
outputDir(String) | 指定输出目录 | /opt/baomidou/ 默认值: windows:D:// linux or mac : /tmp |
author(String) | 作者名 | baomidou |
enableKotlin | 开启 | kotlin 模式 |
enableSwagger | 开启 swagger 模式 | 默认值:false |
dateType(DateType) | 时间策略 DateType.ONLY_DATE | 默认值: DateType.TIME_PACK |
commentDate(String) | 注释日期 | 默认值: yyyy-MM-dd |
new GlobalConfig.Builder() .fileOverride() .outputDir("/opt/baomidou") .author("baomidou") .enableKotlin() .enableSwagger() .dateType(DateType.TIME_PACK) .commentDate("yyyy-MM-dd") .build();
2.4 包配置(PackageConfig)
方法 | 说明 | 示例 |
parent(String) | 父包名 | 默认值:com.baomidou |
moduleName(String) | 父包模块名 | 默认值:无 |
entity(String) | Entity 包名 | 默认值:entity |
service(String) | Service 包名 | 默认值:service |
serviceImpl(String) | Service Impl 包名 | 默认值:service.impl |
mapper(String) | Mapper 包名 | 默认值:mapper |
xml(String) | Mapper XML 包名 | 默认值:mapper.xml |
controller(String) | Controller 包名 | 默认值:controller |
other(String) | 自定义文件包名 | 输出自定义文件时所用到的包名 |
pathInfo(Map<OutputFile, String>) | 路径配置信息 | Collections.singletonMap(OutputFile.mapperXml, “D://”) |
new PackageConfig.Builder() .parent("com.baomidou.mybatisplus.samples.generator") .moduleName("sys") .entity("po") .service("service") .serviceImpl("service.impl") .mapper("mapper") .xml("mapper.xml") .controller("controller") .other("other") .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://")) .build();
此处只摘录部分常用关键词更多大家可参考官网;