开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/7892801d804d2060774f3720f82e776ff318e3ba

前言:

在调用mybatis的查询条件时,之前,遇到需要验证多个参数的查询时,往往需要把所有参数都绑定到一个实体中去,然后调用获取。

现在,我们来详细描述mybatis传递参数的细节。


一、单个参数:

1.定义mapper接口:

 package com.xm.mapper;

 import java.util.List;

 import com.xm.pojo.Student;

 public interface StudentMapper {

     /**
* 根据name查询
* @param name
* @return
*/
public Student getByName(String name); }

StudentMapper.java

2.实现mapper映射:

 <?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.xm.mapper.StudentMapper"> <!-- 根据name查询 -->
<select id="getByName" resultType="student">
select * from student where name=#{name}
</select>
</mapper>

StudentMapper.xml

3.定义测试用例:

 package com.xm;

 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.SpringRunner; import com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RunWith(SpringRunner.class)
@SpringBootTest
public class StudentTest {
@Autowired
private StudentMapper studentMapper; @Test
public void selectStudent() { Student student = studentMapper.getByName("郭小明");
System.out.println(student); } }

StudentTest.java

4.测试结果:

(1)数据库查询结果:

(2)测试结果输出:

注意在mapper映射中,单个参数类型,也可以不写 parameterType="",参数名可以随意写,比如#{names}

实现mapper映射:

 <?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.xm.mapper.StudentMapper"> <!-- 根据name查询 -->
<select id="getByName" resultType="student">
<!-- select * from student where name=#{name} -->
select * from student where name=#{names}
</select>
</mapper>

StudentMapper.xml

二、多个参数

1.根据参数名查询

(1)定义mapper接口:

 package com.xm.mapper;

 import java.util.List;

 import com.xm.pojo.Student;

 public interface StudentMapper {

     /**
* 根据用户名和id同时查询
* @param id
* @param name
* @return
*/
public Student getStudentByIdAndName(Integer id,String name); }

StudentMapper.java

(2)实现mapper映射:

 <?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.xm.mapper.StudentMapper"> <!-- 根据用户名和id同时查询 -->
<select id="getStudentByIdAndName" resultType="student">
select * from student where name=#{name} and id=#{id}
</select>
</mapper>

StudentMapper.xml

(3)定义测试用例:

 package com.xm;

 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.SpringRunner; import com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RunWith(SpringRunner.class)
@SpringBootTest
public class StudentTest {
@Autowired
private StudentMapper studentMapper; @Test
public void selectStudent() { /*Student student = studentMapper.getByName("郭小明");*/
Student student = studentMapper.getStudentByIdAndName(1, "郭小明");
System.out.println(student); } }

StudentTest.java

(4)测试结果:

说明:这种简单的根据参数名传参是失败的

2.根据参数key值获取,获取规则为param1,param2,param3.........:

(1)实现mapper映射:

 <?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.xm.mapper.StudentMapper"> <!-- 根据用户名和id同时查询 -->
<select id="getStudentByIdAndName" resultType="student">
<!-- select * from student where name=#{name} and id=#{id} -->
select * from student where name=#{param2} and id=#{param1}
</select>
</mapper>

StudentMapper.xml

(2)测试结果:

说明:针对这种情况,如果参数较多的情况下,获取准确的参数名更好一些

3.绑定参数名

(1)定义mapper接口:

 package com.xm.mapper;

 import java.util.List;

 import org.apache.ibatis.annotations.Param;

 import com.xm.pojo.Student;

 public interface StudentMapper {

     /**
* 根据用户名和id同时查询
* @param id
* @param name
* @return
*/
public Student getStudentByIdAndName(@Param("id")Integer id,@Param("name")String name); }

StudentMapper.java

(2)实现mapper映射:

 <?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.xm.mapper.StudentMapper"> <!-- 根据用户名和id同时查询 -->
<select id="getStudentByIdAndName" resultType="student">
select * from student where name=#{name} and id=#{id}
<!-- select * from student where name=#{param2} and id=#{param1} -->
</select>
</mapper>

StudentMapper.xml

(3)测试结果:

说明:针对参数较多,且参数都属于同一个pojo类的时候,可以把参数先封装入实体,再获取

4.封装实体参数:

(1)定义mapper接口:

 package com.xm.mapper;

 import java.util.List;

 import org.apache.ibatis.annotations.Param;

 import com.xm.pojo.Student;

 public interface StudentMapper {

     /**
* 根据用户名和id同时查询
* @param id
* @param name
* @return
*/
public Student getStudentByIdAndName(Student student); }

