MybatisPlusInterceptor实现sql拦截器超详细教程
作者:学不会揍你
这篇文章主要给大家介绍了关于MybatisPlusInterceptor实现sql拦截器超详细教程的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下
1 . 导入pom
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
2 . 配置下MybatisPlus的yml
mybatis-plus: mapper-locations: - classpath:mapper/*/*Mapper.xml global-config: db-config: id-type: auto banner: true configuration: 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 @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); log.info("进入sql拦截器"); //分页拦截器插件 //如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { //租户id(拼接sql的条件的值) @Override public Expression getTenantId() { return new LongValue(1); } //追加的字段(拼接sql的条件) @Override public String getTenantIdColumn() { return "SU_CODE"; } //去掉实体类字段 @Override public 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()); @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setUseDeprecatedExecutor(false); } }
6 . controller
@RestController @RequestMapping("exam") public class SysUserController { @Autowired private SysUserService sysUserService; @RequestMapping("list") public IPage<SysUser> list(SysUserDTO userDTO) { IPage<SysUser> page = new Page<>(userDTO.getPage(), userDTO.getLimit()); return sysUserService.page(page); } }
7 . 测试
成功实现sql拦截并进行拼接
总结
到此这篇关于MybatisPlusInterceptor实现sql拦截器的文章就介绍到这了,更多相关MybatisPlusInterceptor实现sql拦截器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!