当前位置: 移动技术网 > IT编程>开发语言>Java > Mybatis传递多个参数进行SQL查询的用法

Mybatis传递多个参数进行SQL查询的用法

2019年07月22日  | 移动技术网IT编程  | 我要评论

ps:ibatis3如何传递多个参数有两个方法:一种是使用java.map,另一种是使用javabean。

当只向xxxmapper.xml文件中传递一个参数时,可以简单的用“_parameter”来接收xxxmapper.java传递进来的参数,并代入查询,比如说这样:

(1)xxxmapper.java文件中这样定义:

list<string> selectallairportcode(boolean mapping);

(2)这时在对应的xxxmapper.xml文件中可以使用“_parameter”来接收这个参数:

<select id="selectallairportcode" resulttype="java.lang.string"
parametertype="java.lang.boolean">
select departure_airport from usr_air_line union select
arrival_airport from usr_air_line
<if test="_parameter == true">
union select rel_departure_airport from usr_air_line union
select
rel_arrival_airport from usr_air_line
</if>
</select>

但是,如果在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文件中使用到的参数名称:

airline selecteffectiveairline(
@param("departureairport") string departureairport,
@param("arrivalairport") string arrivalairport,
@param("status") bigdecimal status);

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

<select id="selecteffectiveairline" resultmap="baseresultmap" parametertype="java.util.map">
select
<include refid="base_column_list" />
from
usr_air_line
<where>
<if test="departureairport != null">
departure_airport = #{departureairport}
</if>
<if test="arrivalairport != null">
and arrival_airport=#{arrivalairport}
</if>
<if test="status != null">
and status = #{status}
</if>
</where>
</select>

注:需要注意的是if条件判断中的参数和在sql语句中写法是不一样的,if判断中的变量是没有添加#{ }的

下面在单独给大家介绍下通过mybatis传递多个参数的两个方法

map传递多个参数

parametertype 可以是别名或完全限定名,map或者java.util.map,这两个都是可以的

<select id="selectblogbymap" parametertype="map" resulttype="blog"> 
select t.id, t.title, t.content 
from blog t 
where t.title = #{h_title} 
and t.content =#{h_content} 
</select> 
public void testselectbymap() { 
sqlsession session = sqlsessionfactory.opensession(); 
map<string, object> param=new hashmap<string, object>(); 
param.put("h_title", "oracle"); 
param.put("h_content", "使用序列"); 
blog blog = (blog)session.selectone("cn.enjoylife.blogmapper.selectblogbymap",param); 
session.close(); 
system.out.println("blog title:"+blog.gettitle()); 
} 

通过javabean传递多个参数

<select id="selectblogbybean" parametertype="blog" resulttype="blog">
select t.id, t.title, t.content
from blog t
wheret.title = #{title}
and t.content =#{content} 
</select>
public void testselectbybean() { 
sqlsession session = sqlsessionfactory.opensession(); 
blog blog=new blog(); 
blog.settitle("oracle"); 
blog.setcontent("使用序列!"); 
blog newblog = (blog)session.selectone("cn.enjoylife.blogmapper.selectblogbybean",blog); 
session.close(); 
system.out.println("new blog id:"+newblog.getid()); 
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网