SSM專案從零開始到入門026-mybatis的parameterType傳入引數型別
阿新 • • 發佈:2019-01-31
在mybatis對映介面的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType為輸入引數,在配置的時候,配置相應的輸入引數型別即可。parameterType有基本資料型別和複雜的資料型別配置。
1. MyBatis的傳入引數parameterType型別分兩種
1. 1. 基本資料型別:int,string,long,Date;1. 2. 複雜資料型別:類和Map
2. 如何獲取引數中的值:
2.1 基本資料型別:#{引數} 獲取引數中的值2.2 複雜資料型別:#{屬性名} ,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的時候最關鍵的也是最容易出錯的就是collection屬性。
該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的,主要有一下3種情況:
1. 如果傳入的是單引數且引數型別是一個List的時候,collection屬性值為list2. 如果傳入的是單引數且引數型別是一個array陣列的時候,collection的屬性值為array
3. 如果傳入的引數是多個的時候,我們就需要把它們封裝成一個Map了,當然單引數也可
3.3 傳入陣列
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 傳入mapmapper介面程式碼:
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中有list或array時,foreach中的collection必須是具體list或array的變數名。
比如這裡MAP含有一個名為idList的list,所以MAP中用idList取值,這點和單獨傳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物件中有list或array時,foreach中的collection必須是具體list或array的變數名。
比如這裡User含有一個名為idList的list,所以User中用idList取值,這點和單獨傳list或array時不太一樣。
3.6註解@Param
例子1:
註解單一屬性;這個類似於將引數重新命名了一次
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>