Mybatis-plus<一> Springboot框架使用MybatisPlus代码自动生成器

Mybatis-plus官网: https://mp.baomidou.com/

Demo GitHub下载地址(包含数据库建表sql,数据库数据与源代码):https://github.com/RJvon/Mybatis_plus_demo

未经作者同意请勿转载

Mybatis-plus简介

MyBatis-Plus (简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

  1. MP 提供了大量的自定义设置,生成的代码完全能够满足各类型的需求

  2. MP 的代码生成器 和 Mybatis MBG 代码生成器: MP 的代码生成器都是基于 java 代码来生成。MBG 基于 xml 文件进行代码生成 MyBatis 的代码生成器可生成: 实体类、Mapper 接口、Mapper 映射文件 MP 的代码生成器可生成: 实体类(可以选择是否支持 AR)、Mapper 接口、Mapper 映射文件、 Service 层、Controller 层.

  3. 表及字段命名策略选择 在 MP 中,我们建议数据库表名 和 表字段名采用驼峰命名方式, 如果采用下划 线命名方式 请开启全局下划线开关,如果表名字段名命名方式不一致请注解指定,我们建议最好保持一致。

这么做的原因是为了避免在对应实体类时产生的性能损耗,这样字段不用做映射就能直接和实体类对应。当然如果项目里不用考虑这点性能损耗,那么你采用下滑线也是没问题的,只需要在生成代码时配置 dbColumnUnderline 属性就可以 。

新建Springboot项目,导入相关依赖

<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency> </dependencies>

在数据库中建一个表

Demo GitHub下载地址:https://github.com/RJvon/Mybatis_plus_demo(数据库建表sql与源代码)
数据库:New_employees
新员工表:Employees
create database New_employees default charset utf8; create table Employees
(
id integer not null,
name varchar(20) not null,
sex bit default 1,
birth datetime not null,
tel char(11),
school varchar(255),
primary key (id)
); insert into Employees values
(101, '赵清华',1, '1996-5-5', '14894728324','清华大学'),
(102, '钱北大',1, '1997-6-6','14894728321', '北京大学'),
(103, '孙复旦',1, '1996-7-7', '14894728322','复旦大学'),
(104, '李交通',0, '1999-8-8', '14894728323','上海交通大学'),
(105, '周浙大',1, '1995-9-9', '14894728325','浙江大学'),
(106, '吴中科',1, '1997-10-10','14894728326', '中国科学技术大学'),
(107, '郑南京',0, '1983-11-11', '14894728327','南京大学'),
(108, '王人民',1, '1999-12-12', '14894728328','中国人民大学'),
(1001, '赵清',1, '1996-5-5', '14894728324','清华大学'),
(1002, '钱北',1, '1997-6-6','14894728321', '北京大学'),
(1003, '孙复',1, '1996-7-7', '14894728322','复旦大学'),
(1004, '李交',0, '1999-8-8', null,'上海交通大学'),
(1005, '周浙',1, '1995-9-9', '14894728325','浙江大学'),
(1006, '吴中',1, '1997-10-10','14894728326', '中国科学技术大学'),
(1007, '郑南',0, '1983-11-11', '14894728327','南京大学'),
(1008, '王人',1, '1999-12-12', '14894728328','中国人民大学');

代码自动生成器代码:

package com.von.demo;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.*;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.FileType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; /**
* @author xueqing f
* @version 1.0
* @date 2021/7/31 16:33
* Demo GitHub下载地址:https://github.com/RJvon/Mybatis_plus_demo(数据库建表sql与源代码)
*/
public class CodeGenerator { /**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
} public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator(); // 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("xueqing");
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc); // 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/New_employees?serverTimezone=GMT%2B8&useSSL=true");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc); // 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("module"));
pc.setParent("com.von.demo");
mpg.setPackageInfo(pc); // 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
}; // 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
/*// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";*/ // 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg); // 配置模板
TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null);
mpg.setTemplate(templateConfig); // 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix("_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
} }

运行CodeGenerator:

生成的代码结构:

