MyBatis(四):mybatis中使用in查询时的注意事项
准备工作
1)创建测试表
jobitem
CREATE TABLE "jobitem" (
"id" bigint(20) NOT NULL AUTO_INCREMENT COMMENT '唯一键 pk',
"appId" varchar(32) NOT NULL COMMENT 'yarn任务id(applicationId)',
"submitFilePath" varchar(256) NOT NULL COMMENT '提交脚本路径',
"state" varchar(16) DEFAULT NULL COMMENT '任务状态',
"monitorType" varchar(512) DEFAULT NULL COMMENT '监控列表',
"createUserId" varchar(32) NOT NULL COMMENT '创建者关联Id',
"createUserName" varchar(32) NOT NULL COMMENT '创建者用户名',
"createTime" datetime NOT NULL COMMENT '创建时间',
PRIMARY KEY ("id"),
UNIQUE KEY "key" ("appId")
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='yarn任务持久化存储对象';
备注:这里mysql版本是5.7
2)使用mybatis-generator-plugin生成实体类:
Jobitem.java
package com.boco.jobmonitor.model; import java.util.Date; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; public class Jobitem {
/**
* 唯一键 pk<br>
* 列名:id 类型:INTEGER(10) 允许空:false 缺省值:null
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; /**
* yarn任务id(applicationId)<br>
* 列名:appId 类型:VARCHAR(32) 允许空:false 缺省值:null
*/
private String appid; /**
* 提交脚本路径<br>
* 列名:submitFilePath 类型:VARCHAR(256) 允许空:false 缺省值:null
*/
private String submitfilepath; /**
* 任务状态<br>
* 列名:state 类型:VARCHAR(16) 允许空:true 缺省值:null
*/
private String state; /**
* 监控列表<br>
* 列名:monitorType 类型:VARCHAR(512) 允许空:true 缺省值:null
*/
private String monitortype; /**
* 创建者关联Id<br>
* 列名:createUserId 类型:VARCHAR(32) 允许空:false 缺省值:null
*/
private String createuserid; /**
* 创建者用户名<br>
* 列名:createUserName 类型:VARCHAR(32) 允许空:false 缺省值:null
*/
private String createusername; /**
* 创建时间<br>
* 列名:createTime 类型:TIMESTAMP(19) 允许空:false 缺省值:null
*/
private Date createtime; public Jobitem() {
} public Jobitem(String appid, String submitfilepath, String state, String monitortype, String createuserid,
String createusername, Date createtime) {
super();
this.appid = appid;
this.submitfilepath = submitfilepath;
this.state = state;
this.monitortype = monitortype;
this.createuserid = createuserid;
this.createusername = createusername;
this.createtime = createtime;
} public Jobitem(Long id, String appid, String submitfilepath, String state, String monitortype,
String createuserid, String createusername, Date createtime) {
super();
this.id = id;
this.appid = appid;
this.submitfilepath = submitfilepath;
this.state = state;
this.monitortype = monitortype;
this.createuserid = createuserid;
this.createusername = createusername;
this.createtime = createtime;
} /**
* 唯一键 pk
*
* @author boco
* @return id 唯一键 pk
*/
public Long getId() {
return id;
} /**
* 唯一键 pk
*
* @author boco
* @param id
* 唯一键 pk
*/
public void setId(Long id) {
this.id = id;
} /**
* yarn任务id(applicationId)
*
* @author boco
* @return appId yarn任务id(applicationId)
*/
public String getAppid() {
return appid;
} /**
* yarn任务id(applicationId)
*
* @author boco
* @param appid
* yarn任务id(applicationId)
*/
public void setAppid(String appid) {
this.appid = appid == null ? null : appid.trim();
} /**
* 提交脚本路径
*
* @author boco
* @return submitFilePath 提交脚本路径
*/
public String getSubmitfilepath() {
return submitfilepath;
} /**
* 提交脚本路径
*
* @author boco
* @param submitfilepath
* 提交脚本路径
*/
public void setSubmitfilepath(String submitfilepath) {
this.submitfilepath = submitfilepath == null ? null : submitfilepath.trim();
} /**
* 任务状态
*
* @author boco
* @return state 任务状态
*/
public String getState() {
return state;
} /**
* 任务状态
*
* @author boco
* @param state
* 任务状态
*/
public void setState(String state) {
this.state = state == null ? null : state.trim();
} /**
* 监控列表
*
* @author boco
* @return monitorType 监控列表
*/
public String getMonitortype() {
return monitortype;
} /**
* 监控列表
*
* @author boco
* @param monitortype
* 监控列表
*/
public void setMonitortype(String monitortype) {
this.monitortype = monitortype == null ? null : monitortype.trim();
} /**
* 创建者关联Id
*
* @author boco
* @return createUserId 创建者关联Id
*/
public String getCreateuserid() {
return createuserid;
} /**
* 创建者关联Id
*
* @author boco
* @param createuserid
* 创建者关联Id
*/
public void setCreateuserid(String createuserid) {
this.createuserid = createuserid == null ? null : createuserid.trim();
} /**
* 创建者用户名
*
* @author boco
* @return createUserName 创建者用户名
*/
public String getCreateusername() {
return createusername;
} /**
* 创建者用户名
*
* @author boco
* @param createusername
* 创建者用户名
*/
public void setCreateusername(String createusername) {
this.createusername = createusername == null ? null : createusername.trim();
} /**
* 创建时间
*
* @author boco
* @return createTime 创建时间
*/
public Date getCreatetime() {
return createtime;
} /**
* 创建时间
*
* @author boco
* @param createtime
* 创建时间
*/
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
}
使用List对sql in进行传参时
如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
JobitemMapper接口类:
List<Jobitem> findByIdList(List<String> appIds);
对应JobitemMapper.xml文件配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dx.jobmonitor.mapper.JobitemMapper" >
<resultMap id="BaseResultMap" type="com.dx.jobmonitor.model.Jobitem" >
<!--
WARNING - @mbggenerated
-->
<id column="id" property="id" jdbcType="BIGINT" />
<result column="appId" property="appid" jdbcType="VARCHAR" />
<result column="submitFilePath" property="submitfilepath" jdbcType="VARCHAR" />
<result column="state" property="state" jdbcType="VARCHAR" />
<result column="monitorType" property="monitortype" jdbcType="VARCHAR" />
<result column="createUserId" property="createuserid" jdbcType="VARCHAR" />
<result column="createUserName" property="createusername" jdbcType="VARCHAR" />
<result column="createTime" property="createtime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
<!--
WARNING - @mbggenerated
-->
id,appId,submitFilePath,state,monitorType,createUserId,createUserName,createTime
</sql>
<select id="findByIdList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from _jobitem where appId in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>
</mapper>
测试代码:
package com.dx.jobmonitor.web; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.dx.jobmonitor.App;
import com.dx.jobmonitor.mapper.JobitemMapper;
import com.dx.jobmonitor.model.Jobitem; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { App.class, obitemMapper.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JobitemTest { @Autowired
private JobitemMapper sJobitemMapper; @Test
public void testFindByIdList() {
List<String> appIds = new ArrayList<String>();
appIds.add("application_1548381669007_0057");
appIds.add("application_1548381669007_0056");
appIds.add("application_1548381669007_0055"); List<Jobitem> result = sJobitemMapper.findByIdList(appIds); Assert.assertEquals(3, result.size());
}
}
使用Array对sql in进行传参时
如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
JobitemMapper接口类:
List<Jobitem> findByIdArray(String[] appIds);
对应JobitemMapper.xml文件配置:
<select id="findByIdArray" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from _jobitem where appId in
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
#{item}
</foreach>
</select>
测试代码:
@Test
public void testFindByIdArray() {
String[] appIds = new String[] { "application_1548381669007_0057", "application_1548381669007_0056",
"application_1548381669007_0055" }; List<Jobitem> result = sJobitemMapper.findByIdArray(appIds); Assert.assertEquals(3, result.size());
}
使用Map对sql in传递多参数时
当查询的参数有多个时,例如 findByIds(String name, Long[] ids)。这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称
JobitemMapper接口类:
List<Jobitem> findByIdMap(Map<String, Object> creatorAndappIds);
对应JobitemMapper.xml文件配置:
<select id="findByIdMap" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from _jobitem
where createUserName=#{username,jdbcType=VARCHAR}
and appId in
<foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
#{item}
</foreach>
</select>
测试代码:
@Test
public void testFindByIdMap() {
Map<String, Object> creatorAndappIds = new HashMap<String, Object>();
creatorAndappIds.put("username", "admin");
String[] appIds = new String[] { "application_1548381669007_0057", "application_1548381669007_0056",
"application_1548381669007_0055" };
creatorAndappIds.put("ids", appIds); List<Jobitem> result = sJobitemMapper.findByIdMap(creatorAndappIds); Assert.assertEquals(3, result.size());
} @Test
public void testFindByIdMap2() {
Map<String, Object> creatorAndappIds = new HashMap<String, Object>();
creatorAndappIds.put("username", "admin");
List<String> appIds = new ArrayList<String>();
appIds.add("application_1548381669007_0057");
appIds.add("application_1548381669007_0056");
appIds.add("application_1548381669007_0055");
creatorAndappIds.put("ids", appIds); List<Jobitem> result = sJobitemMapper.findByIdMap(creatorAndappIds); Assert.assertEquals(3, result.size());
}
使用string...对sql in进行传参时
JobitemMapper接口类:
List<Jobitem> findByIdMutilParams(String... appIds);
对应JobitemMapper.xml文件配置:
<select id="findByIdMutilParams" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from _jobitem
where appId in
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
#{item}
</foreach>
</select>
测试代码:
@Test
public void testFindByIdMultiParams() {
List<Jobitem> result = sJobitemMapper.findByIdMutilParams("application_1548381669007_0057",
"application_1548381669007_0056", "application_1548381669007_0055"); Assert.assertEquals(3, result.size());
}
MyBatis(四):mybatis中使用in查询时的注意事项的更多相关文章
- 在form子句中使用子查询时的注意事项
今天中午为了弄清这个问题,本人真的是头都搞大了!最后明白了一点,在from子句中使用子查询是,一定要将临时表的别名带上,否则会灰常痛苦!!!
- mybatis中使用in查询时的注意事项
1. 当查询的参数只有一个时 findByIds(List<Long> ids) 1.a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list < ...
- oracle中使用sql查询时字段为空则赋值默认
转至:http://www.th7.cn/db/Oracle/201501/86125.shtml oracle 通过 nvl( )函数sql 查询时为 空值 赋默认值 oracle 函数介绍之nvl ...
- mybatis sql in 查询(mybatis sql语句传入参数是list)mybatis中使用in查询时in怎么接收值
1.in查询条件是list时 <select id="getMultiMomentsCommentsCounts" resultType="int"> ...
- mybatis中使用where in查询时的注意事项
我使用的时候collection值为mapper的参数名如:int deleteRoleByUserIds(@Param("userIds") String[] userIds); ...
- mybatis xml 文件中like模糊查询
1.直接传参法 直接传参法,就是将要查询的关键字keyword,在代码中拼接好要查询的格式,如%keyword%,然后直接作为参数传入mapper.xml的映射文件中. 2.CONCAT()函数 My ...
- MYSQL中的SELECT查询时进行运算
SELECT在mysql中是查询表中的数据的作用,但也可以在查询的时候直接进行运算,然后返回查询后的结果 比如 )) FROM username2 其中的IFNULL函数是对adven数据进行判断,若 ...
- SQL中join连接查询时条件放在on后与where后的区别
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时表时使用的条 ...
- 关于在vs中添加生成命令时的注意事项
涉及到目录最好用双引号括起来,防止在目录含有空格或文字时发生错误.例如 del "$(SolutionDir)\..\xxxxxx\xxxx\Build\*.*" /s /q xc ...
随机推荐
- window下用taskkill杀死进程
TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid | /IM imagename] } ...
- UOJ#30/Codeforces 487E Tourists 点双连通分量,Tarjan,圆方树,树链剖分,线段树
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ30.html 题目传送门 - UOJ#30 题意 uoj写的很简洁.清晰,这里就不抄一遍了. 题解 首先建 ...
- BZOJ2209 [Jsoi2011]括号序列 splay
原文链接http://www.cnblogs.com/zhouzhendong/p/8093556.html 题目传送门 - BZOJ2209 题解 我太弱了,调出这题感觉都要吐了. 题解懒得写了. ...
- weka的基本使用
目录: 1. 简介 2.界面初识 3.数据格式 4.数据准备 5.关联规则 6.分类与回归 7.聚类分析 8.Weka相关资料 9.Weka二次开发 10.Weka源代码导入 1. 简介 WEKA的全 ...
- 一、利用Python编写飞机大战游戏-面向对象设计思想
相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...
- HDU 1540 Tunnel Warfare(经典)(区间合并)【线段树】
<题目链接> 题目大意: 一个长度为n的线段,下面m个操作 D x 表示将单元x毁掉 R 表示修复最后毁坏的那个单元 Q x 询问这个单元以及它周围有多少个连续的单元,如果它本身已经被 ...
- 从零搭建 ES 搜索服务(三)同义词搜索
一.前言 上篇介绍了 ES 的基础搜索,能满足我们基本的需求,然而在实际使用中还可能希望搜索「番茄」能将包含「西红柿」的结果也罗列出来,本篇将介绍如何实现同义词之间的搜索. 二.安装 ES 同义词插件 ...
- react+typescript报错集锦<持续更新>
typescript报错集锦 错误:Import sources within a group must be alphabetized.tslint(ordered-imports) 原因:impo ...
- 调整和删除Win7休眠文件Hiberfil.sys释放C盘
Hiberfil.sys 是 Windows 休眠功能(Windows Hibernation)将内存数据与会话保存至硬盘.以便计算机断电重新启动后可以快速恢复会话所需的内存镜像文件.在早期版本的 W ...
- linux学习笔记 4建立用户
一般用法 #useradd mysql 含义 创建 mysql用户 特殊用法 1> #useradd -d /usr/cjh -m cjh 含义:创建cjh用户 产生一个主目录 /usr/cj ...