MybatisLink(MyBatisPlusX)单表查询,连表查询,动态设置表名(3)

MybatisLink(MyBatisPlusX)单表查询连表查询,动态设
表名(3)
四. MyBatis Link 单表查询,连表查询,动态表名展⽰
随着业务量的增长,部分表的数据量过于庞⼤,可以设置保留少量数据的查询表,或者是根据时间段进⾏分表,由于表的结构是⼀致的,所以只需要更改表名即可。
例如 t_sys_log 系统⽇志表,随着访问量的增⼤,⽇志表的数据很快可以达到千万级,那么可以对表进⾏切分,⼀年⼀张表,且设置⼀张 t_sys_log_ro(ro == read-only,⾮固定)表,⾥⾯设置三个⽉的数据,便于快速查询。
表明动态设置只针对wrapper⼊参查询
1.单表替换表名
使⽤⽅式
推荐使⽤改⽅法:fw.setTableName("t_test_a_ro");
中药煎药器
或者也可以使⽤这种⽅式:fw.addTableName("t_test_a", "t_test_a_ro");
@Log
cwmp@ApiOperation(value = "分页查询")
@GetMapping("page")
// @RequiresPermissions("test:a:page")
public Object page(String query) {
FindWrapper<TestAVo> fw = getWrapper(query, TestAVo.class);
fw.setTableName("t_test_a_ro");
IPage<TestADto> page = testAMgr.page(fw);
return buildPage(page, LIST);
}
技术原理
/**
* <p>
* 根据 entity 条件,查询全部记录
* </p>
*
* @param wrapper 实体对象封装操作类(可以为 null)
*/
List<D> list(@Param(Constants.WRAPPER) Wrapper<V> wrapper);
上⾯这段会⽣成如下查询sql
<script>
SELECT <choose>
<when test="ew != null and ew.sqlSelect != null">
${ew.sqlSelect}
</when>
<otherwise>
t_test_a.`id` t_test_a__id, t_test_a.`c_id` t_test_a__c_id,
t_test_a.`b_id` t_test_a__b_id, t_test_a.`a_id` t_test_a__a_id,
t_test_a.`crt_tm` t_test_a__crt_tm, t_test_a.`crt_by` t_test_a__crt_by,
t_test_a.`upd_tm` t_test_a__upd_tm, t_test_a.`upd_by` t_test_a__upd_by,
t_test_a.`edit_flag` t_test_a__edit_flag
</otherwise>
</choose> FROM <choose>
<when test="ew != null and ew.tableName != null">人工智能建站
${ew.tableName} t_test_a
</when>
<otherwise>t_test_a</otherwise>
</choose>
<trim prefix="WHERE" prefixOverrides="AND|OR">
<choose>
<when test="ew != null">
<if test="ew.entity != null">
<if test="ew.entity.id != null">id=#{ew.entity.id}</if>
<if test="ew.entity.cId != null"> AND c_id=#{ew.entity.cId}</if>
<if test="ew.entity.bId != null"> AND b_id=#{ew.entity.bId}</if>
<if test="ew.entity.aId != null"> AND a_id=#{ew.entity.aId}</if>
<if test="Tm != null"> AND crt_tm=#{Tm}</if>
<if test="By != null"> AND crt_by=#{By}</if>
<if test="ew.entity.updTm != null"> AND upd_tm=#{ew.entity.updTm}</if>
<if test="ew.entity.updBy != null"> AND upd_by=#{ew.entity.updBy}</if>
</if>
AND edit_flag=0
<if test="ew.sqlSegment != null and ew.sqlSegment != '' EmptyOfWhere"> AND ${ew.sqlSegment}
</if>
<if test="ew.sqlSegment != null and ew.sqlSegment != '' ptyOfWhere">
${ew.sqlSegment}
</if>
</when>
<otherwise> AND edit_flag=0</otherwise>
</choose>
</trim>
</script>
2.多表连查替换表名
使⽤⽅式
fw.addTableName("t_test_a", "t_test_a_ro");
排卵检测仪
@Log
@ApiOperation(value = "listTestAATestB")
@GetMapping("listTestAATestB")
public Object listTestAATestB(String query) {
FindWrapper<TestAVo> fw = getWrapper(query, TestAVo.class);
fw.addTableName("t_test_a", "t_test_a_ro");
fw.addTableName("t_test_b", "t_test_b_ro");
List<TestADto> list = testAMgr.listTestAATestB(fw);
return buildList(list);
}
技术原理
/
**
* t_test_a 与  t_test_b ⼀对⼀连表查询
*/
@Link(  print = true, printRm = false,
ones = { @OneToOne(rightClass = TestBVo.class, rightColumn = "a_id") })
List<TestADto> listTestAATestB(@Param(Constants.WRAPPER) Wrapper<TestAVo> wrapper);
如果设置了别名,fw.addTableName("t_test_a", "t_test_a_ro"); 就要改为 fw.addTableName(别名, "t_test_a_ro");    上⾯这段会⽣成如下查询sql
<script>
SELECT <choose>
<when test="ew != null and ew.sqlSelect != null">
${ew.sqlSelect}
</when>
<otherwise> t_test_a.`id` t_test_a__id , t_test_a.`c_id` t_test_a__c_id ,
t_test_a.`b_id` t_test_a__b_id , t_test_a.`a_id` t_test_a__a_id ,
t_test_a.`crt_tm` t_test_a__crt_tm , t_test_a.`crt_by` t_test_a__crt_by ,
t_test_a.`upd_tm` t_test_a__upd_tm , t_test_a.`upd_by` t_test_a__upd_by ,
t_test_a.`edit_flag` t_test_a__edit_flag , t_test_b.`id` t_test_b__id ,
t_test_b.`a_id` t_test_b__a_id , t_test_b.`c_id` t_test_b__c_id ,
t_test_b.`crt_tm` t_test_b__crt_tm , t_test_b.`crt_by` t_test_b__crt_by ,
t_test_b.`upd_tm` t_test_b__upd_tm , t_test_b.`upd_by` t_test_b__upd_by ,
t_test_b.`edit_flag` t_test_b__edit_flag
</otherwise>
</choose> FROM <choose>
<when test="ew != null and ew.tableNameMap != null and ew.tableNameMap.t_test_a != null">
${ew.tableNameMap.t_test_a} t_test_a
</when>
<otherwise>t_test_a t_test_a</otherwise>
</choose> INNER JOIN <choose>
<when test="ew != null and ew.tableNameMap != null and ew.tableNameMap.t_test_b != null">
${ew.tableNameMap.t_test_b} t_test_b
</when>
<otherwise>t_test_b t_test_b</otherwise>
</choose> <trim prefix="WHERE" prefixOverrides="AND|OR">
AND t_test_a.edit_flag=0 AND t_test_b.edit_flag=0 and t_test_a.b_id=t_test_b.id茂发跳跳糖
<if test="ew != null">
<if test="ew.id != null">
AND t_test_a.id = ${ew.id}
</if>
<if test="ew.sqlSegment != null and ew.sqlSegment != '' EmptyOfWhere">
AND ${ew.sqlSegment}
电动刻字笔
</if>
<if test="ew.sqlSegment != null and ew.sqlSegment != '' ptyOfWhere">
${ew.sqlSegment}
</if>
</if>
</trim>
</script>

本文发布于:2024-09-21 01:42:33,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/190389.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:查询   数据   表名   部分   连表   单表   动态
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议