原文地址: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"
,
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(
"**********************************************"
);
}
}
}
}
|
運行結果如下:
二、可能會遇到的錯誤
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?



