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

當只向xxxMapper.xml文件中傳遞一個參數時,能夠簡單的用「_parameter」來接收xxxMapper.java傳遞進來的參數,並代入查詢,好比說這樣:java


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



List<String> selectAllAirportCode(Boolean mapping);app

1ide

List<String> selectAllAirportCode(Boolean mapping);this

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



<select id="selectAllAirportCode" resultType="java.lang.String"orm

parameterType="java.lang.Boolean">xml

select DEPARTURE_AIRPORT from USR_AIR_LINE union select接口

ARRIVAL_AIRPORT from USR_AIR_LINEci

<if test="_parameter == true">

union select REL_DEPARTURE_AIRPORT from USR_AIR_LINE union

select

REL_ARRIVAL_AIRPORT from USR_AIR_LINE

</if>

</select>

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

1

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

}

1

2

3

4

5

6

7

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>

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文件中使用到的參數名稱:




Airline selectEffectiveAirline(

@Param("departureAirport") String departureAirport,

@Param("arrivalAirport") String arrivalAirport,

@Param("status") BigDecimal status);

1

2

3

4

Airline selectEffectiveAirline(

            @Param("departureAirport") String departureAirport,

            @Param("arrivalAirport") String arrivalAirport,

            @Param("status") BigDecimal status);

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




<select id="selectEffectiveAirline" resultMap="BaseResultMap">

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>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<select id="selectEffectiveAirline" resultMap="BaseResultMap">

        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

版權聲明:原創做品,容許轉載,轉載時請務必以超連接形式標明文章 原始出處 、做者信息和本聲明。不然將追究法律責任。

轉載請註明來源:關於向Mybatis傳遞多個參數進行SQL查詢的用法 - zifangsky的我的博客

相關文章
相關標籤/搜索