1、引入maven

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency> <dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>

application.yml 增加

mybatis-plus:
#扫描mapper文件所在位置
mapper-locations: classpath*:mapper/**/*Mapper.xml
#可以指定实体类所在包路径
typeAliasesPackage: com.rnce.model
global-config:
banner: false
db-config:
# 主键类型 0:数据库ID自增 1.未定义 2.用户输入 3 id_worker 4.uuid 5.id_worker字符串表示
id-type: AUTO
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: NOT_NULL
# 默认数据库表下划线命名
table-underline: true
# configuration:
# map-underscore-to-camel-case: false
# cache-enabled: true #配置的缓存的全局开关
# lazyLoadingEnabled: true #延时加载的开关
# multipleResultSetsEnabled: true #开启的话,延时加载一个属性时会加载该对象全部属性,否则按需加载属性
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句,调试用 spring:
#DATABASE CONFIG
datasource:
#driver-class-name: com.mysql.jdbc.Driver
#driver-class-name: com.p6spy.engine.spy.P6SpyDriver
driverClassName: com.mysql.cj.jdbc.Driver
username: dsk
password: Hz56
url: jdbc:mysql://106.103:3306/dwk?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource #这里是配置druid连接池,以下都是druid的配置信息
druid:
# 初始连接数
initialSize: 5
# 最小连接池数量
minIdle: 10
# 最大连接池数量
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
testWhileIdle: true
validationQuery: SELECT 1 FROM DUAL
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username:
login-password:
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true

启动器上增加 要把Mapper接口文件的包扫描进去

代码生成类

CodeGenerator.java

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
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.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; public class CodeGenerator {
public static void main(String[] args) { // 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator(); // 2、全局配置
GlobalConfig gc = new GlobalConfig();
//获取工作目录
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
//作者
gc.setAuthor("yvioo");
//生成后是否打开资源管理器
gc.setOpen(false);
//重新生成时文件是否覆盖
gc.setFileOverride(false);
//去掉Service接口的首字母
gc.setServiceName("%sService");
gc.setIdType(IdType.AUTO); //主键策略
gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
gc.setSwagger2(false);//开启Swagger2模式 mpg.setGlobalConfig(gc); // 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://1003:3306/mazc?serverTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("mc");
dsc.setPassword("Hz56");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc); // 4、包配置
PackageConfig pc = new PackageConfig();
//模块名
pc.setModuleName(null);
//指定生成的根目录
pc.setParent("com.test.cms.mybatisplus"); //以下设置的包名要在上面的目录下 //控制器的包名
pc.setController("controller");
//实体类的包名
pc.setEntity("entity"); pc.setService("service");
//实现类的包名
pc.setServiceImpl("service.impl");
//映射文件的包名
pc.setMapper("mapper");
mpg.setPackageInfo(pc); // 5、策略配置
StrategyConfig strategy = new StrategyConfig(); //设置要生成的代码表名
strategy.setInclude("mrevice"); //数据库表映射到实体的命名策略
strategy.setNaming(NamingStrategy.underline_to_camel); //生成实体时去掉表前缀
strategy.setTablePrefix(pc.getModuleName() + "_"); //数据库表字段映射到实体的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setEntityLombokModel(true); //restful api风格控制器
strategy.setRestControllerStyle(true); //url中驼峰转连字符
strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); // 6、执行
mpg.execute();
}
}

