Mybatis传递多个参数进行SQL查询的用法
当只向xxxMapper.xml文件中传递一个参数时,可以简单的用“_parameter”来接收xxxMapper.java传递进来的参数,并代入查询。
但是,如果在xxxMapper.java文件中传递进来多个参数,就不能使用上面这种形式来接收参数,这时可以有两种方案来解决这个问题:
一 向xml文件中传递进去一个Map<String, Object>集合,然后xml文件中就可以正常使用Map集合中的各个参数了。
具体实例如下:
(1)xxxMapper.java文件中这样定义:
List<Airline> findAll(Map<String, Object> parms);
(2)在用到上面定义的具体实现类中给Map传参:
public List<Airline> findAll(PageInfo page,Airline airline) {
HashMap<String,Object> params = new HashMap<String,Object>();
params.put("page", page);
params.put("airline", airline);
return airlineMapper.findAll(params);
}
(3)此时对应的xxxMapper.xml文件使用“java.util.Map”来接收这个Map集合:
<sql id="sqlfileders">
<bind name="fileders"
value="#{'id':'ID','departureAirport':'DEPARTURE_AIRPORT','relDepartureAirport':'REL_DEPARTURE_AIRPORT','arrivalAirport':'ARRIVAL_AIRPORT','relArrivalAirport':'REL_ARRIVAL_AIRPORT','popStatus':'POP_STATUS','status':'STATUS','creator':'CREATOR','createTime':'CREATE_TIME'}" />
<bind name="javapropertys"
value="#{'ID':'id','DEPARTURE_AIRPORT':'departureAirport','REL_DEPARTURE_AIRPORT':'relDepartureAirport','ARRIVAL_AIRPORT':'arrivalAirport','REL_ARRIVAL_AIRPORT':'relArrivalAirport','POP_STATUS':'popStatus','STATUS':'status','CREATOR':'creator','CREATE_TIME':'createTime'}" />
</sql>
<select id="findAll" resultMap="BaseResultMap" parameterType="java.util.Map">
<![CDATA[
select x.* from (
select z.*, rownum numbers from (
]]>
select
<include refid="Base_Column_List" />
from
USR_AIR_LINE
<where>
<if test="airline.departureAirport != null">
DEPARTURE_AIRPORT = #{airline.departureAirport}
</if>
<if test="airline.arrivalAirport != null">
and ARRIVAL_AIRPORT=#{airline.arrivalAirport}
</if>
<if test="airline.relDepartureAirport != null">
and REL_DEPARTURE_AIRPORT =
#{airline.relDepartureAirport}
</if>
<if test="airline.relArrivalAirport != null">
and REL_ARRIVAL_AIRPORT = #{airline.relArrivalAirport}
</if>
<if test="airline.popStatus != null">
and POP_STATUS = #{airline.popStatus}
</if>
<if test="airline.status != null">
and STATUS = #{airline.status}
</if>
</where>
<if test="page.sortName != null">
<include refid="sqlfileders" />
<bind name="orderfield" value="#this.fileders[page.sortName]" />
order by ${orderfield} ${page.sortOrder}
</if>
<![CDATA[ ) z where rownum < ]]>
#{page.to}
<![CDATA[ ) x where x.numbers >= ]]>
#{page.from}
</select>
注:上面的实例实现的是分页查询数据。我们可以发现使用Map来传递参数这种形式并不好,因为这样使得在接口中只有一个Map参数,其他人进行维护的时候并不清楚到底需要向这个Map里面传递什么参数进去
二 通过给参数添加@Param注解来解决问题:
public List<SecPost> getSecPostByMainId(@Param("mainPostId") String mainPostId,
@Param("startIndex") int startIndex, @Param("endIndex") int endIndex);
(2)此时xxxMapper.xml文件中对应的地方就可以正常使用在@Param注解中对应的值了:
<select id="getSecPostByMainId" parameterType="map" resultType="com.jxd.pojo.SecPost">
select sec3.* from (select rownum as r, sec2.* from (select * from my_second sec1 where sec1.main_id = #{mainPostId} order by sec1.sec_creatime desc) sec2) sec3 where sec3.r between #{startIndex} and #{endIndex}
</select>
Mybatis传递多个参数进行SQL查询的用法的更多相关文章
- Mybatis传递多个参数的4种方式(干货)
Mybatis传递多个参数的4种方式(干货)-----https://blog.csdn.net/youanyyou/article/details/79406486
- Mybatis 传递多个参数
Mybatis提供了4种传递多个参数的方法: 1 Map sql语句 接口 调用方法 这个方法虽然简单易用,但是存在一个弊端:Map存储的元素是键值对,可读性不好. 2 注解 使用MyBatis的参数 ...
- Mybatis传递多个参数的几种方式
顺序传参法 public User selectUser(String name, int deptId); <select id="selectUser" resultMa ...
- MyBatis 传递多个参数的几种方法
简介: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简 ...
- Mybatis传递多个参数
方法一: //DAO层的函数方法Public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...
- Mybatis传递多个参数的解决办法(三种)
第一种方案 DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...
- SQL查询基本用法
-- 单列查询 select 编号 from employees -- 多列查询 select 编号,姓名 from employees -- 查询所有列 select * from employee ...
- Mybatis 传入多个参数查询数据 (3种方法)
第一种方案 DAO层的函数方法 public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
随机推荐
- C++ STL 之 容器的深拷贝和浅拷贝
如果我们没有提供拷贝构造函数,没有重载=操作符,vector 对我们的 mc 对象进行的简单的浅拷贝,将拷贝的对象插入到容器中,导致我们的 mc 对象的 data 指针和容器中mc 对象的拷贝对象中的 ...
- 【leetcode】296.Best Meeting Point
原题 A group of two or more people wants to meet and minimize the total travel distance. You are given ...
- asp.net使用FileUpload控件上传图片且重命名
我在根目录下创建了一个Images图片存放文件夹,上传的图片都在这 下面贴代码 if (FileUpload1.HasFile) { string filename = FileUpload1.Fil ...
- C#DataGrid列值出现E形式的小数,将DataGrid表格上的数据保存至数据库表时会因格式转换不正确导致报错
问题描述:在DataGridView中调整金额一列,当输入小数0.000001后会显示1E-6,此时进行保存操作时报错,提示无法将string类型转换成Decimal 原因分析:由于列调整金额为1E- ...
- 记一次idea后台日志乱码解决办法
- Educational Codeforces Round 40 G. Castle Defense (二分+滑动数组+greedy)
G. Castle Defense time limit per test 1.5 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #588 (Div. 1) C. Konrad and Company Evaluation
直接建反边暴力 复杂度分析见https://blog.csdn.net/Izumi_Hanako/article/details/101267502 #include<bits/stdc++.h ...
- Windows下安装配置Apache+PHP+Mysql环境
1.下载相关安装包 Apache下载: http://archive.apache.org/dist/httpd/binaries/win32/ ,选择httpd-2.2.25-win32-x86-n ...
- SPI使用笔记ADS1259+AD5676
SPI的通信速率通常比较快.目前用到的ADS1259芯片,可以达到2-4MHz,可能可以更加快.一般spi都是从慢速开始调试,但是具体到某个芯片,应该核对芯片时序图,比如ti的ds1259,数据手册上 ...
- http协议。会话控制cookie、session
http协议是无状态的协议.每次访问页面的http协议都是独立的,正是因为http协议是无状态的,所以导致访问一个页面后再去访问另一个页面的时候,一些数据会消失,比如:用户的登录信息就会消失.那么怎么 ...