Mybatis二级缓存关于多表关联时修改被关联表数据,缓存没清空的问题

表结构如下:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(16) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';

CREATE TABLE `user_info` (
  `userId` int(11) NOT NULL,
  `idNumber` varchar(18) DEFAULT NULL COMMENT '身份证号',
  `realName` varchar(40) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  `sex` char(1) DEFAULT NULL COMMENT '男:1  女:0',
  PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

UserMapper如下:

<mapper namespace="com.xxx.mapper.UserMapper">
	<span style="color:#ff0000;"><strong><cache/></strong></span>
	<resultMap id="User" type="com.xxx.entity.User">
		<id property="userId" column="id"/>
		<result property="username" column="username"/>

		<!-- 表关联 1:1 -->
		<association property="userInfo" column="id"
			select="com.xxx.mapper.UserInfoMapper.findUserInfoByUserId"/>
	</resultMap><pre name="code" class="html"><span style="white-space:pre">	</span><!-- 根据userId查找用户 -->
	<select id="findByUserId" resultMap="User">
		SELECT u.id,
			   u.username,
			   info.realName,
			   info.idNumber,
			   info.birthday,
			   info.sex
		FROM user u LEFT JOIN user_info info ON u.id = info.userid WHERE u.id = #{userId} AND isDeleted = '0'
	</select>

 
UserInfoMapper如下: 

<mapper namespace="com.xxx.mapper.UserInfoMapper">
	<span style="color:#ff0000;"><strong><cache/></strong></span>
	<resultMap id="UserInfo" type="com.xxx.entity.UserInfo">
		<id property="userId" column="userId"/>
		<result property="idNumber" column="idNumber"/>
		<result property="realName" column="realName"/>
		<result property="birthday" column="birthday"/>
		<result property="sex" column="sex"/>
	</resultMap>

我是把user和userinfo 1:1关联到一块,在进行数据查找时,如service.findUserByUserId(1);操作,Mybatis会将数据加到缓存中,后续的findUserByUserId不会对数据库查找。但是,如果我此时更新UserInfo呢?

结果很残酷,findUserByUserId查询的依然是缓存,如何才能将被关联的表的cache清空,查询数据库呢?

此时需要使用标签<cache/> <cache ref/>

<mapper namespace="com.xxx.mapper.UserMapper">
	<span style="color:#ff0000;"><strong><cache-ref namespace="com.xxx.mapper.UserInfoMapper"/></strong></span>

	<resultMap id="User" type="com.xxx.entity.User">
		<id property="userId" column="id"/>
		<result property="username" column="username"/>

		<!-- 表关联 1:1 -->
		<association property="userInfo" column="id"
			select="com.xxx.mapper.UserInfoMapper.findUserInfoByUserId"/>
	</resultMap><pre name="code" class="html"><span>	</span><!-- 根据userId查找用户 -->
	<select id="findByUserId" resultMap="User">
		SELECT u.id,
			   u.username,
			   info.realName,
			   info.idNumber,
			   info.birthday,
			   info.sex
		FROM user u LEFT JOIN user_info info ON u.id = info.userid WHERE u.id = #{userId} AND isDeleted = '0'
	</select>

 
UserInfoMapper如下: 

<mapper namespace="com.xxx.mapper.UserInfoMapper">
	<span style="color:#ff0000;"><strong><cache/></strong></span>
	<resultMap id="UserInfo" type="com.xxx.entity.UserInfo">
		<id property="userId" column="userId"/>
		<result property="idNumber" column="idNumber"/>
		<result property="realName" column="realName"/>
		<result property="birthday" column="birthday"/>
		<result property="sex" column="sex"/>
	</resultMap>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值