mybatis中@Many

简介: mybatis中@Many

吃饭先喝汤,不用请药方——佚名

之前写了mybatis中@One

今天写个@Many的

首先还是代码:

package com.ruben.mapper;
import com.ruben.pojo.po.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.session.RowBounds;
import java.util.List;
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user_info")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "age", property = "age"),
            @Result(column = "email", property = "email"),
            @Result(property = "userRoles", javaType = List.class, column = "userRole.userId = id",
                    many = @Many(select = "com.ruben.mapper.UserRoleMapper.selectListCursor"))
    })
    List<UserInfo> selectList(RowBounds rowBounds);
}


这里的UserRoleMapper.selectListCursor还是没动,感觉这里不应该是userRole.userId,直接使用userId、roleId会更好

<?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.ruben.mapper.UserRoleMapper">
    <resultMap id="userRole" type="com.ruben.pojo.po.UserRole">
        <result column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="role_id" property="roleId"/>
        <association property="role"
                     column="id = role_id"
                     javaType="com.ruben.pojo.po.RoleInfo"
                     select="com.ruben.mapper.RoleMapper.getById"
                     fetchType="lazy"/>
    </resultMap>
    <select id="selectListCursor" resultMap="userRole">
        select *
        from user_role
        <where>
            <if test="userRole.userId != null">
                AND user_id = #{userRole.userId}
            </if>
            <if test="userRole.roleId != null">
                AND role_id = #{userRole.roleId}
            </if>
        </where>
    </select>
</mapper>

然后结合之前的代码,这里写个单元测试:

@Test
void testMany(@Autowired UserMapper userMapper) {
    List<UserInfo> users = userMapper.selectList(new RowBounds(0, 5));
    Assertions.assertEquals("admin", users.get(0).getUserRoles().get(0).getRole().getRoleName());
    Assertions.assertEquals("user", users.get(0).getUserRoles().get(1).getRole().getRoleName());
}

运行结果:

相关文章
|
SQL Java 数据库连接
Mybatis中强大的resultMap
Mybatis中强大的resultMap
673 0
|
Java
Java 清空 List 的多种方法?
Java 清空 List 的多种方法?
3396 0
|
JSON Java 数据格式
一文彻底搞懂 @RequestBody 和 @RequestParam 的区别(附实战示例)
本文深入解析Spring Boot中@RequestBody和@RequestParam的区别,通过实战示例详解两者在数据来源、格式、使用场景及验证处理上的差异,帮助开发者正确选择参数绑定方式,提升开发效率与系统安全性。
2070 0
|
API Docker 容器
超实用工具分享!Gotenberg,让你的文档转换PDF无缝对接
超实用工具分享!Gotenberg,让你的文档转换PDF无缝对接
2167 4
|
安全 Java 数据库
后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)
后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)
|
存储 缓存 Java
更快、更灵活、场景更丰富,云效镜像构建能力升级啦
本文介绍了云效在镜像构建方面的改进。
1244 38
|
设计模式 Java 测试技术
Spring Boot中的依赖注入详解
Spring Boot中的依赖注入详解
|
Java API 开发者
代码小妙招:用Java轻松获取List交集数据
在Java中获取两个 `List`的交集可以通过 `retainAll`方法和Java 8引入的流操作来实现。使用 `retainAll`方法更为直接,但会修改原始 `List`的内容。而使用流则提供了不修改原始 `List`、更为灵活的处理方式。开发者可以根据具体的需求和场景,选择最适合的方法来实现。了解和掌握这些方法,能够帮助开发者在实际开发中更高效地处理集合相关的问题。
902 1
|
存储 前端开发 JavaScript