在使用Mybatis开发时,借助xml来写具体的sql,再写传参类型或者返回结果类型时,通常会与ParameterType, ParameterMap, ResultMap, ResultType这四个打交到,那么这个Type与Map到底怎么区别,什么时候要指定类型,什么时候又可以不指定呢?
I. 环境配置
我们使用SpringBoot + Mybatis + MySql来搭建实例demo
- springboot: 2.2.0.RELEASE
- mysql: 5.7.22
1. 项目配置
1 2 3 4 5 6 7 8 9 10 11
| <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
1 2 3 4 5
| 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. 数据库表
用于测试的数据库
1 2 3 4 5 6 7 8 9 10
| 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
,当然也可以根据实际需要指定
1 2 3 4 5 6
| 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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时,可以如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @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也是一个好的选择,如
1
| List<MoneyPo> queryByNameV2(Map<String, Object> params);
|
对应的sql如下,这里主要是为了演示这个使用姿势,StrTypeHandler
是一个自定义的类型抓换,不管传参什么类型,都转成String
1 2 3 4 5 6 7
| <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中核心数据如下图

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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); System.out.println(moneyMapperV4.queryByCondition(map2)); }
|
输出如下
1 2 3 4
| [] [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. 不能错过的源码和相关知识点
0. 项目
系列博文:
1. 微信公众号: 一灰灰Blog
尽信书则不如无书,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
一灰灰blog
Be the first person to leave a comment!