原文地址:http://legend2011.blog.51cto.com/3018495/1015003

若映射器中的方法只有一个参数,则在对应的SQL语句中,可以采用#{参数名}的方式来引用此参数,以前的例子多属于此类。但这种方法却不适用于需要传递多个参数的情况,今天就来介绍如何使用注解传递多个参数(示例源码下载地址:http://down.51cto.com/data/537051)。

一、使用注解实现多参数传递

首先应引入“org.apache.ibatis.annotations.Param”,我们在接口TeacherMapper中引入,并增加一个教师分页查询的方法findTeacherByPage的声明。如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.abc.mapper;
import com.abc.domain.Teacher;
import org.springframework.stereotype.Component;
import java.util.List;
//使用@Param注解需要先引入Param
import 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的内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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元素使用嵌套的
select语句的详情,请参考笔者博客:
http://legend2011.blog.51cto.com/3018495/985907
-->
<collection property="supStudents" column="id" ofType="Student"
select="com.abc.mapper.StudentMapper.getStudents"/>
</resultMap>
<select id="findTeacherByPage" resultMap="supervisorResultMap">
select * from teacher
order by ${sort} ${dir} limit #{start},#{limit}
</select>
</mapper>

运行主程序如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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",02);
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("**********************************************");
}
}
}
}
   运行结果如下:

二、可能会遇到的错误

1、关于order by

一般而言,我们会使用#{参数名}的形式来引用方法中的参数,但这种方式对于order by子句无效或报错。例如,当TeacherMapper.xml的select语句findTeacherByPage中的order by子句以#{sort}的形式引用方法中的sort参数的值时,是无效的(读者可自行验证);以#{dir}的形式引用方法中的dir参数的值时,会报MySQLSyntaxErrorException,如下图所示:

因此,在这里使用了${参数名}的形式引用了相应的参数值。

2、invalid XML character错误

这是一个诡异的错误。当在映射文件内的注释中,汉字“错”后紧跟中文的句号时即报此错误,如下图所示:

在Spring的配置文件beans.xml中,也是一样。类似地,汉字“错”后紧跟中文的逗号时也会报此错误。此时若在“错”字后面加一汉字,即不再报错;然而加“啊”字却仍然报错。读者可自行尝试,说不定还能找出其他错误的情形。

报出的异常都是org.xml.sax.SAXParseException(如上面错误图片中红框左边所示),这也许意味着它们都是使用同样的xml解析组件。而这种错误,会不会是此组件的bug?

最新文章

  1. Python学习笔记—Python基础1 介绍、发展史、安装、基本语法
  2. android-xxxx must implement the inherited abstract method报错
  3. 第1章 ZigBee协议栈初始化网络启动流程
  4. c#图片添加水印
  5. Ubuntu14.04 安装 PHP cURL
  6. 九度OJ1172--哈夫曼树
  7. C# 父类的属性赋值给子类的方法
  8. Python的subprocess模块
  9. 最受欢迎的iOS第三方SDK
  10. IOS中的通知NSNotification
  11. 多线程:head first Thread.join()
  12. SSM所需的jar
  13. nginx正向代理和反正代理区别
  14. jdbc的入门学习
  15. [Leetcode]27. 移除元素
  16. 【mysql】:mysql性能优化总结
  17. php 统计某个目录中所有文件的大小
  18. es6阮一峰读后感
  19. 配置本地无密码 SSH登录远程服务器
  20. PostgreSQL主备流复制机制

热门文章

  1. ZOJ 3829 模拟贪心
  2. 怎样訪问pcie整个4k的配置空间
  3. Android生命周期里你也许不知道的事
  4. iOS中respondsToSelector与conformsToProtocol的相关理解和使用
  5. Xshell dns tunnel攻击
  6. caffe中lenet_train_test.prototxt配置文件注解
  7. 92.bower 需要git
  8. xBIM 高级01 IFC多模型合并
  9. POJ 3188暴搜
  10. Uva 1605 Building for UN【构造法】