开发工具: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. ubuntu下安装wireshark(以及配置非root)

    https://jingyan.baidu.com/article/c74d60009d992f0f6a595de6.html Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网 ...

  2. window对象的事件:onresize、onpageshow、onload

    onresize事件非常容易理解: 即当窗口或者框架的大小发生变化时,就会触发此事件. 实例demo onpageshow事件是当用户浏览网页时触发的. onpageshow 事件类似于 onload ...

  3. c3p0 数据连接池 流行开源

    注意事项:配置文件规定命名,不能更改   c3p0-config <?xml version="1.0" encoding="UTF-8"?>< ...

  4. git如何使用

    Git是分布式的,但多数时候仍然要使用中央仓库作为所有开发者的交互中心,和svn一样,开发人员仍要在本地写代码并提交到中央服务器.Git相较于svn最大的优势就在于其强大的分支系统,而git的工作流程 ...

  5. C# WinForm拖入文件到窗体,得到文件路径

    private void textBox1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataForma ...

  6. get post put delete

    get:获取资源 幂等 post:执行不安全和非幂等操作(幂等:多次请求于一次请求的效果一样) put:更新资源  幂等 delete:删除资源  幂等 如何理解幂等: public book { p ...

  7. yii2.0安装ElasticSearch及使用

    yii2.0安装ElasticSearch安装及使用教程:https://www.yiichina.com/tutorial/1046 Elasticsearch 权威指南(中文版):https:// ...

  8. vsftpd配置

    yum -y install vsftpd useradd upload -s /sbin/nologin passwd upload mkdir /data/upload chown -R upol ...

  9. HCNA配置RIPv1

    1.拓扑图 2.配置 R1 The device is running! ###### <Huawei>sys Enter system view, return user view wi ...

  10. JavaScript 正则表单验证(用户名、密码、确认密码、手机号、座机号、身份证号)

    1.关于JavaScript表单验证,如果使用双向绑定的前端js框架,会更容易的多.但是博主还是建议大家不要脱离源生js本身.因为源生js才是王道. 注意: a.代码中的错误提示可能会没有,在代码中找 ...