一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

    • H2Database

    • JPA

    • JdbcTemplate

    • Jooq

    • Mybatis

      • 【DB系列】Mybatis+xml整合篇
      • 【DB系列】Mybatis+注解整合篇
      • 【DB系列】MybatisPlus整合篇
      • 【DB系列】Mybatis-Plus代码自动生成
      • 【DB系列】Mybatis多数据源配置与使用
      • 【DB系列】Mybatis-Plus多数据源配置
      • 【DB系列】Mybatis基于AbstractRoutingDataSource与AOP实现多数据源切换
      • 【DB系列】SpringBoot系列Mybatis之Mapper注册的几种方式
      • 【DB系列】SpringBoot系列Mybatis之Mapper接口与Sql绑定几种姿势
      • 【DB系列】SpringBoo系列Mybatis之自定义类型转换TypeHandler
      • 【DB系列】SpringBoot系列Mybatis之插件机制Interceptor
      • 【DB系列】Mybatis系列教程之CURD基本使用姿势
      • 【DB系列】Mybatis传参作为字段/表名时的注意事项
      • 【DB系列】Mybatis系列教程之CURD基本使用姿势-注解篇
      • 【DB系列】Mybatis之参数传递的几种姿势
      • 【DB系列】Mybatis之转义符的使用姿势
      • 【DB系列】Mybatis之传参类型如何确定
      • 【DB系列】Mybatis之ParameterMap、ParameterType传参类型指定使用姿势
        • I. 环境配置
          • 1. 项目配置
          • 2. 数据库表
        • II. Parameter/Result介绍
          • 1. ParameterMap & ParameterType
          • 2. 最后验证一下我们的使用
        • III. 不能错过的源码和相关知识点
          • 0. 项目
          • 1. 微信公众号: 一灰灰Blog
      • 【DB系列】Mybatis之ResultMap、ResultType返回结果使用姿势
      • 【DB系列】Mybatis之批量插入的几种姿势
    • 事务

    • MongoDB

    • Redis

    • 实例

  • 搜索系列

  • MQ系列

  • WEB系列

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • DB系列
  • Mybatis
一灰灰
2021-11-06

【DB系列】Mybatis之ParameterMap、ParameterType传参类型指定使用姿势

在使用Mybatis开发时,借助xml来写具体的sql,再写传参类型或者返回结果类型时,通常会与ParameterType, ParameterMap, ResultMap, ResultType这四个打交到,那么这个Type与Map到底怎么区别,什么时候要指定类型,什么时候又可以不指定呢?

# I. 环境配置

我们使用SpringBoot + Mybatis + MySql来搭建实例demo

  • springboot: 2.2.0.RELEASE
  • mysql: 5.7.22

# 1. 项目配置

<dependencies>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11

核心的依赖mybatis-spring-boot-starter,至于版本选择,到mvn仓库中,找最新的

另外一个不可获取的就是db配置信息,appliaction.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password:
1
2
3
4
5

# 2. 数据库表

用于测试的数据库

