6、SpringBoot+Mybatis整合------参数传递
开发工具:STS
代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/7892801d804d2060774f3720f82e776ff318e3ba
前言:
在调用mybatis的查询条件时,之前,遇到需要验证多个参数的查询时,往往需要把所有参数都绑定到一个实体中去,然后调用获取。
现在,我们来详细描述mybatis传递参数的细节。
一、单个参数:
1.定义mapper接口:
1 package com.xm.mapper; 2 3 import java.util.List; 4 5 import com.xm.pojo.Student; 6 7 public interface StudentMapper { 8 9 10 /** 11 * 根据name查询 12 * @param name 13 * @return 14 */ 15 public Student getByName(String name); 16 17 }
StudentMapper.java
2.实现mapper映射:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xm.mapper.StudentMapper"> 4 5 6 7 <!-- 根据name查询 --> 8 <select id="getByName" resultType="student"> 9 select * from student where name=#{name} 10 </select> 11 </mapper>
StudentMapper.xml
3.定义测试用例:
1 package com.xm; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.boot.test.context.SpringBootTest; 7 import org.springframework.test.context.junit4.SpringRunner; 8 9 import com.xm.mapper.StudentMapper; 10 import com.xm.pojo.Student; 11 12 @RunWith(SpringRunner.class) 13 @SpringBootTest 14 public class StudentTest { 15 @Autowired 16 private StudentMapper studentMapper; 17 18 @Test 19 public void selectStudent() { 20 21 Student student = studentMapper.getByName("郭小明"); 22 System.out.println(student); 23 24 } 25 26 27 }
StudentTest.java
4.测试结果:
(1)数据库查询结果:
(2)测试结果输出:
注意在mapper映射中,单个参数类型,也可以不写 parameterType="",参数名可以随意写,比如#{names}
实现mapper映射:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xm.mapper.StudentMapper"> 4 5 6 7 <!-- 根据name查询 --> 8 <select id="getByName" resultType="student"> 9 <!-- select * from student where name=#{name} --> 10 select * from student where name=#{names} 11 </select> 12 </mapper>
StudentMapper.xml
二、多个参数
1.根据参数名查询
(1)定义mapper接口:
1 package com.xm.mapper; 2 3 import java.util.List; 4 5 import com.xm.pojo.Student; 6 7 public interface StudentMapper { 8 9 10 /** 11 * 根据用户名和id同时查询 12 * @param id 13 * @param name 14 * @return 15 */ 16 public Student getStudentByIdAndName(Integer id,String name); 17 18 }
StudentMapper.java
(2)实现mapper映射:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xm.mapper.StudentMapper"> 4 5 6 <!-- 根据用户名和id同时查询 --> 7 <select id="getStudentByIdAndName" resultType="student"> 8 select * from student where name=#{name} and id=#{id} 9 </select> 10 </mapper>
StudentMapper.xml
(3)定义测试用例:
1 package com.xm; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.boot.test.context.SpringBootTest; 7 import org.springframework.test.context.junit4.SpringRunner; 8 9 import com.xm.mapper.StudentMapper; 10 import com.xm.pojo.Student; 11 12 @RunWith(SpringRunner.class) 13 @SpringBootTest 14 public class StudentTest { 15 @Autowired 16 private StudentMapper studentMapper; 17 18 19 @Test 20 public void selectStudent() { 21 22 /*Student student = studentMapper.getByName("郭小明");*/ 23 Student student = studentMapper.getStudentByIdAndName(1, "郭小明"); 24 System.out.println(student); 25 26 } 27 28 29 }
StudentTest.java
(4)测试结果:
说明:这种简单的根据参数名传参是失败的
2.根据参数key值获取,获取规则为param1,param2,param3.........:
(1)实现mapper映射:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xm.mapper.StudentMapper"> 4 5 6 <!-- 根据用户名和id同时查询 --> 7 <select id="getStudentByIdAndName" resultType="student"> 8 <!-- select * from student where name=#{name} and id=#{id} --> 9 select * from student where name=#{param2} and id=#{param1} 10 </select> 11 </mapper>
StudentMapper.xml
(2)测试结果:
说明:针对这种情况,如果参数较多的情况下,获取准确的参数名更好一些
3.绑定参数名
(1)定义mapper接口:
1 package com.xm.mapper; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Param; 6 7 import com.xm.pojo.Student; 8 9 public interface StudentMapper { 10 11 12 13 /** 14 * 根据用户名和id同时查询 15 * @param id 16 * @param name 17 * @return 18 */ 19 public Student getStudentByIdAndName(@Param("id")Integer id,@Param("name")String name); 20 21 }
StudentMapper.java
(2)实现mapper映射:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xm.mapper.StudentMapper"> 4 5 6 <!-- 根据用户名和id同时查询 --> 7 <select id="getStudentByIdAndName" resultType="student"> 8 select * from student where name=#{name} and id=#{id} 9 <!-- select * from student where name=#{param2} and id=#{param1} --> 10 </select> 11 </mapper>
StudentMapper.xml
(3)测试结果:
说明:针对参数较多,且参数都属于同一个pojo类的时候,可以把参数先封装入实体,再获取
4.封装实体参数:
(1)定义mapper接口:
1 package com.xm.mapper; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Param; 6 7 import com.xm.pojo.Student; 8 9 public interface StudentMapper { 10 11 12 13 /** 14 * 根据用户名和id同时查询 15 * @param id 16 * @param name 17 * @return 18 */ 19 public Student getStudentByIdAndName(Student student); 20 21 }
StudentMapper.java
(2)定义测试用例:
1 package com.xm; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.boot.test.context.SpringBootTest; 7 import org.springframework.test.context.junit4.SpringRunner; 8 9 import com.xm.mapper.StudentMapper; 10 import com.xm.pojo.Student; 11 12 @RunWith(SpringRunner.class) 13 @SpringBootTest 14 public class StudentTest { 15 @Autowired 16 private StudentMapper studentMapper; 17 18 @Test 19 public void selectStudent() { 20 21 /*Student student = studentMapper.getByName("郭小明");*/ 22 /*Student student = studentMapper.getStudentByIdAndName(1, "郭小明");*/ 23 Student student = new Student(); 24 student.setName("郭小明"); 25 student.setId(1); 26 Student student2 = studentMapper.getStudentByIdAndName(student); 27 System.out.println(student2); 28 29 } 30 31 32 }
StudentMapper.java
(3)测试结果:
2018-07-02
转载于:https://www.cnblogs.com/TimerHotel/p/springboot_matatis_06.html
6、SpringBoot+Mybatis整合------参数传递相关推荐
- Springboot + Mybatis整合的小demo,火车订票系统
Springboot +Mybatis 的一个订票系统 这学期开了一门软件测试课程,需要做一个系统用于软件测试的学习,就使用目前JavaEE开发中比较火的SpringBoot + Mybatis做了一 ...
- Springboot Mybatis 整合(完整版)
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboo ...
- springboot+mybatis整合shiro——登录认证和权限控制
引依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring是与spring整合.shiro-ehcache是与EHCac ...
- 8、SpringBoot+Mybatis整合------参数取值方式
前言: 我们知道,在mybatis中,参数取值方式有两种: #{ } 和 ${ } 下面,我们来探讨下#{ }与${ }不同. 一.#{ } 例: select * from student wher ...
- springboot mybatis整合
1:build.gradle 添加依赖包 compile group: 'org.postgresql', name: 'postgresql', version: '42.2.4'compile g ...
- redis05_springboot+mybatis整合redis cluster
一.springboot+mybatis整合redis cluster整合流程图 二.springboot+mybatis整合redis cluster具体实现 2.1 创建springboot+ ...
- SpringBoot+Mybatis(01)
你强任你强,东皇加张良 前边我们讲过Mybatis入门,以及一些特殊的知识点. 但是现在我们用Mybatis没有单独用的了,基本上都是结合SpringBoot来使用. 这篇就写一下SpringBoot ...
- 玩转springboot:整合mybatis实例
这篇文章讲解一下springboot整合mybatis,其实,springboot整合mybatis和springmvc整合mybatis并没有什么太大的区别,大体上还是差不多哦,只是比springm ...
- Eclipse中实现SpringBoot与Mybatis整合(图文教程带源码)
场景 数据库中数据 实现效果 项目结构 前面参照 Eclipse中新建SpringBoot项目并输出HelloWorld https://blog.csdn.net/BADAO_LIUMANG_QIZ ...
最新文章
- Python学习日志9月14日
- Drools 5.1.1(一)
- MFC六大核心机制之二:运行时类型识别(RTTI)
- 【深度学习系列】基础知识、模型学习
- wince 6.0 pb
- WPF基础学习笔记(一)Dependency Object 和 Dependency Property
- 用anaconda保证64位和32位的python共存
- Jenkins清空当前Clean Workspace
- 苹果手机解压缩软件_「 神器 」用得贼舒服的压缩/解压缩工具
- 游戏网页制作 仿英雄联盟网页设计作业 HTML CSS游戏官网网页模板 大学生游戏介绍网站毕业设计 DW游戏主题网页模板下载 游戏娱乐网页成品代码 英雄联盟网页作品下载
- 8421码到5421码的转换_8421BD码转换成5421BCD码.doc
- python在医学中的应用_如何应用Python处理医学影像学中的DICOM信息
- 子系统kali安装桌面
- MySQL_12_ShardingJDBC实现读写分离与分布式事务
- 如何打造一份优秀的技术简历
- 传奇私服服务器怎么增加npc,在自己的传奇服务器中如何添加NPC
- 实施微前端的六种方式
- 交换机组播风暴_用思科交换机杜绝局域网广播风暴
- 扫二维码付款后看图片
- 如何充分发挥 Scrapy 的异步能力【转载】
热门文章
- iOS 隐藏下级页面的tabbar的统一处理
- java邮件附件默认路径_JavaMail - 文件夹管理( Folder Management)
- 含有多个java程序的文件夹导入MyEclipes 出现错误的解决办法
- HTC打算一条道走到黑,开始资助「脑后插管操作」
- 逆向工程 sql_mybatis逆向生成工具,真的很好用!
- 英特尔:80%的边缘数据都是视频数据!新成立物联网视频事业部,总部base中国...
- 用命令行管理你的GitHub项目,不必再开网页,官方CLI工具1.0版今日上线
- 全球首个AI驾校教练+驾照考官已上岗,装手机里就能用,再也不怕挨教练骂了...
- Ubuntu使用小技巧
- java extend 和 implements 的区别