7

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

 2 years ago
source link: https://spring.hhui.top/spring-blog/2021/11/06/211106-SpringBoot%E7%B3%BB%E5%88%97Mybatis%E4%B9%8BParameterMap%E3%80%81ParameterType%E4%BC%A0%E5%8F%82%E7%B1%BB%E5%9E%8B%E6%8C%87%E5%AE%9A%E4%BD%BF%E7%94%A8%E5%A7%BF%E5%8A%BF/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
logo.jpg

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

核心的依赖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:

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;

II. Parameter/Result介绍

1. ParameterMap & ParameterType

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

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>

通常有两种场景会经常看到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>

若传参为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>

说明

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

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

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

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

对应的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>

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));
}
[]
[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)]

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

系列博文:

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

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

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

一灰灰blog

打赏 如果觉得我的文章对您有帮助,请随意打赏。

分享到


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK