在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType為輸入參數,在配置的時候,配置相應的輸入參數類型即可。parameterType有基本數據類型和復雜的數據類型配置。
1. MyBatis的傳入參數parameterType類型分兩種
1. 1. 基本數據類型:int、string、long、Date;
1. 2. 復雜數據類型:類(JavaBean、Integer等)和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屬性值為list
2. 如果傳入的是單參數且參數類型是一個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 傳入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中有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>
---------------------
作者:Remember_Ray 來源:
CSDN 原文:https://blog.csdn.net/q343509740/article/details/80578832