1 . 导入pom com.baomidou mybatis-plus-boot-starter 3.4.2 2 . 配置下Myba
1 . 导入pom
com.baomidou mybatis-plus-boot-starter 3.4.2
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@Configurationpublic 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 list(SysUserDTO userDTO) { IPage page = new Page<>(userDTO.getPage(), userDTO.getLimit()); return sysUserService.page(page); }}
7 . 测试
成功实现sql拦截并进行拼接
来源地址:https://blog.csdn.net/a17331003724/article/details/130005913
--结束END--
本文标题: MybatisPlusInterceptor实现sql拦截器(超详细)
本文链接: https://lsjlt.com/news/386653.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0