About the use of SQL queries for passing multiple parameters to MyBatis

Source: Internet
Author: User
Tags cdata

When passing only one parameter to the xxxmapper.xml file, you can simply use "_parameter" to receive the parameters passed in by Xxxmapper.java , and to substitute the query, for example:

(1) This definition in the Xxxmapper.java file:

List<string> Selectallairportcode (Boolean mapping);

(2) At this point in the correspondingthe Xxxmapper.xml file can be used"_parameter"To receive this parameter:

<select id= "Selectallairportcode"  resulttype= "java.lang.String" parametertype= "Java.lang.Boolean" >select departure_airport from usr_air_line union selectarrival_airport from  usr_air_line<if test= "_parameter == true" >union select rel_departure_ Airport from usr_air_line unionselectrel_arrival_airport from usr_air_line</if> </select> 

However, if you pass multiple parameters in the Xxxmapper.java file, you cannot use the form above to receive parameters, there are two options for solving the problem:

always pass in an XML file a map< String, Object>, collection , and then the various parameters in the map collection can be used normally in the XML file. Examples are as follows:

(1) This definition in the Xxxmapper.java file:

List<airline> findAll (map<string, object> parms);

(2) for map parameters in the specific implementation class defined above:

Public list<airline> findAll (PageInfo page,airline Airline) {hashmap<string,object> params = new HashMap <String,Object> ();p arams.put ("page", page);p arams.put ("Airline", airline); return Airlinemapper.findall ( params);}

(3) at this time the correspondingThe xxxmapper.xml file uses "Java.util.Map" to receive this Map collection:

<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"  />fromusr_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>

Note: The above example implements the paging query data. We can see that it is not good to use map to pass parameters, because this makes it possible to have only one map parameter in the interface, and it is not clear what parameters need to be passed to the map when the other person is maintaining it

two   fix the problem by adding @param annotations to parameters:

(1) Add @Param annotations, The values in this annotation correspond to the parameter names used in the XML file:

airline selecteffectiveairline (@Param ("Departureairport")  string departureairport, @Param (" Arrivalairport ")  string arrivalairport, @Param (" status ")  bigdecimal status); 

(2) at this time @Param annotation can be used normally in the corresponding place in the >xxxmapper.xml file :

<select id= "Selecteffectiveairline"  resultmap= "Baseresultmap" >select<include refid= " Base_column_list " />fromusr_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> 

Note: It is important to note that the parameters in the if condition judgment are not the same as those in the SQL statement, if the variables in the judgment are not added #{}


This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1772616

Usage of SQL queries for passing multiple parameters to MyBatis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.