在 MyBatis 的 XML 配置文件中,如果需要返回 List 类型的数据,需要使用 resultMap 标签来定义返回类型。具体步骤如下:

  1. 在 mapper 中定义 resultMap 标签,设置 id 和 type 属性。
<mapper namespace="com.example.mapper.UserMapper">
    <resultMap id="userMap" type="com.example.entity.User">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="age" property="age" />
        <result column="email" property="email" />
    </resultMap>
</mapper>
  1. 在 select 标签中设置 resultMap 属性为上一步定义的 resultMap 的 id 值。
<select id="selectUsers" resultMap="userMap">
    SELECT * FROM users
</select>
  1. 在 Java 代码中调用 selectUsers 方法,即可返回 List 类型的数据。
List<User> userList = sqlSession.selectList("com.example.mapper.UserMapper.selectUsers");
``
mybatis的xml中sql返回list类型的数据

原文地址: https://www.cveoy.top/t/topic/haY2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录