第一种方案

DAO层的函数方法

public User selectUser(String name,String area);

对应的Mapper.xml

<select id="selectUser" resultMap="BaseResultMap">

select * from user_user_t where user_name = #{0} and user_area=#{1}

</select>

其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。

第二种方案  此方法采用Map传多参数.

Dao层的函数方法

public User selectUser(Map paramMap);

对应的Mapper.xml

<select id="selectUser" resultMap="BaseResultMap">

select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}

</select>

Service层调用

public User xxxSelectUser(){

Map paramMap=new hashMap();

paramMap.put(“userName”,”对应具体的参数值”);

paramMap.put(“userArea”,”对应具体的参数值”);

User user=xxx.selectUser(paramMap);

}

个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。

第三种方案

Dao层的函数方法

public User selectUser(@Param("userName")String name,@Param("userArea")String area);

对应的Mapper.xml

<select id="selectUser" resultMap="BaseResultMap">

select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}

</select>

个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。
---------------------
作者:zhw0596
来源:CSDN
原文:https://blog.csdn.net/zhw0596/article/details/81482770

------------------------------------------------------------------------------------------------------

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集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<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注解来解决问题:

(1)给xxxMapper.java文件的方法中的参数添加@Param注解,这个注解中的值对应xml文件中使用到的参数名称:
public List<SecPost> getSecPostByMainId(@Param("mainPostId") String mainPostId,
@Param("startIndex") int startIndex, @Param("endIndex") int endIndex);

(2)此时xxxMapper.xml文件中对应的地方就可以正常使用在@Param注解中对应的值了:

1
2
3
<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 传入多个参数查询数据 (3种方法)的更多相关文章

  1. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...

  2. 小峰mybatis(2)mybatis传入多个参数等..

    一.mybatis传入多个参数: 前面讲传入多个参数都是使用map,hashmap:key value的形式:-- 项目中开发都建议使用map传参: 比如现在通过两个参数,name和age来查询: 通 ...

  3. (转)MyBatis传入多个参数的问题

    背景:记录mybaitis的使用方法,博闻强记,后面尽量记忆使用. MyBatis传入多个参数的问题 MyBatis传入多个参数的问题 详细记录mybatis在传递多个参数时候的使用方法 关于Myba ...

  4. python操作mongodb根据_id查询数据的实现方法

    python操作mongodb根据_id查询数据的实现方法   python操作mongodb根据_id查询数据的实现方法,实例分析了Python根据pymongo不同版本操作ObjectId的技巧, ...

  5. Spring3 MVC请求参数获取的几种方法

    Spring3 MVC请求参数获取的几种方法 一.      通过@PathVariabl获取路径中的参数 @RequestMapping(value="user/{id}/{name}&q ...

  6. 获取网页URL地址及参数等的两种方法(js和C#)

    转:获取网页URL地址及参数等的两种方法(js和C#) 一 js 先看一个示例 用javascript获取url网址信息 <script type="text/javascript&q ...

  7. Spring3 MVC请求参数获取的几种方法[转]

    Spring3 MVC请求参数获取的几种方法 Spring3 MVC请求参数获取的几种方法 一.      通过@PathVariabl获取路径中的参数 @RequestMapping(value=& ...

  8. Python--day69--ORM查询的13种方法

    ORM查询的13种方法: 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <3> ...

  9. 【SQL】Oracle分页查询的三种方法

    [SQL]Oracle分页查询的三种方法 采用伪列 rownum 查询前10条记录 ? 1 2 3 4 5 6 7 8 9 10 11 [sql] select * from t_user t whe ...

随机推荐

  1. qt连接oracle数据库

    由与qt开源版本没有提供oracle数据库驱动,需要自己根据源代码来手动编译oracle驱动. 经过近三天的折腾,终于成功编译oracle驱动,连接到数据库 ps:期间经过各种失败疼苦迷茫.现在终于完 ...

  2. C++ sizeof(struct) 的注意

    今天在测试将C++代码导出的NavMesh二进制文件用一套C#改写的代码导入时,发现导入的数据出现不一致的问题. 分别在C++和C#AddTile的函数内设置断点,观察最后得到的tile有大部分的字段 ...

  3. vue 钩子函数的初接触

    vue-router的路由钩子函数: 第一种:全局钩子函数. router.beforeEach((to, from, next) => { console.log('beforeEach') ...

  4. 全面聊聊JavaScript的浅拷贝和深拷贝

    一.背景      首先我们可以看下面这段简单的代码: var obj = {name:'程序猿',sex:'男'}; var arr = ['程序猿','程序媛']; var copyobj = o ...

  5. Redis面试题记录--缓存双写情况下导致数据不一致问题

    转载自:https://blog.csdn.net/lzhcoder/article/details/79469123 https://blog.csdn.net/u013374645/article ...

  6. Image Processing and Analysis_8_Edge Detection:Edge and line oriented contour detection State of the art ——2011

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  7. COM_STMT_PREPARE 1

    mysqld_stmt_prepare void mysqld_stmt_prepare(THD* thd, const char * query, uint length, Prepared_sta ...

  8. Linux系统进程的知识总结,进程与线程之间的纠葛...

    来源:嵌入式ARM 当一个程序开始执行后,在开始执行到执行完毕退出这段时间内,它在内存中的部分就叫称作一个进程. Linux 是一个多任务的操作系统,也就是说,在同一时间内,可以有多个进程同时执行.我 ...

  9. mysql总复习

    目录 数据库操作 库操作 表操作 数据行操作 表关系操作 单表操作 外键创建 多表联查 pymysql模块 索引 主键索引 唯一索引 普通索引 数据库操作 库操作 create database 库名 ...

  10. Vue入门到出门

    原来微信小程序的js跟这个差不多啊.这个也不像jQuery那种完全是为了方便写js的感觉,难道算前端框架?还不太了解,总之要先看看,然后用HBuilder快点上手做点东西…… ------------ ...