mybatis-plus动态表名的实现示例
程序员文章站
2022-03-18 17:36:37
背景在分表的背景下,有时候查询数据的时候需要跨表查询,那此时就需要mp在解析的时候,能够很好的自适应表格名称实现mp中是通过paginationinterceptor(分页插件)完成动态表名解析的,配...
背景
在分表的背景下,有时候查询数据的时候需要跨表查询,那此时就需要mp在解析的时候,能够很好的自适应表格名称
实现
mp中是通过paginationinterceptor(分页插件)完成动态表名解析的,配置如下:
数据库中表
依赖
<dependency> <groupid>com.baomidou</groupid> <artifactid>mybatis-plus-boot-starter</artifactid> <version>3.3.1.tmp</version> </dependency>
配置类
package com.huanchuang.common.config; import com.baomidou.mybatisplus.extension.parsers.dynamictablenameparser; import com.baomidou.mybatisplus.extension.parsers.itablenamehandler; import com.baomidou.mybatisplus.extension.plugins.paginationinterceptor; import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.jsqlparsercountoptimize; import org.mybatis.spring.annotation.mapperscan; import org.springframework.boot.autoconfigure.condition.conditionalonproperty; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import java.util.collections; import java.util.hashmap; /** * @package: com.huanchuang.common.config * @description: <mybatis-plush配置类> * @author: milla * @createdate: 2020/09/04 14:42 * @updateuser: milla * @updatedate: 2020/09/04 14:42 * @updateremark: <> * @version: 1.0 */ @configuration @mapperscan("com.huanchuang.ext.mapper**") @conditionalonproperty(prefix = "spring.config", name = "enablemybatisplusdynamictable", havingvalue = "true") public class mybatisplusdynamictableconfig { private static final string dynamic_table_pre = "common_user"; /** * mybatis-plus分页插件 */ @bean public paginationinterceptor paginationinterceptor() { paginationinterceptor paginationinterceptor = new paginationinterceptor(); // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false paginationinterceptor.setoverflow(false); // 设置最大单页限制数量,默认 500 条,-1 不受限制 paginationinterceptor.setlimit(500); // // 开启 count 的 join 优化,只针对部分 left join paginationinterceptor.setcountsqlparser(new jsqlparsercountoptimize(true)); dynamictablenameparser dynamictablenameparser = new dynamictablenameparser(); dynamictablenameparser.settablenamehandlermap(new hashmap<string, itablenamehandler>(2) {{ //动态表规则-生成自己需要的动态表名 put(dynamic_table_pre, (metaobject, sql, tablename) -> dynamictabletreadlocal.instance.gettablename()); }}); paginationinterceptor.setsqlparserlist(collections.singletonlist(dynamictablenameparser)); return paginationinterceptor; } }
动态表名存储类
package com.huanchuang.common.config; /** * @package: com.huanchuang.common.config * @description: <动态表格存储类> * @author: milla * @createdate: 2020/09/04 14:42 * @updateuser: milla * @updatedate: 2020/09/04 14:42 * @updateremark: <> * @version: 1.0 */ public enum dynamictabletreadlocal { instance; private threadlocal<string> tablename = new threadlocal<>(); public string gettablename() { return tablename.get(); } public void settablename(string tablename) { this.tablename.set(tablename); } public void remove() { tablename.remove(); } }
使用
private void select(int year) { dynamictabletreadlocal.instance.settablename("user_" + year); lambdaquerywrapper<sparkdownsample> wrapper = wrappers.lambdaquery(user.class); list<user> userlist = usermapper.list(wrapper ) }
@data public class user { private long id; private string username; private string address; private char sex; private byte age; }
原理
以mybatis的query方法作为入口通过动态代理执行到配置的分页插件通过分页插件进行sql解析根据分页插件中配置的tablenamehandler进行目标表格的替换最后形成一个可执行sql,执行查询
到此这篇关于mybatis-plus动态表名的实现示例的文章就介绍到这了,更多相关mybatis-plus 动态表名内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!