mybatis 如何利用resultMap復(fù)雜類型list映射
更新時間:2021年07月30日 12:44:43 作者:954L
這篇文章主要介紹了mybatis 如何利用resultMap復(fù)雜類型list映射的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
mybatis resultMap復(fù)雜類型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.單個參數(shù)傳參
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. 實(shí)體對象傳參
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>
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用Guava實(shí)現(xiàn)日志脫敏的示例代碼
本文主要介紹了SpringBoot使用Guava實(shí)現(xiàn)日志脫敏的示例代碼,使用Guava中的Strings、Maps和CharMatcher類來進(jìn)行日志脫敏,保護(hù)敏感數(shù)據(jù)的安全,感興趣的可以了解一下2024-01-01
解決SpringBoot內(nèi)嵌Tomcat并發(fā)容量的問題
這篇文章主要介紹了解決SpringBoot內(nèi)嵌Tomcat并發(fā)容量的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Springboot中基于X509完成SSL檢驗(yàn)的原理與實(shí)現(xiàn)
本文詳細(xì)解析了HTTPS通信中SSL證書的作用和原理,SSL證書建立在客戶端和服務(wù)器之間的安全通道,確保數(shù)據(jù)傳輸?shù)耐暾院捅C苄?詳細(xì)的介紹了Springboot中基于X509完成SSL檢驗(yàn)的原理與實(shí)現(xiàn),感興趣的可以了解一下2024-09-09
Java女裝商城系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個女裝商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
Java內(nèi)存釋放實(shí)現(xiàn)代碼案例
這篇文章主要介紹了Java內(nèi)存釋放實(shí)現(xiàn)代碼案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12

