MybatisPlusInterceptor實(shí)現(xiàn)sql攔截器超詳細(xì)教程
1 . 導(dǎo)入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 . 實(shí)體類
@Data @TableName("sys_user") @ApiModel(value = "SysUser對(duì)象", 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對(duì)象", 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("進(jìn)入sql攔截器"); //分頁(yè)攔截器插件 //如果用了分頁(yè)插件注意先 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"; } //去掉實(shí)體類字段 @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; } // 用了分頁(yè)插件必須設(shè)置 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 . 測(cè)試
成功實(shí)現(xiàn)sql攔截并進(jìn)行拼接
總結(jié)
到此這篇關(guān)于MybatisPlusInterceptor實(shí)現(xiàn)sql攔截器的文章就介紹到這了,更多相關(guān)MybatisPlusInterceptor實(shí)現(xiàn)sql攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MybatisPlus攔截器如何實(shí)現(xiàn)數(shù)據(jù)表分表
- mybatis-plus配置攔截器實(shí)現(xiàn)sql完整打印的代碼設(shè)計(jì)
- MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的方法
- Mybatis-plus通過(guò)添加攔截器實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)權(quán)限
- MyBatis-Plus攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限控制的示例
- mybatis-plus 攔截器敏感字段加解密的實(shí)現(xiàn)
- MyBatis-Plus攔截器對(duì)敏感數(shù)據(jù)實(shí)現(xiàn)加密
- mybatis-plus攔截器、字段填充器、類型處理器、表名替換、SqlInjector(聯(lián)合主鍵處理)
- mybatisplus 的SQL攔截器實(shí)現(xiàn)關(guān)聯(lián)查詢功能
- Mybatis Plus 3.4.0分頁(yè)攔截器的用法小結(jié)
相關(guān)文章
java實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06親身體驗(yàn)Intellij?Idea從卡頓到順暢全過(guò)程
這篇文章主要介紹了親身體驗(yàn)Intellij?Idea從卡頓到順暢全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫(kù)字段加密脫敏
這篇文章主要為大家介紹了mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫(kù)字段加密脫敏,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09idea熱部署插件jrebel正式版及破解版安裝詳細(xì)圖文教程
這篇文章主要介紹了idea熱部署插件jrebel正式版及破解版安裝詳細(xì)教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Java8新日期時(shí)間API的20個(gè)使用示例
這篇文章主要介紹了Java8新日期時(shí)間API的20個(gè)使用示例,為了學(xué)習(xí)Java 8的這個(gè)新庫(kù),這里我創(chuàng)建了20個(gè)以任務(wù)為導(dǎo)向的例子,需要的朋友可以參考下2015-03-03Java的super關(guān)鍵字與instanceof運(yùn)算符使用方法
這篇文章主要介紹了Java的super關(guān)鍵字與instanceof運(yùn)算符使用方法,是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09Java實(shí)現(xiàn)橋接方法isBridge()和合成方法isSynthetic()
本文主要介紹了Java實(shí)現(xiàn)橋接方法isBridge()和合成方法isSynthetic(),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06