MyBatis关于Mapper配置文件知识集合

来源:互联网 发布:java人事管理系统项目 编辑:程序博客网 时间:2024/06/16 05:47
(1)MyBatis多参数传递之默认命名方式

      对于映射器中的方法,MyBatis默认从左到右给方法的参数命名为param1、param2…,依次类推。我们可以无需借助注解,直接在SQL语句中使用这些默认名称。

      首先去掉@Param注解的TeacherMapper.java如下所示(完整源码下载地址:http://down.51cto.com/data/539217):

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

       按照上述的默认命名方式,MyBatis对findTeacherByPage方法的参数从左到右的默认命名依次是:sort为param1,dir为param2,start为param3,limit为param4。然后,就可以在映射文件TeacherMapper.xml里的、与此方法相对应的SQL语句中以#{参数名}的方式来使用这些名称了。如下第25行所示:

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


      和利用注解的方式一样,在order by子句中使用#{参数名}的方式无效,读者可自行实验。所以,这里仍然使用${参数名}的方式。


(2)MyBatis多参数传递之Map方式

前面的文章介绍了MyBatis多参数传递的注解、参数默认命名等方式,今天介绍Map的方式。仍然以前面的分页查询教师信息的方法findTeacherByPage为例(示例源代码下载地址:http://down.51cto.com/data/546809)。

      首先修改映射器接口TeacherMapper中的findTeacherByPage方法如下:

1
2
//分页查询教师信息
public List<Teacher> findTeacherByPage(Map<String, Object> map);

      相应地,这里用到了Map接口,就应该引入此接口:import java.util.Map。

在执行类CollectionDemo中,调用findTeacherByPage方法的相关代码如下:

Map<String, Object> params = new HashMap<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);


      可以看出,我们先把参数放在了一个Map中,这样我们就可以在相应的SQL语句中以#{…}的形式引用这些参数了。如下所示:

<select id="findTeacherByPage" resultMap="supervisorResultMap"parameterType="java.util.Map">select * from teacher where title like #{title}order by ${sort} ${dir} limit #{start},#{limit}</select>


       与以前一样,在order by子句中应使用${…}的方式。实际上,这里的parameterType="java.util.Map"可以不要。

(3)MyBatis多参数传递之混合方式

网友mashiguang提问如下的方法如何传递参数:public List findStudents(Map conditions, int page, int pageSize)。这是一种混合形式,亦即既有Map类型的参数,也有类似int这种普通类型的参数。经过一番摸索,笔者还比较顺利地找到了这种情况的处理方法。

      其实也很简单。在默认命名方式(MyBatis多参数传递之默认命名方式示例)一文中,介绍了MyBatis对参数的默认命名,这种命名在这种情况下依然有效。我们需要做的,就是如何根据这个命名读出Map中的参数值。这里就采用这种方式来实现教师分页查询。先修改映射器接口(TeacherMapper.java)中的教师分页查询方法的声明如下(完整源码下载:http://down.51cto.com/data/742758): 

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


 (代码1)

      那么MyBatis将会对此方法的三个参数依次命名为param1、param2和param3,其中第一个参数为Map类型,后两个参数为int类型。

      执行类(CollectionDemo.java)中的查询代码片段如下: 

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


  (代码2)

      相应的映射配置(TeacherMapper.xml)文件片段如下: 

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


 (代码3)

      在以上的映射文件中,使用#{param1.title}的形式就能访问Map中title属性的值。当然,在order by子句中应使用${param1.sort}的形式(可参见本系列博文中的“MyBatis多参数传递之注解方式示例”一文,第二部分“可能会遇到的错误”第一个就是关于order by的。不过经笔者验证,在本例中使用“#”也是可以的)。由此我们可以总结出,我们使用#{参数默认命名.属性名}的形式,就可以在映射文件访问Map参数的属性值。


1 0
原创粉丝点击