1 . 导入pom

com.baomidoumybatis-plus-boot-starter3.4.2

2 . 配置下MybatisPlus的yml

mybatis-plus:mapper-locations:- classpath:mapper/*/*Mapper.xmlglobal-config:db-config:id-type: autobanner: trueconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3 . 实体类

@Data@TableName("sys_user")@ApiModel(value = "SysUser对象", description = "用户信息")public class SysUser implements Serializable {@TableId("SU_CODE")@ApiModelProperty(value = "用户编码")private String suCode;@TableField("SUS_CODE")@ApiModelProperty(value = "分类编码")private String susCode;@TableField("SU_NAME")@ApiModelProperty(value = "用户姓名")private String suName;@TableField("SU_SEX")@ApiModelProperty(value = "性别:0未知,1男,2女")private Integer suSex;@TableField("SU_AGE")@ApiModelProperty(value = "年龄")private Integer suAge;}

4 . DTO

@Data@ApiModel(value = "SysUserDTO对象", description = "用户信息DTO")public class SysUserDTO {@ApiModelProperty(value = "用户编码")private String suCode;@ApiModelProperty(value = "分类编码")private String susCode;@ApiModelProperty(value = "用户姓名")private String suName;@ApiModelProperty(value = "性别:0未知,1男,2女")private Integer suSex;@ApiModelProperty(value = "年龄")private Integer suAge;private Integer page = 1;private Integer limit = 10;}

5 . MybatisPlus的config

import cn.hutool.core.util.ArrayUtil;import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;import lombok.extern.slf4j.Slf4j;import net.sf.jsqlparser.expression.Expression;import net.sf.jsqlparser.expression.LongValue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Slf4j@Configurationpublic class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();log.info("进入sql拦截器");//分页拦截器插件//如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptorinterceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {//租户id(拼接sql的条件的值)@Overridepublic Expression getTenantId() {return new LongValue(1);}//追加的字段(拼接sql的条件)@Overridepublic String getTenantIdColumn() {return "SU_CODE";}//去掉实体类字段@Overridepublic boolean ignoreTable(String tableName) {String[] arr = new String[]{"susCode","suName","suSex","suAge"};return ArrayUtil.contains(arr, tableName);}}));interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}// 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false//interceptor.addInnerInterceptor(new PaginationInnerInterceptor());@Beanpublic ConfigurationCustomizer configurationCustomizer() {return configuration -> configuration.setUseDeprecatedExecutor(false);}}

6 . controller

@RestController@RequestMapping("exam")public class SysUserController {@Autowiredprivate SysUserService sysUserService;@RequestMapping("list")public IPage list(SysUserDTO userDTO) {IPage page = new Page(userDTO.getPage(), userDTO.getLimit());return sysUserService.page(page);}}

7 . 测试

成功实现sql拦截并进行拼接