mybatis传入参数类型parameterType和输出结果类型resultType详解

前言

Mybatis的Mapper文件中的select、insert、update、delete元素中都有一个parameterType和resultType属性,parameterType属性用于对应的mapper接口方法接受的参数类型,resultType用于指定sql输出的结果类型。

resultType:
指定sql输出结果类型,总共就两种:

1. 基本数据类型。

2. pojo类类型。mybatis将sql查询结果的一行记录数据映射为resultType指定类型的对象。如果有多条数据,则分别进行映射,并把对象放到容器List中。所以即使返回是list数组,resultType也是pojo类型

parameterType:
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

//mapKey其实就是#{mapKey}
<if test="mapKey != null">
and

//map中的list同普通的一样,只是在遍历的时候collection要写出map中的List的键值
<foreach collection="mapKey" item="itemOfMapKey" open="order_id IN(" separator="," close=")">
#{itemOfMapKey,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>
 
---------------------

原文地址:https://www.cnblogs.com/hyhy904/p/11053920.html