How mybatis uses resultmap to map complex type list

Time:2021-10-18

Mybatis resultmap complex type list mapping

Mapping generics to objects

xml


<resultMap 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 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);

Mapping generics to wrapper types

xml


<resultMap 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 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);

Several value transfer methods of mybatis

1. Single parameter transfer


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. Transfer parameters in sequence


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. Use @ param annotation to pass parameters


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. Use map to transfer parameters. Note the parameter transfer method: parametertype = “Java. Util. Map”

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

5. Entity object parameter transfer

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

6. Transfer parameters


List<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
List<User> userList = userMapper.selectUserInfo(list);
    
List<User> selectUserInfo(List<User> record);
<select 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>

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer.