目录
一、 Mybatis_Plus简介
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
二、 代码生成器-AutoGenerator
1. AutoGenerator是什么?
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Pojo、Mapper、
Mapper XML、Service、Controller 等各个模块的代码
2. AutoGenerator能干什么?
对于单表而言,几乎是一个全能的工具,极大的提升了开发效率。更多的关注业务逻辑的实现。
3. 怎么使用?
创建一个AutoGenerator项目
AutoGenerator本身和我们项目没有关联,所以可以单独新建为一个Project,这边也做成Maven聚
合项目里的一个子项目
注意
适用版本:mybatis-plus-generator 3.5.1 及其以上版本,对历史版本不兼容!3.5.1 以下的请参考
代码生成器旧
4. 创建Generator子项目
4.1 项目创建
父项目右键单击-> new -> Module
这里选择maven项目,选择quickstart
父工程pom.xml文件下自动更新插入
<modules>
<module>cloud_e_generator</module>
</modules>
4.2 添加依赖
对cloud_e_generator项目下的pom.xml文件进行依赖添加
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
完整pom文件如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud_e</artifactId>
<groupId>com.chuci</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud_e_generator</artifactId>
<name>cloud_e_generator</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--web 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus 依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<!--mybatis-plus 代码生成器依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<!--freemarker 依赖-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!--mysql 依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
4.3 驱动代码
代码简述:
String url = 数据库URL;
String username = 用户名;
String password = 密码;
这里注意mybatis配置文件Mapper的生成目录,同时保证数据表名称是否一一对应。更多代码生成器配置
模板引擎配置,默认 Velocity 可选模板引擎 Beetl 或 Freemarker
.templateEngine(new BeetlTemplateEngine())
.templateEngine(new FreemarkerTemplateEngine())
package com.chuci;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Collections;
/**
* 执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
*
* @author zhoubin
* @since 1.0.0
*/
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static void main(String[] args) {
// 代码生成器
String url = "jdbc:mysql://localhost:3306/db_cloud_e?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
String username = "root";
String password = "root";
FastAutoGenerator.create(url, username, password)
.globalConfig(builder -> {
builder.author("chuci") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("F:\\Users\\ChuGe\\ProjectCollection\\CloudE\\CloudE_Platform\\CloudE\\cloud_e\\cloud_e_generator\\src\\main\\java\\com\\chuci\\generate"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.chuci") // 设置父包名
.moduleName("server") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "F:\\Users\\ChuGe\\ProjectCollection\\CloudE\\CloudE_Platform\\CloudE\\cloud_e\\cloud_e_generator\\src\\main\\java\\com\\chuci\\generate\\mappers")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("t_admin", "t_admin_role", "t_appraise", "t_department", "t_employee",
"t_employee_ec", "t_employee_remove", "t_employee_train", "t_joblevel", "t_mail_log",
"t_menu", "t_menu_role", "t_nation", "t_oplog", "t_politics_status",
"t_position", "t_role", "t_salary", "t_salary_adjust", "t_sys_msg", "t_sys_msg_content") // 设置需要生成的表名
.addTablePrefix("t_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
4.4 代码生成
运行代码开始自动生成controller层,mapper层, entry层(实体类), server层以及server实现层impl,以及mybatis映射文件等。
最后,将整个文件复制粘贴到对应文件夹即可(或者在生成之前即可配置好生成代码的路径,一边一次性生成)
至此,本节完~~~
上一节: Cloud E随笔-后端_piece1–项目搭建
下一节:Cloud E随笔-后端_piece3–实现登录功能
此 系 列 以 完 整 记 录 自 己 项 目 经 历 此系列以完整记录自己项目经历 此系列以完整记录自己项目经历