一、介绍
本教程将介绍如何使用 mybatis-plus 工具自动给我们生成 Controller、Service、Entity、Mapper、Mapper.xml 层代码
要求:
① 生成的Controller类
② 生成的Entity 类
③ 生成的 Service,默认名称下是以 I 开头的接口, 在生成Service层代码中需要把这个 I 去掉
二,环境准备
1,数据库
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`age` int(0) NULL DEFAULT NULL COMMENT '年龄',
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`version` int(0) NULL DEFAULT 1,
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
`deleted` int(0) NULL DEFAULT 0,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'Jone', 18, 'test1@baomidou.com', 1, '2021-06-19 21:30:20', '2021-06-19 21:30:20', 0);
INSERT INTO `user` VALUES (2, 'Jack', 20, 'test2@baomidou.com', 1, '2021-06-19 21:30:20', '2021-06-19 21:30:20', 0);
INSERT INTO `user` VALUES (3, 'Tom', 28, 'test3@baomidou.com', 1, '2021-06-19 21:30:20', '2021-06-19 21:30:20', 0);
INSERT INTO `user` VALUES (4, 'Sandy', 21, 'test4@baomidou.com', 1, '2021-06-19 21:30:20', '2021-06-19 21:30:20', 0);
INSERT INTO `user` VALUES (5, 'Billie', 24, 'test5@baomidou.com', 1, '2021-06-19 21:30:20', '2021-06-19 21:30:20', 1);
SET FOREIGN_KEY_CHECKS = 1;
2, 创建 一个SpringBoot 项目
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s7TD3SL5-1624248860175)(/images/blogPictureFile/7d6a36c2-74df-463e-8e8a-02b343ce81c8.png)]
3,pom
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mysql connection-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--springboot jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot web-->
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3,创建代码生成类
package com.demo;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class kunCode {
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
//1、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
//生成路径(一般都是生成在此项目的src/main/java下面)
gc.setOutputDir(projectPath + "/src/main/java");
//设置作者
gc.setAuthor("kun");
gc.setOpen(false);
//第二次生成会把第一次生成的覆盖掉
gc.setFileOverride(true);
//生成的service接口名字首字母是否为I,这样设置就没有
gc.setServiceName("%sService");
//生成resultMap
gc.setBaseResultMap(true);
mpg.setGlobalConfig(gc);
//2、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 3、包配置
PackageConfig pc = new PackageConfig();
//pc.setModuleName("com.example");
//你项目的根目录:com.example.....
pc.setParent("com.demo");
mpg.setPackageInfo(pc);
// 4、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 设置controller层时会默认继承的类:要写全路径
//strategy.setSuperControllerClass("com.kun.controller");
// 设置entity层时会默认继承的类:要写全路径
//strategy.setSuperEntityClass("com.kun.entity");
// 去除表名前缀
strategy.setTablePrefix("t_");
//使用lombok
strategy.setEntityLombokModel(true);
strategy.setInclude("user"); // 逆向工程使用的表 如果要生成多个,这里可以传入String[]
mpg.setStrategy(strategy);
//5、执行
mpg.execute();
}
}