StudentMapper.java

(2)定义测试用例:

 package com.xm;

 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.SpringRunner; import com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RunWith(SpringRunner.class)
@SpringBootTest
public class StudentTest {
@Autowired
private StudentMapper studentMapper; @Test
public void selectStudent() { /*Student student = studentMapper.getByName("郭小明");*/
/*Student student = studentMapper.getStudentByIdAndName(1, "郭小明");*/
Student student = new Student();
student.setName("郭小明");
student.setId(1);
Student student2 = studentMapper.getStudentByIdAndName(student);
System.out.println(student2); } }

StudentMapper.java

(3)测试结果:


                                                                                2018-07-02

6、SpringBoot+Mybatis整合------参数传递的更多相关文章

  1. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  2. SpringBoot+Mybatis整合入门(一)

    SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...

  3. springboot/Mybatis整合

    正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper ...

  4. SpringBoot+Mybatis整合实例

    前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...

  5. 2、SpringBoot+Mybatis整合------一对一

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/93398da60c647573645917b2 ...

  6. springboot+mybatis整合(单元测试,异常处理,日志管理,AOP)

    我用的事IDEA,jdk版本是1.7.新建项目的时候这个地方的选择需要注意一下,springboot版本是1.5的,否则不支持1.7的jdk pom.xml <dependency> &l ...

  7. 9、SpringBoot+Mybatis整合------动态sql

    开发工具:STS 前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 动态sql中的语法: where标签 if标签 trim标签 set标签 s ...

  8. 1、SpringBoot+Mybatis整合------简单CRUD的实现

    编译工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9 ...

  9. springboot mybatis 整合

    新建项目在上一篇. 第二步:创建表和相应的实体类 实体类:user.java package com.qtt.im.entity; import java.io.Serializable; publi ...

随机推荐

  1. SETI ACdream - 1430 后缀自动机求不相交子串

    http://blog.csdn.net/gatevin/article/details/45875343 题目是求不重叠的不同子串个数 一般来说, endpos集合包含了子串结尾位置,结尾在&quo ...

  2. [转]js 判断js函数、变量是否存在

    本文转自:http://blog.csdn.net/liang4571231/article/details/4042519 在进行js编程时,总会出现可能一些函数或者变量未定义而被引用,导致报错的情 ...

  3. [转]使用 YCombo 做 JS /CSS开发 合并 压缩

    本文转自:http://www.neoease.com/minimize-javascript-files-using-ycombo/ 前文已介绍过 YCombo 及相关的 CSS 和 JS 合并工具 ...

  4. MATLAB线性回归方程与非线性回归方程的相关计算

    每次比赛都需要查一下,这次直接总结到自己的博客中. 以这个为例子: 2.线性方程的相关计算 x=[1,2,3,4,5]';%参数矩阵 X=[ones(5,1),x];%产生一个5行一列的矩阵,后接x矩 ...

  5. pat1002. A+B for Polynomials (25)

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  6. 在DataColumn.Expression把DateTime转换成String的问题

    我在使用MySql5.1的数据库中,使用winForm的DataGridView要把数据库中全称DateTime格式,转换成Date格式,就是把日期时间转换成日期,不要时间.如‘2013-07-08 ...

  7. XHTML教会我的一些东西-2

    不知道写些什么,每次看完视频之后都按照里面的方法把相应的代码写了一遍.感觉问题不大,可能是我没去写那些复杂的代码,没有去思考其他一些新颖的架构. 在自己写代码和看视频的过程中,我明白了,一定要注意每一 ...

  8. 从零开始的全栈工程师——js篇2.19(BOM)

    一.BOM 浏览器对象模型 1.window.open(url,ways) url 是打开的网页地址ways 打开的方式 _self 2.window.close() 关闭当前页面 3.window. ...

  9. BZOJ4709: [Jsoi2011]柠檬(决策单调性)

    题意 题目链接 Sol 结论:每次选择的区间一定满足首位元素相同.. 仔细想想其实挺显然的,如果不相同可以删掉多着的元素,对答案的贡献是相同的 那么设\(f[i]\)表示到第\(i\)个位置的最大价值 ...

  10. codevs 1213 解的个数

    1213 解的个数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 已知整数x,y满足如下面的条件: ax+by+c = ...