首页 > 解决方案 > 如何从 Mybatis 获取 db 列值进行分片?

问题描述

我使用 Mybatis 访问 db,有些表是通过 id 和 hash 算法分片的。

我想写一个Mybatis intecepter来自动更改表名,它需要获取分片列的值。

表实体:

@Data
@TableName("m_user")
public class User {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
    private Integer age;
}

用户映射器 sql:

@Select("select * from m_user where id = #{id2} and name = #{name2};")
List<User> selectByIdAndName(Integer id2, String name2);

我用boundSql.getParameterObject()andboundSql.getParameterMappings()来检查,但不能确定分片列id是否在sql中,然后获取分片列的值。

ParameterMappings 值和 ParameterObject 值在这里:

parameter mapping:ParameterMapping{property='id2', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}
parameter mapping:ParameterMapping{property='name2', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}
params:{id2=1, param1=1, name2=name1, param2=name1}

参数是Mapper函数参数,但我需要分片列id和值,程序只能获取id2param1

如何从 Mybatis 获取 db 列和值?

标签: mybatis

解决方案


推荐阅读