程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Mybatis傳遞多個參數停止SQL查詢的用法

Mybatis傳遞多個參數停止SQL查詢的用法

編輯:關於JAVA

Mybatis傳遞多個參數停止SQL查詢的用法。本站提示廣大學習愛好者:(Mybatis傳遞多個參數停止SQL查詢的用法)文章只能為提供參考,不一定能成為您想要的結果。以下是Mybatis傳遞多個參數停止SQL查詢的用法正文


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()); 
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved