添加依赖
com.baomidou mybatis-plus-boot-starter3.4.2
另:全部依赖
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.3.RELEASE com.example demo0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.mybatis.spring.boot mybatis-spring-boot-starter2.1.1 mysql mysql-connector-javaruntime com.github.pagehelper pagehelper-spring-boot-starter1.3.0 org.projectlombok lomboktrue com.baomidou mybatis-plus-boot-starter3.4.2 org.junit.jupiter junit-jupiterRELEASE test org.springframework.boot spring-boot-maven-plugin
调整配置
去掉mybatis配置
添加mybatis plus配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
type-aliases-package: com.qcby.train.entity
mapper-locations: classpath:mapper/*.xml
# 全局配置id自增 =>
global-config:
db-config:
id-type: auto
Mybatis-plus继承了分页
设置表名
默认表名就是实体类的类名
在实体类的类名上加上@TableName注解进行表示
主键生成策略
默认MP插入数据时,在没有设置主键生成策略的情况下的策略是基于雪花算法的自增id
如果需要别的策略,在实体类中代表主键的字段上加上@TableId注解,使用其type属性指定主键生成策略
@TableId(type=IdType.AUTO) 自动增长
或在全局设置中设置
id-type:AUTO
驼峰映射
MP默认开启字段名列名的驼峰映射
userName(实体类中)——》user_name(数据库中)
自定义映射
在实体类的属性上面加@TableField("address")
如果需要关闭可以使用如下配置进行关闭
mybatis-plus: configuration: #是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射 map-underscore-to-camel-case: false
日志
如果需要打印MP操作对应的SQL语句等,可以配置日志输出。
配置方式如下:
mybatis-plus: configuration: # 日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
条件构造器Wrapper
在其子类`AbstractWrapper`中提供了很多用于构造Where条件的方法。
QueryWrapper针对select方法,查询哪些列
UpdateWrapper针对set语法的set方法,设置来对哪些列进行更新
常用AbstractWrapper方法
分页
Mapper层
Service层
Controller层