Mybatis-plus<一> Springboot框架使用MybatisPlus代码自动生成器的更多相关文章

  1. MyBatis-plus 代码自动生成器

    MyBatis-plus  代码自动生成器 1.添加pom文件依赖 <!-- Mybatis-Plus 自动生成实体类--> <dependency> <groupId& ...

  2. MyBatisPlus性能分析插件,条件构造器,代码自动生成器详解

    性能分析插件 我们在平时的开发中,会遇到一些慢sql,测试,druid MP(MyBatisPlus)也提供性能分析插件,如果超过这个时间就停止 不过官方在3.2版本的时候取消了,原因如下 条件构造器 ...

  3. 专门为小白准备的入门级mybatis-plus-generator代码自动生成器,提高开发效率。值得收藏

    引入依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-g ...

  4. Mybatis-Plus03 代码自动生成器

    先看完Mybatis-Plus01和Mybatis-Plus02再看Mybatis-Plus03 AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerato ...

  5. SpringBoot整合MyBatis-Plus代码自动生成类

    在springboot的test测试类下创建 MpGenerator.java   配置  MpGenerator.java public class MpGenerator { @Test publ ...

  6. MybatisPlus根据模板生成器代码

    导读 网上的代码生成器,都不是自己想要的,今天下午研究了下,可以使用mybatisplus自定义模板,根据模板生成相应的代码,可以根据需求,改造相应模板即可.代码已上传github/百度云. 项目结构 ...

  7. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  8. SpringBoot框架与MyBatis集成,连接Mysql数据库

    SpringBoot是一种用来简化新Spring应用初始搭建及开发过程的框架,它使用特定方式来进行配置,使得开发人员不再需要定义样板化的配置.MyBatis是一个支持普通SQL查询.存储和高级映射的持 ...

  9. springboot+mybatis+maven角色权限框架

    发布时间:2018-10-24   技术:springboot,mybatis,maven,shiro   概述 Springboot作为基础框架,使用mybatis作为持久层框架 使用官方推荐的th ...

随机推荐

  1. 迈达斯midas Gen 2019 2.1 中文汉化安装教程

    midas Gen 2019 v2.1 for win是一款关于结构设计有限元分享的工具,分为建筑领域.桥梁领域.岩土领域.仿真领域四个大类.具有人性化的操作界面,且采用了优秀的的计算机显示技术,是建 ...

  2. 自制车速记录仪「GitHub 热点速览 v.21.31」

    作者:HelloGitHub-小鱼干 如果你有一辆普通的自行车,那么就可以使用下 X-TRACK 这个项目制作一个自己的测速器,记录你的行驶轨迹还有车速,体验一把硬件发烧友的乐趣.如果你有一个非 ma ...

  3. intouch与PLC之间通讯状态监测和设置

    intouch与PLC进行通讯状态监测中,一般做法需要PLC来实施主动脉冲计数,或者bool变化来实现.本文通过上位机自带参数设置,实现对intouch通讯状态监视,将画面恢复初始状态,并及时弹窗报警 ...

  4. Go通关03:控制结构,if、for、switch逻辑语句

    if 条件语句 func main() { i:=6 if i >10 { fmt.Println("i>10") } else if i>5 && ...

  5. a = input(a, yymmdd10.)引发的问题

    在数据清理过程中,经常会遇到以文本储存的日期型数据,这种数据不能直接进行分析,需要先将其转化为以数值存储的格式. 首先准备数据集: data data1; input a :$10. b :$10. ...

  6. 【Maven实战技巧】「插件使用专题」Maven-Assembly插件实现自定义打包

    前提概要 最近我们项目越来越多了,然后我就在想如何才能把基础服务的打包方式统一起来,并且可以实现按照我们的要求来生成,通过研究,我们通过使用maven的assembly插件完美的实现了该需求,爽爆了有 ...

  7. Feign远程调用

    有关微服务中,服务与服务如何通信,我已经给大家介绍了Ribbon远程调用的相关知识,不知道大家有没有发现Ribbon的问题呢? Ribbon的问题 在Ribbon中,如果我们想要发起一个调用,是这样的 ...

  8. OSPF多区域

    目录 一.OSPF的多区域 1.1 生成OSPF多区域的原因 1.2 路由器的类型 1.3 区域的类型 二.链路状态数据库 2.1 链路状态数据库的组成 2.2链路状态通告 三.OSPF多区域配置 四 ...

  9. 小程序中多个echarts折线图在同一个页面的使用

    最近做小程序的业务中遇到一个页面要同时显示几个echarts图,刚开始遇到各种冲突,死数据可以,动态数据就报错的问题,折磨了一天,仔细看了官网和查在各种资料之后,终于解决了. 直接上代码: commi ...

  10. 关于:org.apache.catalina.connector.ClientAbortException及getOutputStream() has already been called for this response的异常处理

    1.异常场景: 在进行将数据用word导出或者Excel导出的时候.抛出的异常getOutputStream() has already been called for this response 导 ...