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")
publicinterface 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 //记录条数
);
}
<?xmlversion="1.0"encoding="utf8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--与以前一样,namespace的值是对应的映射器接口的完整名称-->
<mappernamespace="com.abc.mapper.TeacherMapper">
<!--教师实体映射-->
<resultMapid="supervisorResultMap"type="Teacher">
<idproperty="id"/>
<resultproperty="name"/>
<resultproperty="gender"/>
<resultproperty="researchArea"column="research_area"/>
<resultproperty="title"/>
<!--collection元素映射教师的指导学生集合的属性。这里采用了
“命名空间名.select语句id”的形式来引用StudentMapper.xml中的
select语句getStudents。关于这种collection元素使用嵌套的
select语句的详情,请参考笔者博客:
http://legend2011.blog.51cto.com/3018495/985907
-->
<collectionproperty="supStudents"column="id"ofType="Student"
select="com.abc.mapper.StudentMapper.getStudents"/>
</resultMap>
<selectid="findTeacherByPage"resultMap="supervisorResultMap">
select * from teacher
order 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;
publicclass CollectionDemo
{
privatestatic ApplicationContext ctx;
static
{
//在类路径下寻找resources/beans.xml文件
ctx = new ClassPathXmlApplicationContext("resources/beans.xml");
}
publicstaticvoid 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("**********************************************");
}
}
}
}
因此,在这里使用了${参数名}的形式引用了相应的参数值。
在Spring的配置文件beans.xml中,也是一样。类似地,汉字“错”后紧跟中文的逗号时也会报此错误。此时若在“错”字后面加一汉字,即不再报错;然而加“啊”字却仍然报错。读者可自行尝试,说不定还能找出其他错误的情形。