温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mybatis怎么利用resultMap复杂类型list映射

发布时间:2021-07-30 16:00:49 来源:亿速云 阅读:1694 作者:chen 栏目:开发技术

这篇文章主要介绍“mybatis怎么利用resultMap复杂类型list映射”,在日常操作中,相信很多人在mybatis怎么利用resultMap复杂类型list映射问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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>

到此,关于“mybatis怎么利用resultMap复杂类型list映射”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI