欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-08-10 09:38:21|栏目:JAVA代码|点击:

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>

上一篇:java实现后台处理base64图片还原为文件

栏    目:JAVA代码

下一篇:Java关键字this与super详解用法

本文标题:mybatis 如何利用resultMap复杂类型list映射

本文地址:http://www.codeinn.net/misctech/210362.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有