开发工具: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

最新文章

  1. 图文介绍如何在Eclipse统计代码行数(转)
  2. Thrift简单实践
  3. CSS中对图片(background)的一些设置心得总结
  4. javascript 基础篇
  5. span和div的区别
  6. 《JavaScript高级程序设计》笔记(2):位操作符
  7. 【Xamarin挖墙脚系列:Xamarin.Android的API设计准则】
  8. 《鸟哥的Linux私房菜》读书笔记二
  9. JavaScript onConflict 处理
  10. JQuery简单实现图片轮播效果
  11. (转)DevExpress GridView属性设置
  12. Java 二维码生成工具类
  13. hdu 1528 Card Game Cheater ( 二分图匹配 )
  14. java学习笔记13--比较器(Comparable、Comparator)
  15. java 的equals 与== ,null与isempty的区别
  16. python之路--day13-模块
  17. GMT与Etc/GMT地区信息的时区转换
  18. PYTHON之路,线程
  19. 《http权威指南》读书笔记10
  20. html5shiv.min.js

热门文章

  1. (转)python 列表与元组的操作简介
  2. DEDE利用Ajax实现调用当前登录会员的信息简要说明
  3. POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
  4. Redis-Service.Stack的初级使用
  5. maven课程 项目管理利器-maven 3-7 maven依赖范围 2星
  6. MyEclipse快捷键大全,很实用
  7. css3 animatehue属性
  8. ssh-agent &amp;&amp; ssh-agent forward &amp;&amp; SSH ProxyCommand
  9. git 因线上分支名重复导致无法拉取代码
  10. May 7th 2017 Week 19th Sunday