PS: There are two ways to pass multiple parameters in ibatis3: one is to use java.Map, and the other is to use JavaBean.
When only one parameter is passed to the xxxMapper.xml file, you can simply use "_parameter" to receive the parameters passed in xxxMapper.java and substitute them into the query, for example:
(1) The following is defined in the xxxMapper.java file:
List<String> selectAllAirportCode(Boolean mapping);
(2) At this time, you can use "_parameter" to receive this parameter in the corresponding xxxMapper.xml file:
<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 unionelectREL_ARRIVAL_AIRPORT from USR_AIR_LINE unionelectREL_ARRIVAL_AIRPORT from USR_AIR_LINE unionelectREL_ARRIVAL_AIRPORT from USR_AIR_LINE unionelectREL_AIRPORT from USR_AIR_LINE unionelectREL_ARRIVAL_AIRPORT from USR_AIR_LINE</if></select>
However, if multiple parameters are passed in the xxxMapper.java file, the above form cannot be used to receive parameters. At this time, there are two solutions to solve this problem:
Pass a Map<String, Object> collection into the xml file, and then the various parameters in the Map collection can be used normally in the xml file.
Specific examples are as follows:
(1) The following is defined in the xxxMapper.java file:
List<Airline> findAll(Map<String, Object> parms);
(2) Pass parameters to 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) At this time, the corresponding 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':'arr ivalAirport','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 pagination query data. We can find that using Map to pass parameters is not good, because this makes it possible that there is only one Map parameter in the interface. When others are maintaining it, they are not sure what parameters to pass into this map.
2. Solve the problem by adding @Param annotation to the parameters:
(1) Add @Param annotation to the parameters in the method of the xxxMapper.java file. The values in this annotation correspond to the parameter name used in the xml file:
Airlines selectEffectiveAirline(@Param("departureAirport") String departmentAirport,@Param("arrivalAirport") String arrivalAirport,@Param("status") BigDecimal status);(2) At this time, the corresponding places in the xxxMapper.xml file can be used normally in the @Param annotation:
<select id="selectEffectiveAirline" resultMap="BaseResultMap" parameterType="java.util.Map">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 should be noted that the parameters in the if condition judgment are written differently from those in SQL statements. The variables in the if judgment are not added #{ }
Below I will introduce to you two methods of passing multiple parameters through Mybatis
Map passes multiple parameters
parameterType can be an alias or fully qualified name, map or java.util.Map, both of which are OK.
<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", "User sequence"); Blog blog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByMap",param); session.close(); System.out.println("blog title:"+blog.getTitle()); } Passing multiple parameters through JavaBean
<select id="selectBlogByBean" parameterType="Blog" resultType="Blog">select t.ID, t.title, t.content from blog twheret.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()); }