返回顶部
首页 > 资讯 > 后端开发 > JAVA >MybatisPlusInterceptor实现sql拦截器(超详细)
  • 752
分享到

MybatisPlusInterceptor实现sql拦截器(超详细)

sqljava数据库 2023-08-31 18:08:05 752人浏览 独家记忆
摘要

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

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作