Mybatis中的Mapper.xml映射文件sql查询接收多个参数
我们都知道,在Mybatis中的Mapper.xml映射文件可以定制动态SQL,在dao层定义的接口中定义的参数传到xml文件中之后,在查询之前mybatis会对其进行动态解析,通常使用#{}接收,下面介绍几种比较常用的用法。
接收多个参数
遇到这个问题是在昨天实现分页的时候接收参数时遇到了错误,最后用第一种方法解决,但是怀着路漫漫其修远兮的态度,吾必将上下而求索其他用法,所以在查询了网上很多教程和mybatis官方文档,总结出以下四种常用用法;
具体分页参考:
手动实现分页 或
手动分页
- 使用arg0,arg1或使用param1,param2接收(arg索引从0开始,param索引从1开始);
- 多个参数封装成对象;
- 多个参数封装成Map集合;
- 使用@param绑定参数;
下面分别介绍
public interface CustomerDao {
List<Customer> findAllPageSql(Integer pageNo,Integer pageSize);
}
第一种方法:使用arg或param
<mapper namespace="com.vue.dao.CustomerDao">
<select id="findAllPageSql" parameterType="java.lang.Integer" resultType="com.vue.entity.Customer">
SELECT * FROM t_customer LIMIT #{param1},#{param2}
</select>
</mapper>
或
<mapper namespace="com.vue.dao.CustomerDao">
<select id="findAllPageSql" parameterType="java.lang.Integer" resultType="com.vue.entity.Customer">
SELECT * FROM t_customer LIMIT #{arg0},#{arg1}
</select>
</mapper>
第二种方法:封装成对象
Page.java
public class Page {
private Integer pageNo;
private Integer pageSize;
//getter,setter略
}
传参:
控制层封装前台传来的page信息
@ResponseBody
@GetMapping("/allClass")
public String findAllPageClass(@RequestParam(required = true,defaultValue = "1") Integer pageNo,
@RequestParam(required = false,defaultValue = "5") Integer pageSize) {
Page page = new Page();
page.setPageNo(pageNo-1);
page.setPageSize(pageSize);
return customerService.findAllPageClass(page);
}
service层:
public interface CustomerService{
String findAllPageClass(Page page);
}
持久层传参:
public interface CustomerDao {
List<Customer> findAllPageClass(Page page);
}
mapper.xml映射文件
<mapper namespace="com.vue.dao.CustomerDao">
<select id="findAllPageClass" resultType="com.vue.entity.Customer" parameterType="com.vue.util.Page">
SELECT * FROM t_customer LIMIT #{pageNo},#{pageSize}
</select>
</mapper>
第三种方法:封装成Map集合
传参:
控制层封装前台传来的page信息
@ResponseBody
@GetMapping("/allMap")
public String findAllPageMap(@RequestParam(required = true,defaultValue = "1") Integer pageNo,
@RequestParam(required = false,defaultValue = "5") Integer pageSize) {
Map<String,Integer> pageMap = new HashMap<>();
pageMap.put("pageNo",pageNo-1);
pageMap.put("pageSize",pageSize);
return customerService.findAllPageMap(pageMap);
}
service层:
public interface CustomerService{
String findAllPageMap(Map<String, Integer> pageMap);
}
持久层传参:
public interface CustomerDao {
List<Customer> findAllPageMap(Map<String, Integer> pageMap);
}
mapper.xml映射文件
<mapper namespace="com.vue.dao.CustomerDao">
<select id="findAllPageMap" parameterType="java.util.Map" resultType="com.vue.entity.Customer">
SELECT * FROM t_customer LIMIT #{pageNo},#{pageSize}
</select>
</mapper>
第四种方法:使用@Param绑定参数
public interface CustomerDao {
List<Customer> findAllPageSql(
@Param(value = "pageNo") Integer pageNo,
@Param(value = "pageSize") Integer pageSize);
}
mapper.xml映射文件
<mapper namespace="com.vue.dao.CustomerDao">
<select id="findAllPageSql" parameterType="java.lang.Integer" resultType="com.vue.entity.Customer">
SELECT * FROM t_customer LIMIT #{pageNo},#{pageSize}
</select>
</mapper>
补充 :#{}与${}区别
#{} 在SQL动态解析之后,编译将#{}替换为占位符?去替换参数,可以防止sql注入;
#{} 只是表示占位,与参数的名字无关,如果只有一个参数会自动对应,下面会介绍多个参数的问题;
${} 是进行字符串拼接,直接取出参数值,放到sql语句中;
使用注意点,当表名作为变量时,必须使用${}获取参数作为表名进行查询,否则会出现语法错误;
总结:
- sql语句动态生成的时候,使用${}
- sql语句中某个参数进行占位的时候用#{}
Mybatis中的Mapper.xml映射文件sql查询接收多个参数的更多相关文章
- Mybatis学习--Mapper.xml映射文件
简介 Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 映射文件中有很多属性,常用的就是parameterType(输入类型 ...
- Mapper.xml映射文件
查询订单关联查询用户: 使用resultType,ordersCustom可以通过继承orders获得其属性,再添加我们需要的用户字段. 使用resultMap,orders表中通过封装user对象来 ...
- MyBatis学习存档(3)——mapper.xml映射文件
MyBatis 真正的强大在于映射语句,专注于SQL,功能强大,SQL映射的配置却是相当简单 所以我们来看看映射文件的具体结构 一.xml节点结构 mapper为根节点 - namespace命名空间 ...
- mybatis 使用注解简化xml映射文件
目录 关于mybatis注解 初次简单使用mybatis注解示例 利用注解实现指定映射 使用注解实现表间关联(1对1) 关于mybatis注解 注解在java中特别常见,mybatis中也支持注解. ...
- 解决 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 以及MyBatis批量加载xml映射文件的方式
错误 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 的出现,意味着项目需要xml文件来 ...
- MyBatis - 3.Mapper XML映射文件
SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序): cache – 给定命名空间的缓存配置. cache-ref – 其他命名空间缓存配置的引用. resultMap – 是最复杂也是 ...
- mybatis Mapper XML 映射文件
传送门:mybatis官方文档 Mapper XML 文件详解 一. 数据查询语句 1. select <select id="selectPerson" parameter ...
- 在mapper.xml映射文件中添加中文注释报错
问题描述: 在写mapper.xml文件时,想给操作数据库语句添加一些中文注释,添加后运行报如下错误: 思考 可能是写了中文注释,编译器在解析xml文件时,未能成功转码,从而导致乱码.但是文件开头也采 ...
- mybatis自动生成mapper,dao映射文件
利用Mybatis-Generator来帮我们自动生成mapper.xml文件,dao文件,model文件. 1.所需文件 关于Mybatis-Generator的下载可以到这个地址:https:// ...
随机推荐
- Java学习笔记 DbUtils数据库查询和log4j日志输出 使用
DbUtils使用 QueryRunner DbUtils中定义了一个数据库操作类QueryRunner,所有的数据库操作CRUD都是通过此类来完成. 此类是线程安全的 方法名 对应sql语句 exc ...
- Web前端基础(4):CSS(一)
1. CSS介绍 现在的互联网前端分三层: HTML:超文本标记语言.从语义的角度描述页面结构. CSS:层叠样式表.从审美的角度负责页面样式. JS:JavaScript .从交互的角度描述页面行为 ...
- python基础—条件语句
一.Python基础 1.第一句python print('hello,world') Q: 后缀名可以任意? A: 导入模块时,如果不是.py后缀,会出错. 2.两种执行的方式: -python解 ...
- 程序运行时间测试 - 使用libc 中 clock 函数
我们运行程序的时候,可以简单使用clock函数测试程序的运行时间:(本示例中以微秒为单位输出) https://github.com/yaowenxu/Workplace/blob/master/ti ...
- Linux—网络防火墙详解
一.防火墙基本知识 二.firewall防火墙 # 安装firewalld [root@localhost ~]# apt-get install firewalld [root@localhost ...
- [Go] 使用go mod安装beego
需要go升级到1.12或以上 mkdir gomodtest cd gomodtest go mod init gomodtest 创建一个server.go package main import ...
- CSP认证201812
201812-1 #include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f #define ll long l ...
- Java面试题-基础篇二(干货)
11.是否可以从一个static方法内部发出对非static方法的调用? 不可以.因为非static方法是要与对象关联在一起的,必须创建一个对象后,才可以在该对象上进行方法调用,而static方法调用 ...
- 数据分析三剑客 numpy,oandas,matplotlib
数据分析: 是不把隐藏在看似杂乱无章的数据域背后的信息提炼出来,总结出所研究对象内在规律 NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩 ...
- 4.web基础$_POST