当只向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注解来解决问题:

(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注解中对应的值了:

<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>

  

 

最新文章

  1. make menuconfig出错,需要安装libncurses5-dev找不到文件的终极解决办法(不必更换源,适用于ubuntu 32位平台)
  2. C#读写XML
  3. double 类型转化为Integer类型 ---DecimalFormat
  4. 汉化入门之ExplorerControls
  5. (ios实战) UINavigationBar 返回按钮 文本自定义实现
  6. 使用Maven Profile实现多环境构建
  7. UVa 10054 The Necklace BFS+建模欧拉回路
  8. 使用vs中的工具进行架构比较
  9. [SQL注入1]From SQL injection to Shell
  10. Keil 3光标问题 以及汉字问题
  11. Resources (being shared)
  12. GIt -- Window下配置 git
  13. 实验八 &lt;FBG&gt; 基于原型的团队项目需求调研与分析
  14. python之99乘法表
  15. T4代码生成器
  16. eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)
  17. CSS| font property
  18. oracle数据库中如何去除空格
  19. MAC升级openssl
  20. MVC中Ajax post 和Ajax Get——提交对象Model

热门文章

  1. React 工程的 VS Code 插件及配置
  2. 微信公众号支付备忘及填坑之路-java
  3. Java日志规范(转载)
  4. HTML Ueditor图片宽度超出编辑器
  5. Java获取近7个月的起止时间
  6. 【小知识点】去除inline-block元素间间距的办法
  7. github 提交和更新代码
  8. Linux软链接创建及删除
  9. 5.Servlet 对象(request-response)
  10. python 获取导入模块的文件路径