基于MyBatis的parameterType傳入?yún)?shù)類型
MyBatis的parameterType傳入?yún)?shù)類型
在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType為輸入?yún)?shù),在配置的時候,配置相應(yīng)的輸入?yún)?shù)類型即可。parameterType有基本數(shù)據(jù)類型和復(fù)雜的數(shù)據(jù)類型配置。
1. MyBatis的傳入?yún)?shù)parameterType類型分兩種
1. 1. 基本數(shù)據(jù)類型:int、string、long、Date;
1. 2. 復(fù)雜數(shù)據(jù)類型:類(JavaBean、Integer等)和Map
2. 如何獲取參數(shù)中的值
2.1 基本數(shù)據(jù)類型:#{參數(shù)} 獲取參數(shù)中的值
2.2 復(fù)雜數(shù)據(jù)類型:#{屬性名} ,map中則是#{key}
3.案例
3.1 傳入Long型
mapper接口代碼:
public User findUserById(Long id);
xml代碼:
<select id="findUserById" parameterType="java.lang.Long" resultType="User">
select * from user where id = #{id};
</select>
3.2 傳入List
mapper接口代碼:
public List<User> findUserListByIdList(List<Long> idList);
xml代碼:
<select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach collection="list" item="id" index="index" separator=",">
#{id}
</foreach>
)
</where>
</select>
在使用foreach的時候最關(guān)鍵的也是最容易出錯的就是collection屬性。
該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的,主要有一下3種情況:
1. 如果傳入的是單參數(shù)且參數(shù)類型是一個List的時候,collection屬性值為list
2. 如果傳入的是單參數(shù)且參數(shù)類型是一個array數(shù)組的時候,collection的屬性值為array
3. 如果傳入的參數(shù)是多個的時候,我們就需要把它們封裝成一個Map了,當(dāng)然單參數(shù)也可
3.3 傳入數(shù)組:
mapper接口代碼:
public List<User> findUserListByIdList(int[] ids);
xml代碼:
<select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">
select * from user user
<where>
user.ID in (
<foreach collection="array" item="id" index="index" separator=",">
#{id}
</foreach>
)
</where>
</select>
3.4 傳入map
mapper接口代碼:
public boolean exists(Map<String, Object> map);
xml代碼:
<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">
SELECT COUNT(*) FROM USER user
<where>
<if test="code != null">
and user.CODE = #[code]
</if>
<if test="id != null">
and user.ID = #{id}
</if>
<if test="idList !=null ">
and user.ID in (
<foreach collection="idList" item="id" index="index" separator=",">
#{id}
</foreach>
)
</if>
</where>
</select>
MAP中有l(wèi)ist或array時,foreach中的collection必須是具體list或array的變量名。
比如這里MAP含有一個名為idList的list,所以MAP中用idList取值,這點(diǎn)和單獨(dú)傳list或array時不太一樣。
3.5傳入JAVA對象
mapper接口代碼:
public int findUserList(User user);
xml代碼:
<select id="findUserList" parameterType="User" resultType="java.lang.Integer">
SELECT COUNT(*) FROM USER user
<where>
<if test="code != null">
and user.CODE = #[code]
</if>
<if test="id != null">
and user.ID = #{id}
</if>
<if test="idList !=null ">
and user.ID in (
<foreach collection="idList" item="id" index="index" separator=",">
#{id}
</foreach>
)
</if>
</where>
</select>
JAVA對象中有l(wèi)ist或array時,foreach中的collection必須是具體list或array的變量名。
比如這里User含有一個名為idList的list,所以User中用idList取值,這點(diǎn)和單獨(dú)傳list或array時不太一樣。
3.6注解@Param
例子1:
注解單一屬性;這個類似于將參數(shù)重命名了一次
mapper接口代碼:
List<User> selectUserByTime(@Param(value="startdate")String startDate);
xml代碼:
<select id="selectUserByTime" resultType="User" parameterType="java.lang.String">
select * from t_user
where create_time >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD')
</select>
例子2:
注解javaBean
mapper接口代碼:
List<User> selectUserByTime(@Param(value="dateVO")DateVO dateVO);
xml代碼:
<select id="selectUserByTime" resultType="User" parameterType="DateVO">
select *
from t_user
where create_time >= to_date(#{dateVO.startDate,jdbcType=VARCHAR},'YYYY-MM-DD')
and create_time < to_date(#{dateVO.endDate,jdbcType=VARCHAR},'YYYY-MM-DD')
</select>
mybatis 之parameterType="Long"
<select id="selectByPrimaryKeyByArrayMemberId" resultType="memberModel" parameterType="Long">
select
<include refid="Base_Column_List"/>
from member m
where
m.IS_DELETE = 'N'
and m.member_id IN
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item,jdbcType=DECIMAL}
</foreach>
</select>
public ServiceMessage<List<Member>> selectByPrimaryKeyByArrayMemberId(
List<Long> memberIds)
{
try
{
if (memberIds == null || memberIds.size()==0){
return super.returnParamsError("參數(shù)為空!");
}
List<Member> list = memberMapper
.selectByPrimaryKeyByArrayMemberId(memberIds);
return super.returnCorrectResult(list);
}
catch (Throwable e)
{
return super.returnException(e);
}
}
public ServiceMessage<List<Member>> selectByPrimaryKeyByArrayMemberId(List<Long> memberIds);
List<Member> selectByPrimaryKeyByArrayMemberId(List<Long> memberIds);
@Test
public void testSelectByPrimaryKeyByArrayMemberId()
{
InternalMemberService internalMemberService = J1SOAHessianHelper.getService(url,InternalMemberService.class);
List<Long> memberIds = new ArrayList<Long>();
memberIds.add(1l);
memberIds.add(2l);
memberIds.add(1855l);
ServiceMessage<List<Member>> sm = internalMemberService.selectByPrimaryKeyByArrayMemberId(memberIds);
System.out.println(sm.getResult());
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud持久層框架MyBatis Plus的使用與原理解析
MyBatisPlus為MyBatis的增強(qiáng)版,專注于簡化數(shù)據(jù)庫操作,提供自動化CRUD、內(nèi)置分頁和樂觀鎖等功能,極大提升開發(fā)效率,在SpringCloud微服務(wù)架構(gòu)中,MyBatisPlus通過插件機(jī)制和自動生成代碼功能,有效支持企業(yè)級應(yīng)用和分布式系統(tǒng)的開發(fā)2024-10-10
java中rss解析器(rome.jar和jdom.jar)示例
這篇文章主要介紹了java中rss解析器(rome.jar和jdom.jar)示例,需要的朋友可以參考下2014-03-03
關(guān)于Java創(chuàng)建線程的2種方式以及對比
這篇文章主要給大家介紹了關(guān)于Java創(chuàng)建線程的2種方式以及對比的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-01-01
java隨機(jī)數(shù)生成具體實現(xiàn)代碼
這篇文章主要為大家分享了java隨機(jī)數(shù)生成具體實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-04-04
SpringBoot使用ResponseBodyEmitter處理流式日志和進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用ResponseBodyEmitter處理流式日志和進(jìn)度條,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02

