mybatis傳入參數類型parameterType詳解


前言

Mybatis的Mapper文件中的select、insert、update、delete元素中都有一個parameterType屬性,用於對應的mapper接口方法接受的參數類型。

( resultType:指定輸出結果類型,mybatis將sql查詢結果的一行記錄數據映射為resultType指定類型的對象。如果有多條數據,則分別進行映射,並把對象放到容器List中。所以即使返回是list數組,resultType也是pojo類型 )

1. MyBatis的傳入參數parameterType類型分兩種

   1. 1. 基本數據類型:int,string,long,Date;

   1. 2. 復雜數據類型:類和Map

2. 如何獲取參數中的值:

   2.1  基本數據類型:#{value}或${value} 獲取參數中的值

   2.2  復雜數據類型:#{屬性名}或${屬性名}  ,map中則是#{key}或${key}

注:#{}與${}的區別:

#{value}:輸入參數的占位符,相當於jdbc的?  防注入   自動添加了‘  ’ 引號!
例如:select * from user where username = #{name}  //輸入的參數lisa,就會自動加上引號
變成:select * from user where username = ‘lisa’
注意:value可以寫任意變量,沒有統一規定

${value}:  不防注入,就是字符串拼接 
 例如:select * from user where username like '%${value}%'  //默認不加引號
注意:只能寫value!!!

select * FROM user WHERE username like "%"'s'"%"//是正確的,符合語法,引號的形式只能是這樣,不能變!

3.案例:

 3.1 基本數據類型案例

<sql id="Base_Column_List" >
id, car_dept_name, car_maker_name, icon,car_maker_py,hot_type
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from common_car_make
where id = #{id,jdbcType=BIGINT}
</select>
 3.2 復雜類型--map類型   

<select id="queryCarMakerList" resultMap="BaseResultMap" parameterType="java.util.Map">
select
<include refid="Base_Column_List" />
from common_car_make cm
where 1=1
<if test="id != null">
and cm.id = #{id,jdbcType=DECIMAL}
</if>
<if test="carDeptName != null">
and cm.car_dept_name = #{carDeptName,jdbcType=VARCHAR}
</if>
<if test="carMakerName != null">
and cm.car_maker_name = #{carMakerName,jdbcType=VARCHAR}
</if>
<if test="hotType != null" >
and cm.hot_type = #{hotType,jdbcType=BIGINT}
</if>
ORDER BY cm.id
</select>
 3.3 復雜類型--類類型

<update id="updateByPrimaryKeySelective" parameterType="com.epeit.api.model.CommonCarMake" >
update common_car_make
<set>
<if test="carDeptName != null" >
car_dept_name = #{carDeptName,jdbcType=VARCHAR},
</if>
<if test="carMakerName != null" >
car_maker_name = #{carMakerName,jdbcType=VARCHAR},
</if>
<if test="icon != null" >
icon = #{icon,jdbcType=VARCHAR},
</if>
<if test="carMakerPy != null" >
car_maker_py = #{carMakerPy,jdbcType=VARCHAR},
</if>
<if test="hotType != null" >
hot_type = #{hotType,jdbcType=BIGINT},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
 3.4 復雜類型--map中包含數組的情況

<select id="selectProOrderByOrderId" resultType="com.epeit.api.model.ProOrder" parameterType="java.util.HashMap" >
select sum(pro_order_num) proOrderNum,product_id productId,promotion_id promotionId
from pro_order
where 1=1
<if test="orderIds != null">
and
<foreach collection="orderIds" item="item" open="order_id IN(" separator="," close=")">
#{item,jdbcType=BIGINT}
</foreach>
</if>
GROUP BY product_id,promotion_id
</select>
4. Mybatis (ParameterType) 如何傳遞多個不同類型的參數

4.1 方法一:不需要寫parameterType參數

public List<XXXBean> getXXXBeanList(String xxId, String xxCode);

<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{0} and name = #{1}
</select>
由於是多參數那么就不能使用parameterType, 改用#{index}是第幾個就用第幾個的索引,索引從0開始

4.2 方法二:基於注解(最簡單)

public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);

<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{id} and name = #{code}
</select>
由於是多參數那么就不能使用parameterType, 這里用@Param來指定哪一個

4.3 方法三:Map封裝

public List<XXXBean> getXXXBeanList(HashMap map);


<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
select 字段... from XXX where id=#{xxId} code = #{xxCode}
</select>
其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那個就在#{}使用那個,map如何封裝就不用了我說了吧。

4.4 方法四:List封裝

public List<XXXBean> getXXXBeanList(List<String> list);

<select id="getXXXBeanList" resultType="XXBean">
  select 字段... from XXX where id in
  <foreach item="item" index="index" collection="list" open="(" separator=","close=")">
    #{item}
  </foreach>
</select>
--------------------- 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM