、单个参数 mybatis不会做特殊处理。

  #{参数名/任意名}:取出参数值。

、多个参数 mybatis会做特殊处理。

  多个参数会被封装成 一个map。

  key:param1...paramN,或者参数的索引也可以。

  value:传入的参数值。

  #{}就是从map中获取指定的key的值;

  多个参数传递的时候要使用命名参数的形式:

、命名参数:明确指定封装参数时map的key;@Param("id")

  多个参数会被封装成 一个map,
  key:使用@Param注解指定的值
  value:参数值
  #{指定的key}取出对应的参数值 、PoJO 如果多个参数正好是我们业务逻辑的数据模型,我们就可以直接传入pojo;#{属性名}:取出传入的pojo的属性值。 、Map:如果多个参数不是业务模型中的数据,没有对应的pojo,不经常使用,为了方便,我们也可以传入map。      #{key}:取出map中对应的值 、TO:如果多个参数不是业务模型中的数据,但是经常要使用,推荐来编写一个TO(Transfer Object)数据传输对象。   例如分页:     Page{
    int index;
    int size;
    } 、例子: public Employee getEmp(@Param("id")Integer id,String lastName);
取值:id==>#{id/param1} lastName==>#{param2} public Employee getEmp(Integer id,@Param("e")Employee emp);
取值:id==>#{param1} lastName===>#{param2.lastName/e.lastName}
  特别注意: 如果是Collection(List、Set)类型或者是数组,也会特殊处理。也是把传入的list或者数组封装在map中。   key的取值:       key:Collection(collection)。          List(list)          数组(array) public Employee getEmpById(List<Integer> ids);
取值:取出第一个id的值: #{list[]}
、参数值的获取   #{}:可以获取map中的值或者pojo对象属性的值;   ${}:可以获取map中的值或者pojo对象属性的值; select * from tbl_employee where id=${id} and last_name=#{lastName}
Preparing: select * from tbl_employee where id= and last_name=?
  区别:     #{}:是以预编译的形式,将参数设置到sql语句中;PreparedStatement;防止sql注入。     ${}:取出的值直接拼装在sql语句中;会有安全问题;大多情况下,我们去参数的值都应该去使用#{};   原生jdbc不支持占位符的地方我们就可以使用${}进行取值,比如分表、排序。。。;按照年份分表拆分  select * from ${year}_salary where xxx;
select * from tbl_employee order by ${f_name} ${order}
、#{}:更丰富的用法:   规定参数的一些规则:   javaType、 jdbcType、 mode(存储过程)、 numericScale、   resultMap、 typeHandler、 jdbcTypeName、 expression(未来准备支持的功能);   jdbcType通常需要在某种特定的条件下被设置:     在我们数据为null的时候,有些数据库可能不能识别mybatis对null的默认处理。比如Oracle(报错);     JdbcType OTHER:无效的类型;因为mybatis对所有的null都映射的是原生Jdbc的OTHER类型,oracle不能正确处理;     由于全局配置中:jdbcTypeForNull=OTHER;oracle不支持;两种办法     、#{email,jdbcType=OTHER};     、jdbcTypeForNull=NULL        <setting name="jdbcTypeForNull" value="NULL"/>

二、封装MAP

//返回一个map,key是列名,value是值
public Map<String,Object> getUserByIdReturnMap(Integer id);
//返回一个map,key是主键,value是值
@MapKey("id")
public Map<Integer,User> getUserByLastName(String lastName);

mapper配置

     <select id="getUserByLastName" resultType="model.User">
SELECT * FROM
user WHERE last_name like "%"#{lastName}"%"
</select>
<select id="getUserByIdReturnMap" resultType="map">
SELECT * FROM
user WHERE id=#{id}
</select>

三、自定义返回值类型

<1>

实体类定义别名

@Alias("user")
public class User { private int id;
private String lastName;
private String email;
private String gender;

public User selectUserById(Integer id);方法

mapper配置

<!-- 自定义javabeen的规则
type自定义java规则
id 唯一id方便引用
column:指定哪一列
property:指定javaBean属性
-->
<resultMap type="user" id="Myuser">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>
<select id="selectUserById" resultMap="Myuser">
SELECT * FROM
user WHERE id=#{id}
</select>

<2>

@Alias("user")
public class User { private int id;
private String lastName;
private String email;
private String gender;
private Department dept;
@Alias("depart")
public class Department { private Integer id;
private String departName;

public User selectUserAndDepart(Integer id);

mapper配置

<resultMap type="user" id="Mymap">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="depart_id" property="dept.id"/>
<result column="depart_name" property="dept.departName"/>
</resultMap>
<select id="selectUserAndDepart" resultMap="Mymap">
SELECT u.id,u.depart_id,u.email,u.gender,u.last_name,d.depart_name FROM USER
u,department d WHERE u.depart_id=d.id AND u.id=#{id}
</select>

mapper配置2

<resultMap type="user" id="Mymap2">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<association property="dept" javaType="depart">
<id column="id" property="id"/>
<result column="depart_name" property="departName"/>
</association>
</resultMap>

<select id="selectUserAndDepart" resultMap="Mymap2">
SELECT u.id,u.depart_id,u.email,u.gender,u.last_name,d.depart_name FROM USER
u,department d WHERE u.depart_id=d.id AND u.id=#{id}
</select>

<3>分布查询

<resultMap type="user" id="stepMap">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<association property="dept" select="mapper.DepartMapper.selectDepartById" column="depart_id" >
    
<association property="dept" select="mapper.DepartMapper.selectDepartById" column="{id=depart_id}" fetchType="eager">
</association>
{key1=column1,key2=column2}(多个参数) fetchType="lazy"(延迟加载)||"eager"(立刻加载)
        </association>
</resultMap>
<!-- 分布查询 -->
<select id="selectUserByIdStep" resultMap="stepMap">
SELECT * FROM USER WHERE id =#{id}
</select>
<mapper namespace="mapper.DepartMapper">
<select id="selectDepartById" resultType="depart">
SELECT * FROM
department WHERE id=#{id}
</select>
</mapper>

mybatis二(参数处理和map封装及自定义resultMap)的更多相关文章

