MyBatis about mapper configuration file Knowledge Collection

Source: Internet
Author: User

(1) MyBatis The default naming method for multi-parameter transfer

For the methods in the Mapper, MyBatis the default left-to-right parameter to the method named Param1, param2 ..., and so on. We can use these default names directly in SQL statements without the help of annotations.

The first Teachermapper.java to remove the @param annotation is as follows (full source: http://down.51cto.com/data/539217):

123456789101112131415161718 packagecom.abc.mapper;importcom.abc.domain.Teacher;importorg.springframework.stereotype.Component;importjava.util.List;//@Component指定映射器名称为myTeacherMapper//相关内容,可参考笔者博客://http://legend2011.blog.51cto.com/3018495/980150@Component("myTeacherMapper")publicinterfaceTeacherMapper {publicTeacher getById(int id);//分页查询教师信息publicList<Teacher> findTeacherByPage(String sort,//排序字段String dir,  //排序方向intstart, //起始记录intlimit  //记录条数);}

In accordance with the default naming method described above, the default naming of the parameters of the Findteacherbypage method from left to right is MyBatis: Sort is param1,dir to Param2,start Param3,limit to PARAM4. These names can then be used as #{parameter names in the SQL statement corresponding to this method in the mapping file Teachermapper.xml. This is shown in line 25th below:

123456789101112131415161718192021222324252627 <?xmlversion="1.0"encoding="utf8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--与以前一样,namespace的值是对应的映射器接口的完整名称--><mappernamespace="com.abc.mapper.TeacherMapper"><!--教师实体映射--><resultMapid="supervisorResultMap"type="Teacher"><idproperty="id"/><resultproperty="name"/><resultproperty="gender"/><resultproperty="researchArea"column="research_area"/><resultproperty="title"/><!--collection元素映射教师的指导学生集合的属性。这里采用了“命名空间名.select语句id”的形式来引用StudentMapper.xml中的select语句getStudents。关于这种collection元素使用嵌套的select语句的详情,请参考笔者博客:http://legend2011.blog.51cto.com/3018495/985907--><collectionproperty="supStudents"column="id"ofType="Student"select="com.abc.mapper.StudentMapper.getStudents"/></resultMap><!--param1、param2等是MyBatis对映射器方法参数的默认命名--><selectid="findTeacherByPage"resultMap="supervisorResultMap">select * from teacherorder by ${param1} ${param2} limit #{param3},#{param4}</select></mapper>

As with annotations, the use of the #{parameter name} in the ORDER BY clause is not valid, and the reader can experiment on its own. So, here is still the way to use the ${parameter name}.


(2) Map mode of MyBatis multi-parameter transfer

In the previous article, we introduced the methods of MyBatis multi-parameter passing annotations, parameter default naming, and so on today. The findteacherbypage is still the example of how to query teacher information in front of pages (example source code: http://down.51cto.com/data/546809).

First modify the Findteacherbypage method in the Mapper interface Teachermapper as follows:

12 //分页查询教师信息publicList<Teacher> findTeacherByPage(Map<String, Object> map);

Accordingly, the Map interface is used here, and this interface should be introduced: import Java.util.Map.

In the execution class Collectiondemo, the associated code for calling the Findteacherbypage method is as follows:

1234567891011 Map<String, Object> params = newHashMap<String, Object>();//以name字段升序排序,params.put("sort""name");params.put("dir""asc");//查询结果从第0条开始,查询2条记录params.put("start"0);params.put("limit"2);//查询职称为教授或副教授的教师params.put("title""%教授");//分页查询教师信息List<Teacher> teachers = mapper.findTeacherByPage(params);

       as you can see, we first put the parameters in a map so that we can #{in the corresponding SQL statements ...} In the form of a reference to these parameters. As shown below:

12345 < select   id = "Findteacherbypage"   resultmap = parametertype = "Java.util.Map" select * from teacher where title like # {title} order by ${sort} ${dir} limit #{start},#{limit} </ select >

As before, you should use ${...} in the ORDER BY clause. The way. In fact, the parametertype= "Java.util.Map" here can not be.

(3) Mixed mode of MyBatis multi-parameter transfer

Netizen Mashiguang Questions The following method how to pass parameters: public List findstudents (Map conditions, int page, int pageSize). This is a mixed form, that is, a parameter of the map type, and a common type such as int. After some groping, the author also found a way to deal with this situation successfully.

It's very simple, actually. In the default naming method (example of the default naming of MyBatis multi-parameter pass), the default naming of parameters is described in MyBatis, which is still valid in this case. What we need to do is to read the parameter values in the map according to the name. This is the way to achieve teacher paging query. The declaration of the Teacher paging Query method in first modifying the Mapper interface (Teachermapper.java) is as follows (full source download: http://down.51cto.com/data/742758):

123456 //分页查询教师信息publicList<Teacher> findTeacherByPage(Map params, //查询条件int start, //起始记录intlimit  //记录条数);

(Code 1)

Then MyBatis will name the three parameters of this method param1, param2, and Param3, where the first parameter is the map type and the latter two are of type int.

The query code snippet in the execution Class (Collectiondemo.java) is as follows:

123456789101112 Map<String, Object> params =newHashMap<String, Object>();//以name字段升序排序,params.put("sort""name");params.put("dir""asc");//查询职称为教授或副教授的教师params.put("title""%教授");//查询教师分页信息List<Teacher> teachers =//以name字段升序排序,从第0条记录开始查询。//查询2条记录mapper.findTeacherByPage(params,02);

(Code 2)

The corresponding mapping configuration (teachermapper.xml) file fragment is as follows:

1234 <selectid="findTeacherByPage"resultMap="supervisorResultMap">select * from teacher where teacher.title like#{param1.title} order by ${param1.sort} ${param1.dir} limit #{param2},#{param3}</select>

(Code 3)

In the above mapping file, the value of the title property in the map can be accessed using the form #{param1.title}. Of course, the form of ${param1.sort} should be used in the ORDER BY clause (see the "Example of annotations for MyBatis multi-parameter passing" article in this series, the second part "errors that may be encountered", the first one is about order by. However, the author verifies that the use of "#" In this example is also possible. From this we can conclude that we use the #{parameter as the default name. Property name}, you can access the map parameter's property values in the mapping file.


MyBatis about mapper configuration file Knowledge Collection

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.