不要放纵你的爱情,不要让欲望的利剑把你射中——莎士比亚
前两天写了mybatis一对多查询
今天写一下在代码中如何使用@One
实现一对一的关联查询
这里的@One
注解类名org.apache.ibatis.annotations.One
代码如下:
代码语言:javascript复制@Select("<script>"
"select *"
" from user_rolen"
" <where>"
" <if test="userId != null">"
" AND user_id = #{userId}"
" </if>"
" <if test="roleId != null">"
" AND role_id = #{roleId}"
" </if>"
" </where>"
"</script>")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "userId", column = "user_id"),
@Result(property = "roleId", column = "role_id"),
@Result(property = "role", javaType = RoleInfo.class, column = "role_id",
one = @One(select = "com.ruben.mapper.RoleMapper.getById"))
})
List<UserRole> selectList(UserRole userRole);
然后执行单元测试:
代码语言:javascript复制@Test
void testOne(@Autowired UserRoleMapper userRoleMapper) {
List<UserRole> userRoles = userRoleMapper.selectList(new UserRole() {{
setUserId(1L);
}});
Assertions.assertEquals("admin", userRoles.get(0).getRole().getRoleName());
Assertions.assertEquals("user", userRoles.get(1).getRole().getRoleName());
}
结果:
完整代码:
https://gitee.com/VampireAchao/simple-mybatis.git