  1. Mybatis传入参数类型为Map

    mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...

  2. 【batch】批处理文件多参数处理和for循环字符串连接

    batch文件写起来,酸爽不谈了.[1]今天在github上发现个好东西batsh,运行地址:https://batsh.org/.[1] 这里需求的场景是:调用run.bat脚本并传入多个参数(相对 ...

  3. MyBatis(二):Select语句传递参数的集中方案

    从别人说的方案中看出,传递参数方案还挺多,不如自己整理下,以便以后使用过程中有个笔记回忆录. 1.传递一个参数的用法: 配置文件 <select id="getById" r ...

  4. MyBatis传入集合 list 数组 map参数的写法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...

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

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

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

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

  7. 学习笔记:CentOS7学习之二十五:shell中色彩处理和awk使用技巧

    目录 学习笔记:CentOS7学习之二十五:shell中色彩处理和awk使用技巧 25.1 Shell中的色彩处理 25.2 awk基本应用 25.2.1 概念 25.2.2实例演示 25.3 awk ...

  8. 【转载】Mybatis多参数查询映射

    转载地址:http://www.07net01.com/zhishi/402787.html 最近在做一个Mybatis的项目,由于是接触不久,虽然看了一下资料,但在实际开发中还是暴 露了很多问题,其 ...

  9. 【MyBatis】解析MyBatis传入参数的问题

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

随机推荐

  1. Flask--四种请求钩子函数

    请求钩子函数:请求前,请求后需要做的处理 @app.before_first_request-在第一次请求之前执行 @app.before_request-在每一次请求之前执行 @app.after_ ...

  2. VMWare VSphere6.0的实验笔记

    在现有的一个vsphere6.0虚拟平台上环境下搭建一套VSphere环境平台. 任务1: 1.建立1个win2008主机,192.168.12.10.16Gram,40G硬盘1独立存储+150G硬盘 ...

  3. JavaScript之图片操作5

    本次的图片操作是要实现模仿天猫淘宝的放大镜效果,如下图所示: 其实现原理其实很简单,主要就是定位的运用,在上面的图中,左边是一个div,它的大小就是左边图片的大小,我们称为左窗口(原图),红色部分我们 ...

  4. 廖雪峰Java5集合-6Stack-1使用Stack

    1.栈的定义 栈Stack是一种后进先出(LIFO: Last In First Out)的数据结构,可以看作一端封闭的容器,先进去的元素永远在底部,最后出来. 栈有2个重要的方法: push(E e ...

  5. Node.js做的代理转发服务器

    可以代理苹果ID服务器 const http = require('http'); const https = require('https'); const client = require('ht ...

  6. 简单的单进程FTP服务器的实现

    一.功能说明: 1.本程序基于socket实现客户端与服务器端的单进程交互 2.用到的用户名:whw,密码abc123——服务器端密码的验证利用hashlib模块进行MD5的编码以确保通信安全. 3. ...

  7. [UE4]Task的定义与使用

    在Task蓝图里面可以像普通蓝图一样添加函数.变量. 也可以通过使用“set blackboard value as”设置黑板变量,使用“get blackboard value as”获得黑板变量值 ...

  8. ​游戏设计思考:对COK的理解和思考

    转自:http://www.gameres.com/804983.html 一.前言 发此文的起因是最近加入了一个游戏研究群,受到大家对游戏研究热情的感染,也想将自己对游戏的理解和感悟发出来和大家一起 ...

  9. Sep 10th 2018

    今天是教师节,祝家里的两位‘老师’节日快乐.一位是幼儿园的保健医,另一位是驾校的教练.不能说是真正的老师,但作的也是传道授业之工作.今天看到新闻,马云要在明年的今天辞去现任阿里巴巴主席一职,继续投身他 ...

  10. json与bson的区别

    bson是由10gen开发的一个数据格式,目前主要用于mongoDB中,是mongoDB的数据存储格式.bson基于json格式,选择json进行改造的原因主要是json的通用性及json的schem ...