Mybatis-plus注意事项

字段操作

主键自增

//需要在创建数据表的时候设置主键自增
//主键字段自增策略
@TableId(type = IdType.AUTO)
#全局设置主键生成策略
mybatis-plus.global-config.db-config.id-type=auto

插入和更新时间

//插入时自动填充
@TableField(fill = FieldFill.INSERT)
//更新时自动填充
@TableField(fill = FieldFill.UPDATE)
package com.kuang.mybatisplus.handler;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
}

@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}

乐观锁

//创建数据库时的隐藏字段
@Version
private Integer version;
package com.atguigu.mybatis_plus.config;

@EnableTransactionManagement
@Configuration
@MapperScan("com.atguigu.mybatis_plus.mapper")
public class MybatisPlusConfig {
/**
* 乐观锁插件
*/
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
}

分页

@EnableTransactionManagement
@Configuration
@MapperScan("com.atguigu.mybatis_plus.mapper")
public class MybatisPlusConfig {
/**
* 乐观锁插件
*/
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
//查询
Page<User> page = new Page<>(1,5);
Page<User> pageParam = userMapper.selectPage(page, null);
//查询指定列
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("name", "age");
Page<Map<String, Object>> page = new Page<>(1, 5);
Page<Map<String, Object>> pageParam = userMapper.selectMapsPage(page, queryWrapper);

逻辑删除

@TableLogic
private Integer deleted;
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

版本

依赖 版本号
SpringBoot 2.3.7.RELEASE
SpringCloud Hoxton.SR12
SpringCloud Alibaba 2.2.8.RELEASE
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.8.RELEASE</spring-cloud-alibaba.version>

<mybatis-plus.version>3.3.1</mybatis-plus.version>
<velocity.version>2.0</velocity.version>
<swagger.version>2.7.0</swagger.version>
<aliyun.oss.version>3.1.0</aliyun.oss.version>
<jodatime.version>2.10.1</jodatime.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<commons-io.version>2.6</commons-io.version>
<commons-lang.version>3.9</commons-lang.version>
<httpclient.version>4.5.1</httpclient.version>
<jwt.version>0.7.0</jwt.version>
<aliyun-java-sdk-core.version>4.3.3</aliyun-java-sdk-core.version>
<aliyun-java-sdk-vod.version>2.15.2</aliyun-java-sdk-vod.version>
<aliyun-sdk-vod-upload.version>1.4.11</aliyun-sdk-vod-upload.version>
<fastjson.version>1.2.28</fastjson.version>
<gson.version>2.8.2</gson.version>
<json.version>20170516</json.version>
<commons-dbutils.version>1.7</commons-dbutils.version>
<canal.client.version>1.1.0</canal.client.version>
<docker.image.prefix>zx</docker.image.prefix>
<alibaba.easyexcel.version>2.1.1</alibaba.easyexcel.version>
<apache.xmlbeans.version>3.1.0</apache.xmlbeans.version>
</properties>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>

<!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>

<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>

<!--aliyunOSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>${aliyun.oss.version}</version>
</dependency>

<!--日期时间工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime.version}</version>
</dependency>

<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>

<!--commons-io-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>

<!--commons-lang3-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>

<!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>

<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>

<!--aliyun-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun-java-sdk-core.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>${aliyun-java-sdk-vod.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>com.aliyun</groupId>-->
<!--<artifactId>aliyun-sdk-vod-upload</artifactId>-->
<!--<version>${aliyun-sdk-vod-upload.version}</version>-->
<!--</dependency>-->

<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>

<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${commons-dbutils.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>${canal.client.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${alibaba.easyexcel.version}</version>
</dependency>

<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>${apache.xmlbeans.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

配置类

mybatis

@Configuration
public class MybatisConfig {

@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}

swagger

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket gatewayDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("gateway")
.apiInfo(createApiInfo("gateway","网关api"))
.select()
.paths(Predicates.and(PathSelectors.regex("/api/.**")))
.build();
}

@Bean
public Docket memberDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("member")
.apiInfo(createApiInfo("member","用户模块api"))
.select()
.paths(Predicates.and(PathSelectors.regex("/member/.**")))
.build();
}

@Bean
public Docket videoDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("video")
.apiInfo(createApiInfo("video","视频模块api"))
.select()
.paths(Predicates.and(PathSelectors.regex("/video/.**")))
.build();
}

private ApiInfo createApiInfo(String title,String description){
return new ApiInfoBuilder()
.title(title)
.description(description)
.version("1.0")
.contact(new Contact("kuang","http://www.bilibili,com","1400584782@qq.com"))
.build();
}
}

返回值

public class MyResult {

private Integer code;

private String message;

private Object data;

private MyResult(){
this.code = CodeEnum.CODE_SUCCESS.getCode();
this.message = CodeEnum.CODE_SUCCESS.getMessage();
}
private MyResult(Integer code,String message){
this.code = code;
this.message = message;
}
private MyResult(CodeEnum codeEnum){
this.code = codeEnum.getCode();
this.message = codeEnum.getMessage();
}
private MyResult(Integer code,String message,Object data){
this.code = code;
this.message = message;
this.data = data;
}
private MyResult setData(Object data){
this.data = data;
return this;
}
}
public enum CodeEnum {

CODE_SUCCESS(200,"success"),

CODE_COMMON(1000,"服务器内部错误");

private Integer code;
private String message;

private CodeEnum(Integer code,String message){
this.code = code;
this.message = message;
}

public Integer getCode(){
return this.code;
}

public String getMessage(){
return this.message;
}
}