服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - mybatis 如何利用resultMap复杂类型list映射

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

2021-10-29 10:45954L Java教程

这篇文章主要介绍了mybatis 如何利用resultMap复杂类型list映射的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

mybatis resultMap复杂类型list映射

映射泛型为对象

xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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

?
1
2
3
4
5
6
7
@Data
public class StrategyInternetDataDTO {
    private Integer id ;
    private Integer internetData ;
    private List<SysUser> userList ;
 
}

ENTITY

?
1
2
3
4
5
6
7
@Data
public class SysUser {
    private Integer id;
    private String number;
    private String pushToken;
    private String wsChannelId ;
}

MAPPER

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

映射泛型为包装类型

xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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

?
1
2
3
4
5
6
@Data
public class StrategyInternetDataDTO {
    private Integer id ;
    private Integer internetData ;
    private List<Integer> userIdList ;
}

MAPPER

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

mybatis的几种传值方式

1.单个参数传参

?
1
2
3
4
5
6
7
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. 按照顺序传参

?
1
2
3
4
5
6
7
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注解传参

?
1
2
3
4
5
6
7
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"

?
1
2
3
4
5
6
7
8
9
10
11
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. 实体对象传参

?
1
2
3
4
5
6
7
8
9
10
11
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传参

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wkh___/article/details/91550524

延伸 · 阅读

精彩推荐