第一種方案
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注解來解決問題:
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>
|