mybatis 如何利用resultMap复杂类型list映射

mybatis resultMap复杂类型list映射

映射泛型为对象

xml

<resultMap id="internetDataDTO" type="com.mdm.skr.mdm_common.dto.StrategyInternetDataDTO">
    <id property="id" column="id" jdbcType="INTEGER"/>
    <result property="internetData" column="internet_data" jdbcType="INTEGER"/>
    <collection property="userList" ofType="com.mdm.skr.mdm_common.entity.SysUser">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="number" column="number" jdbcType="VARCHAR"/>
        <result property="pushToken" column="push_token" jdbcType="VARCHAR"/>
        <result property="wsChannelId" column="ws_channel_id" jdbcType="VARCHAR"/>
    </collection>
</resultMap> 

<select id="selectInternetDataDTOByInternetDataIdList" resultMap="internetDataDTO">
      SELECT sidu.id, sidu.internet_data, su.id, su.number, su.push_token, su.ws_channel_id
      FROM strategy_internet_data_user sidu JOIN skr_user su on su.id = sidu.user_id
      WHERE sidu.id IN
      <foreach collection="internetDataIdList" open="(" close=")"
          separator="," item="internetDataId">
          #{internetDataId}
      </foreach>
</select>

DTO

@Data
public class StrategyInternetDataDTO {
    private Integer id ;
    private Integer internetData ;
    private List<SysUser> userList ;

}

ENTITY

@Data
public class SysUser {
    private Integer id;
    private String number;
    private String pushToken;
    private String wsChannelId ;
}

MAPPER

List<StrategyInternetDataDTO> selectInternetDataDTOByInternetDataIdList(@Param("internetDataIdList") List<Integer> internetDataIdList);

映射泛型为包装类型

xml

<resultMap id="internetDataDTO" type="com.mdm.skr.mdm_common.dto.StrategyInternetDataDTO">
    <id property="id" column="id" jdbcType="INTEGER"/>
    <result property="internetData" column="internet_data" jdbcType="INTEGER"/>
    <collection property="userIdList" ofType="java.lang.Integer" javaType="java.util.List">
        <result column="userId"/>
    </collection>
</resultMap>  

<select id="selectInternetDataDTOByInternetDataIdList" resultMap="internetDataDTO">
      SELECT sidu.id, sidu.internet_data, sidu.user_id userId
      FROM strategy_internet_data_user sidu
      WHERE sidu.id IN
      <foreach collection="internetDataIdList" open="(" close=")"
          separator="," item="internetDataId">
          #{internetDataId}
      </foreach>
</select>

DTO

@Data
public class StrategyInternetDataDTO {
    private Integer id ;
    private Integer internetData ;
    private List<Integer> userIdList ;
}

MAPPER

List<StrategyInternetDataDTO> selectInternetDataDTOByInternetDataIdList(@Param("internetDataIdList") List<Integer> internetDataIdList);

mybatis的几种传值方式

1.单个参数传参

User selectUserInfo(Integer userId);
<select id = "selectUserInfo" parameterType = "java.lang.Inte" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from user
    where userId = #{userId , jdbcType=INTEGER}
</select>

2. 按照顺序传参

User selectUserInfo(Integer userId, String userName, String userPass);
<select id = "selectUserInfo" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from user
    where userId = #{arg0} and userName = #{arg1} and userPass = #{arg2}
</select>

3. 使用@Param注解传参

User selectUserInfo(@Param("userName")String userName, @Param("userPass")String userPass);
<select id = "selectUserInfo" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from user
    where userName = #{userName} and userPass = #{userPass}
</select>

4. 使用Map传参 注意传参方式:parameterType="java.util.Map"

Map<String,Object> map = new HashMap();
map.put("userName","张三");
map.put("userPass","123");
User user = userMapper.selectUserInfo(map);
User selectUserInfo(Map<String,Object> map);
<select id="selectUserInfo" parameterType="java.util.Map" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from user
    where userName = #{userName} and userPass = #{userPass}
</select>

5. 实体对象传参

User user = new User();
user.setUserName("张三");
user.setUserPass("123");
User user = UserMapper.selectUserInfo(user);
User selectUserInfo(User record);
<select id="selectUserInfo" parameterType="com.LiuXu.bean.User" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from user
    where userName = #{userName} and userPass = #{userPass}
</select>

6. List传参

List<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
List<User> userList = userMapper.selectUserInfo(list);

