0


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拦截并进行拼接

标签: sql java 数据库

本文转载自: https://blog.csdn.net/a17331003724/article/details/130005913
版权归原作者 学不会揍你 所有, 如有侵权,请联系我们删除。

“MybatisPlusInterceptor实现sql拦截器(超详细)”的评论:

还没有评论