一、入门案例
1、开发环境
IDE:idea 2021.3.3
JDK:JDK8+
构建工具:maven 3.5.4
MySQL版本:MySQL 8.13
Spring Boot:2.6.3
MyBatis-Plus:3.5.1
2、创建数据库及表
2.1、创建表
3、创建Spring Boot工程
3.1、使用 Spring Initializr 快速初始化一个 Spring Boot 工程
3.2修改pom.xml 文件
<?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.6.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>demo</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- mybatis-plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId> <optional>true</optional></dependency><!-- mysql-connector --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- mybatis-plus代码生成器 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.31</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
3.3 刷新Mawen
3.4 把application.properties 修改成application.yml 并配置
server:port: 8090 spring:# ???????datasource:# ???????type: com.zaxxer.hikari.HikariDataSource# ?????????driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=trueusername: rootpassword: 123123 mybatis-plus:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.example.mybatisplusdemop.entityconfiguration:# ??MyBatis?????sql?????sql??????log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:ad-cofig:#MybtisPlus????????assign_id??????id-type:
3.5 在 com.example.demo 下创建generator包
在generator包下创建一个叫DemoGenerator(随便叫什么)的class文件
复制以下内容
public static void main(String[] args) {FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8", "root", "123123").globalConfig(builder -> {builder.author("ming") // 设置作者//.enableSwagger() // 开启 swagger 模式.fileOverride() // 覆盖已生成文件.outputDir("D:\\wold"); // 指定输出目录}).packageConfig(builder -> {builder.parent("com.example") // 设置父包名.moduleName("demo") // 设置父包模块名.pathInfo(Collections.singletonMap(OutputFile.mapperXml,"D:\\wold")); // 设置mapperXml生成路径}).strategyConfig(builder -> {builder.addInclude("my_article","t_items","t_orderdetail","t_orders","t_user") // 设置需要生成的表名,多张表可以传递多个String类型的参数.addTablePrefix("my_","t_"); // 设置过滤表前缀}).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板.execute();}
3.6 运行生成文件
3.7 把生成的文件复制进项目完成