List<User> selectUserInfo(List<User> record);
<select id="selectUserInfo" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from user
    where userId in
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Mybatis中resultMap的使用总结

    Mybatis的介绍以及使用:http://www.mybatis.org/mybatis-3/zh/index.html resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性--> <resultMap id="唯一的标识" type="映射的pojo对象&

  • 深入理解Mybatis中的resultType和resultMap

     一.概述 MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在. 在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值. ①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给

  • Mybatis中强大的resultMap功能介绍

    前言 在Mybatis中,有一个强大的功能元素resultMap.当我们希望将JDBC ResultSets中的数据,转化为合理的Java对象时,你就能感受到它的非凡之处.正如其官方所述的那样: resultMap元素是 MyBatis 中最重要最强大的元素.它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作.实际上,在为一些比如连接的复杂语句编写映射代码的时候,一份 resultMap 能够代替实现同等功能的长

  • mybatis简单resultMap使用详解

    Mybatis的介绍以及使用:http://www.mybatis.org/mybatis-3/zh/index.html mybatis是一个半自动的ORM(Object Relational Mapping)框架,需要手动配置一些SQL语句或者注解,相对来说Mybatis留给程序员操作的空间灵活度更高,通常需要手动配置一些东西完成OR映射.当数据库表中的字段 和 POJO实体类不匹配时,这是就需要程序员手动完成字段的映射. mybatis-config.xml配置文件 <?xml versi

  • mybatis 如何利用resultMap复杂类型list映射

    mybatis resultMap复杂类型list映射 映射泛型为对象 xml <resultMap id="internetDataDTO" type="com.mdm.skr.mdm_common.dto.StrategyInternetDataDTO"> <id property="id" column="id" jdbcType="INTEGER"/> <result

  • 详解MyBatis resultType与resultMap中的几种返回类型

    目录 一.返回集合 1.返回JavaBean集合 2.返回 Map 集合 二.返回 Map 1.一条记录 2.多条记录,需要指定 Map 的 Key 和 Value 的类型 三.返回 resultMap 自定义结果集封装 1.自定义 JavaBean 的封装 2.关联查询的封装,一对一,JavaBean 属性包含 JavaBean 3.关联查询的封装,一对多,JavaBean 属性包含 JavaBean 的集合 4.鉴别器discriminator 一.返回集合 1.返回JavaBean集合 p

  • MyBatis中的resultMap简要概述

    Mybatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录. Mybatis的功能架构分为三层(图片借用了百度百科): 1)       API接口层:提供给外部使用的接口API,开发人员通过这些本地API

  • Mybatis 复杂对象resultMap的使用

    目录 Mybatis 复杂对象resultMap 下面是resultMap的定义 普通属性省略说明 select相关配置 Model代码 resultMap处理复杂映射问题 Ⅰ 多对一查询:学生--老师 (1) 创建实体类POJO (2) 创建学生实体类对应的接口 (3) 编写学生接口对应的Mapper.xml (4)在核心配置类中引入Mapper Ⅱ 一对多查询:老师--学生 (1)实体类 (2) 接口 (3)接口对应的Mapper.xml (4)测试: Mybatis 复杂对象resultM

  • mybatis深入讲解resultMap的定义及用法

    目录 我们知道 ,mybatis框架存在pojo对象映射 , 直接将查询到的结果封装到对象中给我们返回, 但如果数据库的中的列和java中类属性名就是不一致,或者如果我们实际返回的对象需要去关联其他的对象(也就是说,其他类的对象作为我们这个类的成员变量),那么这时候使用resultType肯定是不行的 这里我们则需要去定义 resultMap来完成我们的需求 定义resultMap的过程就是描述如何从数据库结果集中去加载对象 resultMap多用于多表查询间的映射关系, 例如 : 我们以部门和

  • MyBatis中传入参数parameterType类型详解

    前言 Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType属性,用于对应的mapper接口方法接受的参数类型.本文主要给大家介绍了关于MyBatis传入参数parameterType类型的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 1. MyBatis的传入参数parameterType类型分两种 1. 1. 基本数据类型:int,string,long,Date; 1. 2. 复杂数据类

  • mybatis多层嵌套resultMap及返回自定义参数详解

    1.两层嵌套,一个list中加另外一个list data:[ {a:123,b:456,c:[{d:7,e:8}]} ] xml文件定义的sql select * from zhy z LEFT JOIN wl w on z.id = w.zid resultMap可以定义: <resultMap id="zhyResultMap" type="zhy的doman实体" extends="zhy自动生成的BaseResultMap">

  • 基于MyBatis的parameterType传入参数类型

    目录 MyBatis的parameterType传入参数类型 1. MyBatis的传入参数parameterType类型分两种 2. 如何获取参数中的值 3.案例 mybatis 之parameterType="Long" MyBatis的parameterType传入参数类型 在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的输入参数

  • mybatis 如何返回list<String>类型数据

    mybatis返回list<String>类型数据 studends表里一条teacher_id 数据对应多条 student_id数据,所以通过teacher_id 查询出来的student_id 是一个List. mybatis代码如下: //返回类型是String类型的student_id <resultMap id="studentIdResult" type="java.lang.String" > <result colum

  • Java Mybatis框架Dao层的实现与映射文件以及核心配置文件详解分析

    目录 Mybatis的Dao层实现 传统开发方式 代理开发方式 MyBatis映射文件深入 动态sql语句 动态SQL之<if> 动态SQL之<foreach> SQL片段抽取 总结 Mybatis核心配置文件深入 typeHandlers标签 plugins标签 总结 Mybatis的Dao层实现 传统开发方式 1.编写UserDao接口 public interface UserMapper { public List<User> findAll() throws

随机推荐