映射器配置文件和映射器接口

com.mybatis3.mappers 包中的 StudentMapper.xml 配置文件内,是如何配置 id 为”
findStudentById”的 SQL 语句的,代码如下:
XML Code

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.aming.training.mybatis.mappers.StudentMapper">
  5.     <resultMap type="Student" id="StudentResult">
  6.         <id property="id" column="id" />
  7.         <result property="name" column="name" />
  8.         <result property="sex" column="sex" />
  9.         <result property="birthday" column="birthday" />
  10.         <result property="height" column="height" />
  11.         <result property="weight" column="weight" />
  12.         <result property="score" column="score" />
  13.         <result property="address" column="address" />
  14.         <result property="email" column="email" />
  15.         <result property="hobby" column="hobby" />
  16.     </resultMap>
  17.      <select id="findStudentById" parameterType="int" resultType="Student">
  18.         SELECT id,name,email,birthday,height,weight,score,address,email,hobby FROM Student WHERE id =  #{id}
  19.     </select>
  20.  </mapper>
调用findStudentById映射的SQL语句
方法一:
  1. @Test
  2. public void testSelect() {
  3.      SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
  4.      try{
  5.          String mapper = "com.aming.training.mybatis.mappers.StudentMapper.findStudentById";
  6.          int id = 1;
  7.          Student student = sqlSession.selectOne(mapper,id);
  8.          System.out.println(student);
  9.       }finally{
  10.          sqlSession.close();
  11.       }
  12. }
我们可以通过字符串(字符串形式为:映射器配置文件所在的包名 namespace + 在文件内定义的语句 id,如上,即包
com.aming.training.mybatis.mappers.StudentMapper 和语句 id:findStudentById 组成)调用映射的 SQL 语句,但是这种方式
容易出错。你需要检查映射器配置文件中的定义,以保证你的输入参数类型和结果返回类型是有效的。


方法二:
第一步:创建一个映射器接口 StudentMapper.java
  1. package com.aming.training.mybatis.mappers;
  2. import java.util.List;
  3. import com.aming.training.mybatis.pojo.Student;
  4. public interface StudentMapper {
  5.     /**
  6.      * 根据id获取Student对象
  7.      * @param id id
  8.      * @return Student对象
  9.      */
  10.     Student findStudentById(int id);
  11.  }
第二步:使用映射器接口我们可以以类型安全的形式调用调用映射语句
  1. @Test
  2.     public void testSelect2(){
  3.         SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
  4.         try{
  5.             int id = 1;
  6.             StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
  7.             Student student = studentMapper.findStudentById(id);
  8.             System.out.println(student);
  9.         }finally{
  10.             sqlSession.close();
  11.         }
  12.     }
说明:
1.在 StudentMapper.xml 映射器配置文件中,其名空间 namespace 应该跟 StudentMapper 接口的完全限定名保持一
致。另外, StudentMapper.xml 中语句 idparameterTypereturnType 应该分别和 StudentMapper 接口中的方法名,
参数类型,返回值相对应。

2.即使映射器 Mapper 接口可以以类型安全的方式调用映射语句,但是我们也应该负责书写正确的,匹配方法名、参数类型、 和返回值的映射器 Mapper 接口。
如果映射器 Mapper 接口中的方法和 XML 中的映射语句不能匹配,会在运行期抛出一个异常。
实际上,指定 parameterType 是可选的;MyBatis 可以使用反射机制来决定 parameterType
但是,从配置可读性的角度来看,最好指定parameterType 属性。
如果 parameterType 没有被提及,开发者必须查看Mapper XML 配置和 Java 代码了解传递给语句的输入参数的数据类型。



   




posted @ 2016-08-02 09:33  大明二代  阅读(1210)  评论(0编辑  收藏  举报