mybatis plug 只查id_Mybatis中使用Plugin解决Join查询语句缓存刷新的研究

....

select A.name, B.detail from A left join B.id = A.id

...

为了解决这个问题,我使用了mybatis的plugin功能,首先拦截Executor class的query方法来得到以下三个参数MappedStatement.class, Object.class, RowBounds.class, 这个三个参数是为了计算存放到cahce中的key,然后再由Executor.createCacheKey(mappedStatement, parameter, rowBounds)方法计算出cacheKey, 这样就可以得到每个select语句被缓存到cahce中时所对应的key, 顺带说一下这个cacheKey的计算是由几个要素来计算的,1.select标签的id, 可通过MappedStatement.getId得到 2. RowBounds 的getOffset()和getLimit() 3. select的sql语句  4. 最重要的一点,也是决定这key是否相同的一点, sql的参数,由上面三个参数中的第二个提供, 当然cahceKey的不同也可能会由RowBounds的不同而不同。

得到cahceKey之后把它保存到一个Map>类型的map里,String对应一个sqlid, 比如上面提到的sql语句 getInfo, 不过还要加上namesapace那就是 A.getInfo,  Set 保存某个SQL所对应的不同查询参数的不同结果。当我们得到想要flush的select 的cachekey之后,就可以拦腰Executor class的update方法(包括insert,update,delete), 至于过程很简单,上源码。

在sqlMapConfig.xml中加上:

实现Interceptor接口:

@Intercepts( {

@Signature(type = Executor.class, method = "update", args = {

MappedStatement.class, Object.class }),

@Signature(type = Executor.class, method = "query", args = {

MappedStatement.class, Object.class, RowBounds.class,

ResultHandler.class }) })

public class FlushCacheInterceptor implements Interceptor {

private String property;

private Properties properties;

private Map> keyMap = new HashMap>();

public Object intercept(Invocation invocation) throws Throwable {

MappedStatement mappedStatement = (MappedStatement) invocation

.getArgs()[0];

if (!mappedStatement.getConfiguration().isCacheEnabled())

return invocation.proceed();

String sqlId = mappedStatement.getId();

String nameSpace = sqlId.substring(0, sqlId.indexOf('.'));

Executor exe = (Executor) invocation.getTarget();

String methodName = invocation.getMethod().getName();

if (methodName.equals("query")) {

for (Object key : properties.keySet()) {

if (key.equals(sqlId)) {

Object parameter = invocation.getArgs()[1];

RowBounds rowBounds = (RowBounds) invocation.getArgs()[2];

Cache cache = mappedStatement.getConfiguration().getCache(nameSpace);

cache.getReadWriteLock().readLock().lock();

CacheKey cacheKey = exe.createCacheKey(mappedStatement, parameter, rowBounds);

try {

if (cache.getObject(cacheKey) == null) {

if (keyMap.get(sqlId) == null) {

Set cacheSet = new HashSet();

cacheSet.add(cacheKey);

keyMap.put(sqlId, cacheSet);

} else {

keyMap.get(sqlId).add(cacheKey);

}

}

} finally {

cache.getReadWriteLock().readLock().unlock();

}

break;

}

}

} else if (methodName.equals("update")) {

for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) {

String cacheSqlId = (String) e.nextElement();

String updateNameSpace = properties.getProperty(cacheSqlId);

if (updateNameSpace.equals(nameSpace)) {

String cacheNamespace = cacheSqlId.substring(0, cacheSqlId.indexOf('.'));

Cache cache = mappedStatement.getConfiguration().getCache(cacheNamespace);

Set cacheSet = keyMap.get(cacheSqlId);

cache.getReadWriteLock().writeLock().lock();

try {

for (Iterator it = cacheSet.iterator(); it.hasNext();) {

cache.removeObject(it.next());

}

} finally {

cache.getReadWriteLock().writeLock().unlock();

keyMap.remove(cacheSqlId);

}

}

}

}

return invocation.proceed();

}

public Object plugin(Object target) {

return Plugin.wrap(target, this);

}

public void setProperties(Properties properties) {

this.properties = properties;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于Mybatis在insert嵌套子查询的问题,可以使用Mybatis的动态SQL语句来实现,具体方法如下: 1. 在mapper文件定义一个包含子查询的SQL语句,比如: ``` <select id="getUserIdByName" parameterType="java.lang.String" resultType="java.lang.Integer"> SELECT user_id FROM user WHERE user_name = #{name} </select> ``` 2. 在insert语句使用动态SQL语句来调用子查询,比如: ``` <insert id="insertOrder" parameterType="Order"> INSERT INTO order (order_no, user_id, order_time) VALUES (#{orderNo}, #{userId, jdbcType=INTEGER}, #{orderTime}) <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> </insert> ``` 其,#{userId, jdbcType=INTEGER}是插入语句的一个参数,它的值通过调用getUserIdByName子查询来获取。 关于Mybatis查询结果resultMap的使用概述,可以参考以下几点: 1. resultMap是Mybatis用于映射查询结果集的标签,它可以将查询结果集的列名映射为Java对象的属性名。 2. resultMap标签可以定义在mapper文件,也可以定义在公共的resultMap文件,以便在多个mapper文件复用。 3. resultMap标签支持多种映射方式,如一对一、一对多、多对一、多对多等,可以根据查询结果集的实际情况选择不同的映射方式。 4. resultMap标签还支持映射嵌套对象、映射对象属性、映射枚举值等高级功能,可以根据具体情况选择使用。 总之,Mybatis的resultMap标签是非常强大且灵活的,可以帮助我们快速、方便地实现查询结果集的映射。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值