a_wing啊 2021-03-21 15:57 采纳率: 50%
浏览 91
已结题

MyBatis注解方式和XML方式能不能同时开启二级缓存呢?

ShopMapper.java 

@Mapper

@CacheNamespace(blocking = true)

public interface ShopMapper {

    //省略其他注解方式的查询方法...

    //查询单个

    @Select("select * from t_shop where id=#{id}")

    Shop selectById(Integer id);

    //删除

    @Delete("delete  from t_shop where id=#{id}")

    int deleteShop(int id);

    //修改,xml方式

    int updateShop(Shop shop);

}

ShopMapper.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.rgb.demosystem.mapper.ShopMapper">


    <update id="updateShop" parameterType="Shop" >

        update t_shop

        <set>

            <if test="dorm != null and dorm != ''">

                dorm=#{dorm},

            </if>

            <if test="finishCount != null and finishCount != ''">

                finishCount=#{finishCount},

            </if>

            where id=#{id}

        </set>
    </update>
</mapper>

像这种注解和XML方式两种同时使用的情况下,我使用查询数据的方法时二级缓存是有效果的,但是使用更新数据(就是这里用XML方式的)后,没有刷新缓存,我再执行之前的查询数据方法,得到的还是旧的数据。请问有没有解决方法?

  • 写回答

1条回答 默认 最新

  • 农夫丶果园 2021-03-21 18:54
    关注

    应该是可以的, 你在接口添加注解和在xml中使用<cache /> 效果是一样的 ; mybatis的二级缓存是根据 namespace来的 , 这两种方式同属于一个namespace , 你可以验证一下

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月23日

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突