SpringBoot整合mybatis-plus并实现代码自动生成的更多相关文章

  1. SpringBoot 整合 mybatis 开启驼峰命名规则自动转换

    引言 在使用 MyBatis 进行实际项目开发时,如果数据库表字段名与Java 实体类属性名不一致,映射时则需要编写表字段列表与 Java 实体类属性的映射关系,即resultMap,如下: < ...

  2. springboot整合mybatis通用Mapper

    参考: https://blog.csdn.net/x18707731829/article/details/82814095 https://www.jianshu.com/p/6d2103451d ...

  3. Springboot整合Mybatis实现级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  4. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  5. SpringBoot整合mybatis——配置mybatis驼峰命名规则自动转换

    一.简述 mybatis驼峰式命名规则自动转换: 使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”: 使用好处:省去mapper.xml文件中繁琐编写表字段列表 ...

  6. springboot整合mybatis,redis,代码(二)

    一 说明: springboot整合mybatis,redis,代码(一) 这个开发代码的复制粘贴,可以让一些初学者直接拿过去使用,且没有什么bug 二 对上篇的说明 可以查看上图中文件: 整个工程包 ...

  7. springboot整合mybatis,利用mybatis-genetor自动生成文件

    springboot整合mybatis,利用mybatis-genetor自动生成文件 项目结构: xx 实现思路: 1.添加依赖 <?xml version="1.0" e ...

  8. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  9. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  10. SpringBoot整合Mybatis完整详细版

    记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架,立马感叹:SpringBoot是世界上最好的框架.哈哈! 当初跟着教程练习搭建了一个框架,传送门:spring boot + ...

随机推荐

  1. [yLOI2018] 锦鲤抄

    先思考图上是\(tag\)的特殊情况. 考虑我们按拓扑序反过来操作,就可以得到我们任意想要的顺序. 那么我们把所有的图都缩点操作,那么我们只需要考虑一个联通分量里就行了. 一个联通分量最后只会剩下一个 ...

  2. 关于 n 个 [0,1] 的随机变量第 k 小的期望值

    今天做到一道题,感觉里面一个结论有点意思,就到网上扒了篇证明(bushi)下来了. 知乎回答习惯,先抛结论,再给证明(大雾 结论:对于 \(n\) 个取值范围为 \([0,1]\) 的随机变量 \(x ...

  3. CF1156F Card Bag

    题目传送门. 题意简述:有 \(n\) 张卡牌,每张卡牌有数字 \(a_1,a_2,\cdots,a_n\).现在随机抽取卡牌,不放回,设本次抽到的卡牌为 \(x\),上次抽到的卡牌为 \(y\),若 ...

  4. R数据科学-1

    R数据科学(R for Data Science) Part 1:探索 by: PJX for 查漏补缺 exercise: https://jrnold.github.io/r4ds-exercis ...

  5. R语言与医学统计图形-【28】ggplot2扩展包ggrepel、ggsci、gganimate、ggpubr

    ggplot2绘图系统--扩展包ggrepel.ggsci.gganimate.ggpubr等 部分扩展包可在CRAN直接下载,有些需借助devtools包从Github下载. 1. ggrepel包 ...

  6. Linux— file命令 用于辨识文件类型

    Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...

  7. Linux搭建rsync备份服务器备份

    环境: 1台rsync备份服务器,IP:10.0.0.188 1台rsync备份客户端,IP:10.0.0.51 备份数据需注意: 1. 在业务低谷时间进行备份 2. 进行备份限速 一.搭建rsync ...

  8. jquery时间轴tab切换效果实现结合swiper实现滑动显示效果

    需求:根据时间轴进行tab页面内容切换(时间轴需要滑动查看并选择) 实现思路: 结合swiper插件实现滑动显示效果 根据transform: translateX进行左侧切换效果的实现(具体实现cs ...

  9. 学习 DDD - 通用语言的模式

    大家好,我是霸戈,这周学习了一些关于领域驱动设计的知识 ,对比较深刻的地方做了不少笔记,分享给大家. 在日常需求讨论的时候,经常会碰到一个需求会议开了一个多小时还没有达成共识.作为业务方(领域专家)明 ...

  10. 『学了就忘』Linux文件系统管理 — 67、通过命令模式进行LVM分区

    目录 1.物理卷管理 (1)准备硬盘或者分区 (2)建立物理卷 (3)查看物理卷 (3)删除物理卷 2.创建卷组 (1)建立卷组 (2)查看卷组 (3)增加卷组容量 (4)减小卷组容量 (5)删除卷组 ...