MyBatis多參數傳遞之註解方式示例--轉

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

若映射器中的方法只有一個參數,則在對應的SQL語句中,能夠採用#{參數名}的方式來引用此參數,之前的例子多屬於此類。但這種方法卻不適用於須要傳遞多個參數的狀況,今天就來介紹如何使用註解傳遞多個參數(示例源碼下載地址:http://down.51cto.com/data/537051)。spring

1、使用註解實現多參數傳遞apache

      首先應引入「org.apache.ibatis.annotations.Param」,咱們在接口TeacherMapper中引入,並增長一個教師分頁查詢的方法findTeacherByPage的聲明。以下所示:mybatis

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的內容以下:app

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 >

 

 

      運行主程序以下:dom

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( "**********************************************" );
}
}
}
}
   運行結果以下:

194610816.png

2、可能會遇到的錯誤spa

      一、關於order bycode

      通常而言,咱們會使用#{參數名}的形式來引用方法中的參數,但這種方式對於order by子句無效或報錯。例如,當TeacherMapper.xml的select語句findTeacherByPage中的order by子句以#{sort}的形式引用方法中的sort參數的值時,是無效的(讀者可自行驗證);以#{dir}的形式引用方法中的dir參數的值時,會報MySQLSyntaxErrorException,以下圖所示:xml

194712292.png

       所以,在這裏使用了${參數名}的形式引用了相應的參數值。blog

二、invalid XML character錯誤

      這是一個詭異的錯誤。當在映射文件內的註釋中,漢字「錯」後緊跟中文的句號時即報此錯誤,以下圖所示:

194826800.png

       在Spring的配置文件beans.xml中,也是同樣。相似地,漢字「錯」後緊跟中文的逗號時也會報此錯誤。此時若在「錯」字後面加一漢字,即再也不報錯;然而加「啊」字卻仍然報錯。讀者可自行嘗試,說不定還能找出其餘錯誤的情形。

      報出的異常都是org.xml.sax.SAXParseException(如上面錯誤圖片中紅框左邊所示),這也許意味着它們都是使用一樣的xml解析組件。而這種錯誤,會不會是此組件的bug?

相關文章
相關標籤/搜索