前言

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

mybatis传入参数类型parameterType和输出结果类型resultType详解的更多相关文章

  1. MyBatis传入参数与parameterType

    参考:http://openwares.net/database/mybatis_parametertype.html Mybatis的Mapper文件中的select.insert.update.d ...

  2. MyBatis 传入参数之parameterType

      在MyBatis的select,insert,update,delete这些元素中都提到了parameterType这个属性.MyBatis现在使用parameterType有基本类型和JAVA复 ...

  3. C++函数的传入参数是指针的指针(**)的详解

    要修改变量的值,需要使用变量类型的指针作为参数或者变量的引用.如果变量是一般类型的变量,例如int,则需要使用int 类型的指针类型int *作为参数或者int的引用类型int&.但是如果变量 ...

  4. MyBatis传入参数为list、数组、map写法

    1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item,index,collection,open,sep ...

  5. MyBatis传入参数为list、数组、map写法(转载)

    MyBatis传入参数为list.数组.map写法 1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item ...

  6. golang格式化输出-fmt包用法详解

    golang格式化输出-fmt包用法详解 注意:我在这里给出golang查询关于包的使用的地址:https://godoc.org    声明: 此片文章并非原创,大多数内容都是来自:https:// ...

  7. Mybatis中接口和对应的mapper文件位置配置详解

    Mybatis中接口和对应的mapper文件位置配置详解 原链接为:https://blog.csdn.net/fanfanzk1314/article/details/71480954 今天遇到一个 ...

  8. mybatis传入参数类型parameterType详解

    前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型. ( res ...

  9. MyBatis传入参数为集合、数组SQL写法

    参考:http://blog.csdn.net/small____fish/article/details/8029030 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合 ...

随机推荐

  1. [Selenium] 使用Firefox Driver 示例

    //导入Selenium 库和FirefoxDriver 库 package com.learningselenium.simplewebdriver; import org.openqa.selen ...

  2. IOS中的沙盒机制

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...

  3. Ruby: Print WIN32OLE method names in Ruby

    class WIN32OLE   def list_ole_methods     method_names = ole_methods.collect {|m| m.name}     puts m ...

  4. vue项目中的路径别名

    每次写引入组件的路径,如果路径嵌套比较深,那么会比较麻烦,我们可以在webpack.base.conf.js,中设置路径的别名,默认webpack设置src的别名为@ 建议配置src下一级目录的别名, ...

  5. Codeforces - 466C - Number of Ways - 组合数学

    https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以 ...

  6. 51nod 1574 排列转换(猜结论)

    分析 猜了一下结论,居然对了..........具体操作是:假设排列s是1,2,3,...,nk为排列p中最大的 没有放到正确位置的数,k的位置为posk的右边一定有一个数x<=pos(因为&l ...

  7. 51nod 1094 【水题】

    暴力即可!!! #include <stdio.h> #include <string.h> #include <iostream> using namespace ...

  8. Template Code 无法使用 this.Host 报错

    问题显示: Error 6 Compiling transformation: 'Microsoft.VisualStudio.TextTemplatingBED07DAE3B6FD53FA94701 ...

  9. JAVA多线程(四) Executor并发框架向RabbitMQ推送消息

    github代码地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...

  10. 关于使用IQKeyBoardManager键盘还是被遮挡的问题解决方案

    今天在做一个登录界面的时候发现使用了IQKeyBoardManager键盘还是被遮挡,解决方案如下 解决方案一:在所有视图的最外层添加一个UIView作为容器即可,但在有导航栏的情况下导航栏会跟着向上 ...