一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Mybatis傳遞多個參數進行SQL查詢的用法

Mybatis傳遞多個參數進行SQL查詢的用法

2020-05-23 12:24zifangsky JAVA教程

本文給大家介紹Mybatis傳遞多個參數進行SQL查詢的用法的相關知識,本文還給大家介紹了mybatis通過Map傳遞多個參數和JavaBean傳遞多個參數,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起學習吧

PS:ibatis3如何傳遞多個參數有兩個方法:一種是使用java.Map,另一種是使用JavaBean。

當只向xxxMapper.xml文件中傳遞一個參數時,可以簡單的用“_parameter”來接收xxxMapper.java傳遞進來的參數,并代入查詢,比如說這樣:

(1)xxxMapper.java文件中這樣定義:

?
1
List<String> selectAllAirportCode(Boolean mapping);

(2)這時在對應的xxxMapper.xml文件中可以使用“_parameter”來接收這個參數:

?
1
2
3
4
5
6
7
8
9
10
<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傳參:

?
1
2
3
4
5
6
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文件中使用到的參數名稱:

?
1
2
3
4
Airline selectEffectiveAirline(
@Param("departureAirport") String departureAirport,
@Param("arrivalAirport") String arrivalAirport,
@Param("status") BigDecimal status);

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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,這兩個都是可以的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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傳遞多個參數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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());
}

原文鏈接:http://www.zifangsky.cn/388.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产大片51精品免费观看 | 全日爱韩国视频在线观看 | 1024国产看片在线观看 | 欧美日韩精品一区二区三区视频 | 日本中文字幕在线视频站 | 碰91精品国产91久久婷婷 | 91视频国产精品 | 成人男女啪啪免费观看网站 | 调教开发新婚娇妻放荡 | 成人aaaa | 精品国产国产精2020久久日 | 日韩毛片高清在线看 | 男人捅女人动漫 | 亚洲va久久久久综合 | 青青视频国产依人在线 | 精品久久国产 | 国产精品一区二区三区免费 | 日本人与黑人做爰视频网站 | 国产一区二区三区久久小说 | 国产成人精视频在线观看免费 | 国产精品国色综合久久 | 美女啪啪国产 | 国产日韩成人 | 久久最新地址获取 | 国产区综合另类亚洲欧美 | 国产激情影院 | 紧身牛仔裤美女被啪啪久久网 | 秋霞鲁丝影院久久人人综合 | 日本一卡二卡3卡四卡网站精品 | 性色生活片在线观看 | 精品视频在线观看免费 | 国产精品露脸国语对白河北 | 四虎影视网址 | 视频在线视频免费观看 | 黄德维| chinese男性厕所撒尿合集 | 国产精品免费 | 新版孕妇bbwbbwbbw | 电车痴汉中文字幕 | 国产欧美国产综合第一区 | 美女张开大腿让男人桶 |