CREATE TABLE `money` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
  `money` int(26) NOT NULL DEFAULT '0' COMMENT '钱',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0',
  `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=551 DEFAULT CHARSET=utf8mb4;
1
2
3
4
5
6
7
8
9
10

# II. Parameter/Result介绍

# 1. ParameterMap & ParameterType

这两个主要用于指定传参类型,前面有一篇介绍过传参的姿势有兴趣的小伙伴可以查看一下【DB系列】Mybatis之参数传递的几种姿势 (opens new window)

mybatis中传参一般可以区分为两类

  • 基本数据类型:int、string、long、Date;
  • 复杂数据类型:类(JavaBean、Integer等)和Map

一般来说基本的参数类型,在xml中的sql编写不需要额外的指定ParameterType,当然也可以根据实际需要指定

List<MoneyPo> queryByName(@Param("name") String name);


<select id="queryByName"  parameterType="java.lang.String" resultMap="BaseResultMap">
    select * from money where `name` = #{name}
</select>
1
2
3
4
5
6

通常有两种场景会经常看到ParmeterType,比如传参为Map

List<MoneyPo> queryByCondition(Map<String, Object> params);

<select id="queryByCondition" resultMap="BaseResultMap" parameterType="java.util.Map">
    select
    <include refid="money_po"/>
    from money where 1=1
    <if test="id != null">
        and id = #{id}
    </if>
    <if test="name != null">
        and `name` = #{name}
    </if>
    <if test="is_deleted != null">
        and `is_deleted` = #{is_deleted}
    </if>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

若传参为Java bean时,可以如下

@Data
public class QueryBean {
    private String name;
    private Long id;
}

List<MoneyPo> queryByBean(QueryBean queryBean);

<select id="queryByBean" resultMap="BaseResultMap" parameterType="com.git.hui.boot.mybatis.entity.QueryBean">
    select
    <include refid="money_po"/>
    from money where 1=1
    <if test="id != null">
        and id = #{id}
    </if>
    <if test="name != null">
        and `name` = #{name}
    </if>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

说明

  • 上面的几个case中,也可以都不指定parameterType

上面说到的都是parameterType,那么什么时候会用到parameterMap呢?

当我们希望针对某些查询条件做一些TypeHandler时,除了在#{}中指定之外,借助parameterMap也是一个好的选择,如

List<MoneyPo> queryByNameV2(Map<String, Object> params);
1

对应的sql如下,这里主要是为了演示这个使用姿势,StrTypeHandler是一个自定义的类型抓换,不管传参什么类型,都转成String

<parameterMap id="queryMap" type="java.util.Map">
    <parameter property="name" typeHandler="com.git.hui.boot.mybatis.handler.StrTypeHandler"/>
</parameterMap>

<select id="queryByNameV2" parameterMap="queryMap" resultMap="BaseResultMap">
    select * from money where `name` = #{name}
</select>
1
2
3
4
5
6
7

# 2. 最后验证一下我们的使用

db中核心数据如下图

public void testV4() {
    System.out.println(moneyMapperV4.queryByName("1"));

    Map<String, Object> map = new HashMap<>();
    map.put("id", 1L);
    map.put("name", "一灰灰blog");
    System.out.println(moneyMapperV4.queryByCondition(map));

    QueryBean queryBean = new QueryBean();
    queryBean.setId(1L);
    queryBean.setName("一灰灰blog");
    System.out.println(moneyMapperV4.queryByBean(queryBean));


    Map<String, Object> map2 = new HashMap<>();
    map2.put("name", 120L); // 即便传入的是Long类型,最终传递到mysql时,借助TypeHandler也会会转换成字符串类型
    System.out.println(moneyMapperV4.queryByCondition(map2));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

输出如下

[]
[MoneyPo(id=1, name=一灰灰blog, money=100, isDeleted=0, createAt=2019-04-18 17:01:40.0, updateAt=2019-04-18 17:01:40.0, cnt=null, bank=null)]
[MoneyPo(id=1, name=一灰灰blog, money=100, isDeleted=0, createAt=2019-04-18 17:01:40.0, updateAt=2019-04-18 17:01:40.0, cnt=null, bank=null)]
[MoneyPo(id=120, name=120, money=200, isDeleted=0, createAt=2021-05-24 20:04:39.0, updateAt=2021-09-27 19:21:40.0, cnt=null, bank=null)]
1
2
3
4

# III. 不能错过的源码和相关知识点

# 0. 项目

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 源码:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/103-mybatis-xml (opens new window)

系列博文:

  • 【DB系列】Mybatis系列教程之CURD基本使用姿势 (opens new window)
  • 【DB系列】Mybatis系列教程之CURD基本使用姿势-注解篇 (opens new window)
  • 【DB系列】Mybatis之参数传递的几种姿势 (opens new window)
  • 【DB系列】Mybatis之转义符的使用姿势 (opens new window)
  • 【DB系列】Mybatis之传参类型如何确定 (opens new window)

# 1. 微信公众号: 一灰灰Blog

尽信书则不如无书,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#Mybatis
上次更新: 2022/02/27, 11:13:23
【DB系列】Mybatis之传参类型如何确定
【DB系列】Mybatis之ResultMap、ResultType返回结果使用姿势

← 【DB系列】Mybatis之传参类型如何确定 【DB系列】Mybatis之ResultMap、ResultType返回结果使用姿势→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×