码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis多参数传递之注解方式示例--转

时间:2017-03-30 10:22:07      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:ann   contain   地址   情况   com   except   png   iso   组件   

原文地址: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?

 

MyBatis多参数传递之注解方式示例--转

标签:ann   contain   地址   情况   com   except   png   iso   组件   

原文地址:http://www.cnblogs.com/davidwang456/p/6644468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!