接上一篇的 MultiCache ,这篇主要讲一下 AssignCache 的使用。继续解决上一个场景中没有解决的问题。
根据某几个 role ID 查询所有的 User。查询关联两个以上的 Model,在更新其中一个时,需要让相关的缓存失效
Method:
@Override
@ReadThroughAssignCache(assignedKey = "user/getUsersByRoleIds", namespace = "user", expiration = 60)
public List<User> getUsersByRoleIds(@ParameterValueKeyProvider final List<Long> ids) {
return (List<User>) sqlMapClientTemplate.queryForList("getUsersByRoleIds", ids);
}
SQL:
<select id="getUsersByRoleIds" parameterClass="list" resultClass="user">
SELECT u.id, u.name, r.id as "role.id", r.name as "role.name"
FROM user u, user_role ur, role r
WHERE u.id = ur.userid and r.id = ur.roleid and ur.roleid in
(<iterate conjunction=",">
#ids[]#
</iterate>) order by r.id
</select>