MyBatis Pass multiple parameters for use in SQL queries _java

Source: Internet
Author: User
Tags cdata


There are two ways PS:IBATIS3 can pass multiple parameters: one is using Java.map, the other is using JavaBean.



When passing only one parameter to the Xxxmapper.xml file, you can simply use "_parameter" to receive the parameters that the Xxxmapper.java passes in and to enter the query, for example:



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


List<string> Selectallairportcode (Boolean mapping);


(2) in the corresponding Xxxmapper.xml file, you can use "_parameter" to receive this 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>


However, if you pass in more than one parameter in the Xxxmapper.java file, you cannot receive parameters using this form, there are two solutions to this problem:


The XML file is always passed in a map<string, object> collection, and the parameters in the map collection are normally used in the XML file.


Specific examples are as follows:



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



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



(2) reference to the map in the specific implementation class defined above:


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) The corresponding Xxxmapper.xml file uses "Java.util.Map" to receive the Map collection at this time:



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

Note: The above example implements a paging query data. We can see that using a map to pass parameters is not a good form, because it makes it possible to have only one map parameter in the interface, while others do not know exactly what parameters to pass into the map.



The second is to solve the problem by adding @param annotations to the parameters:


(1) Add the @param annotation to the parameter in the method of the Xxxmapper.java file, the value in this annotation corresponds to the parameter name used in the XML file:


Airline Selecteffectiveairline (
@Param ("Departureairport") String Departureairport,
@Param (" Arrivalairport ") String Arrivalairport,
@Param (" status ") BigDecimal status);


(2) The corresponding value in the @param annotation can be used normally in the corresponding place of the Xxxmapper.xml file at this time:


<select id= "Selecteffectiveairline" resultmap= "Baseresultmap" parametertype= "Java.util.Map" >
Select
<include refid= "Base_column_list"/>
from
usr_air_line
<where> <if
" 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 necessary to note that the parameters in the IF condition is not the same as in the SQL statement, if the variable is not added #{}




Here are two ways to pass multiple parameters through MyBatis in a separate presentation





Map passes multiple parameters


ParameterType can be an alias or fully qualified name, Map or JAVA.UTIL.MAP, both of which are possible


<select id= "Selectblogbymap" parametertype= "map" resulttype= "Blog" > 
select T.id, T.title, 
t.content From blogs 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", "Use sequence"); 
Blog blog = (blog) session.selectone ("Cn.enjoylife.BlogMapper.selectBlogByMap", param); 
Session.close (); 
System.out.println ("blog title:" +blog.gettitle ()); 


Passing multiple parameters via 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 ("Use sequence!") "); 
Blog Newblog = (blog) session.selectone ("Cn.enjoylife.BlogMapper.selectBlogByBean", blog); 
Session.close (); 
System.out.println ("New Blog ID:" +newblog.getid ()); 
}




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.