MyBatis多参数传递的四种方式

来源:互联网 发布:javascript 尚学堂 编辑:程序博客网 时间:2024/04/28 16:26

一、多参数传递之注解方式示例

若映射器中的方法只有一个参数,则在对应的SQL语句中,可以采用#{参数名}的方式来引用此参数,以前的例子多属于此类。但这种方法却不适用于需要传递多个参数的情况,今天就来介绍如何使用注解传递多个参数。

1、使用注解实现多参数传递
      首先应引入“org.apache.ibatis.annotations.Param”,我们在接口TeacherMapper中引入,并增加一个教师分页查询的方法findTeacherByPage的声明。如下所示:

package com.abc.mapper;import com.abc.domain.Teacher;import org.springframework.stereotype.Component;import java.util.List;//使用@Param注解需要先引入Paramimport org.apache.ibatis.annotations.Param;//@Component指定映射器名称为myTeacherMapper//相关内容,可参考笔者博客://http://legend2011.blog.51cto.com/3018495/980150@Component("myTeacherMapper")public interface TeacherMapper {public Teacher getById(int id);//分页查询教师信息public List<Teacher> findTeacherByPage(//使用@Param("sort")注解,即可在SQL语句中//以“#{sort}”的方式引用此方法的sort参数值。//当然也可以在@Param中使用其他名称,//如@Param("mysort")@Param("sort") String sort,//排序字段//以下三个注解同理@Param("dir") String dir,  //排序方向@Param("start") int start, //起始记录@Param("limit") int limit  //记录条数);}

对应的映射文件TeacherMapper.xml的内容如下:

<?xmlversion="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 property="supStudents" column="id" ofType="Student"select="com.abc.mapper.StudentMapper.getStudents"/></resultMap><select id="findTeacherByPage" resultMap="supervisorResultMap">select * from teacherorder by ${sort} ${dir} limit #{start},#{limit}</select></mapper>
运行主程序如下:

package com.demo;import org.springframework.context.ApplicationContext;import com.abc.mapper.StudentMapper;import com.abc.mapper.TeacherMapper;import com.abc.domain.Teacher;import com.abc.domain.Student;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;public class CollectionDemo{private static ApplicationContext ctx;static{//在类路径下寻找resources/beans.xml文件ctx = new ClassPathXmlApplicationContext("resources/beans.xml");}public static void main(String[] args){//从Spring容器中请求映射器TeacherMapper mapper =(TeacherMapper)ctx.getBean("myTeacherMapper");Teacher teacher = null;//查询教师分页信息List<Teacher> teachers =//以name字段升序排序,从第0条记录开始查询。//查询2条记录mapper.findTeacherByPage("name","asc",0, 2);if(teachers == null){System.out.println("未找到相关教师信息。");}else{Object[] t = teachers.toArray();System.out.println("**********************************************");for(int i = 0; i < t.length; i++){teacher = (Teacher)t[i];System.out.println("教师姓名:" + "  " + teacher.getName());System.out.println("教师职称:" + "  " + teacher.getTitle());System.out.println("指导学生信息:");//遍历指导的学生for(Student s : teacher.getSupStudents()){System.out.println( s.getName() + "  " + s.getGender()+ "  " + s.getGrade()+ "  " + s.getMajor());}System.out.println("**********************************************");}}}}

2、可能会遇到的错误
      关于order by
      一般而言,我们会使用#{参数名}的形式来引用方法中的参数,但这种方式对于order by子句无效或报错。例如,当TeacherMapper.xml的select语句findTeacherByPage中的order by子句以#{sort}的形式引用方法中的sort参数的值时,是无效的(读者可自行验证);以#{dir}的形式引用方法中的dir参数的值时,会报MySQLSyntaxErrorException。因此,在这里使用了${参数名}的形式引用了相应的参数值。


二、多参数传递之默认命名方式示例

       对于映射器中的方法,MyBatis默认从左到右给方法的参数命名为param1、param2…,依次类推。我们可以无需借助注解,直接在SQL语句中使用这些默认名称。首先去掉@Param注解的TeacherMapper.java如下所示:

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

      #{…}与${…}差异小议
      MyBatis官方文档(http://code.google.com/p/mybatis/wiki/faq)对两者的描述是:#{…}是一个参数标记,而${…}只是简单的字符串替换。一般而言,为避免SQL注入攻击,传递参数应使用#{…}方式,因为这样MyBatis会处理好特殊字符转义的问题;但在SQL语句的某些地方,又不能使用#{…}方式。上述文档举出的例子是不能用这种方式指定表名,而根据我们的经验,在order by子句中也不能用这种方式。从中我们可以总结出:对于诸如表名、字段名(如order by子句后的排序字段)这些表本身或其字段的名字,和SQL关键字(如order by子句后的asc关键字),是不能使用#{…}方式的,而只能使用字符串替换的${…}方式。


三、MyBatis多参数传递之Map方式示例

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

//分页查询教师信息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"可以不要。


四、MyBatis多参数传递之混合方式

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

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

那么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);

相应的映射配置(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>

在以上的映射文件中,使用#{param1.title}的形式就能访问Map中title属性的值。当然,在order by子句中应使用${param1.sort}的形式,在本例中使用“#”也是可以的,由此我们可以总结出,我们使用#{参数默认命名.属性名}的形式,就可以在映射文件访问Map参数的属性值。

0 0
原创粉丝点击