在 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 中语句 id, parameterType, returnType 应该分别和 StudentMapper 接口中的方法名,
参数类型,返回值相对应。

2.即使映射器 Mapper 接口可以以类型安全的方式调用映射语句,但是我们也应该负责书写正确的,匹配方法名、参数类型、 和返回值的映射器 Mapper 接口。

如果映射器 Mapper 接口中的方法和 XML 中的映射语句不能匹配,会在运行期抛出一个异常。

实际上,指定 parameterType 是可选的;MyBatis 可以使用反射机制来决定 parameterType。
但是,从配置可读性的角度来看,最好指定parameterType 属性。
如果 parameterType 没有被提及,开发者必须查看Mapper XML 配置和 Java 代码了解传递给语句的输入参数的数据类型。
来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/xiao2/p/5728093.html

映射器配置文件和映射器接口相关推荐

  1. [MyBatis日记](3)映射器配置文件与映射器接口

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/50659993 前一篇文章([MyBatis日 ...

  2. SpringMvc及三大组件(适配器、映射器、视图解析器)

    文章目录 1 SpringMvc三大组件 适配器 映射器 视图解析器 1 @RequestMapping和@Controller **1. 什么是mvc** 2. SpringMvc三大组件及其作用 ...

  3. 13-容器的端口映射

    13-容器的端口映射 部署一个简单web nginx容器 docker run -d --name web nginx nginx 默认的端口是 80 端口,此时我们是没有办法访问的. 好的,通过前面 ...

  4. ElasticSearch03_Mapping字段映射、常用类型、数据迁移、ik分词器、自定义分词器

    文章目录 ①. Mapping字段映射概述 ②. 常用类型如下 - text.keyword ③. 映射中对时间类型详解 ④. ES的keyword的属性ignore_above ⑤. 映射的查看.创 ...

  5. 商城项目18_esMapping字段映射、常用类型、数据迁移、ik分词器、自定义分词器

    文章目录 ①. Mapping字段映射概述 ②. 常用类型如下 - text.keyword ③. 映射中对时间类型详解 ④. ES的keyword的属性ignore_above ⑤. 映射的查看.创 ...

  6. java元婴期(23)----java进阶(mybatis(2)---mapper代理mybatis核心配置文件输入输出映射)

    1.mapper代理 1.mybatis开发dao的方法----引入(为啥要使用mapper代理) 1.SqlSession使用范围 1.SqlSessionFactoryBuilder 通过SqlS ...

  7. MyBatis全局配置文件和映射文件

    Mybatis文件的全局配置和映射文件: 配置文件 MyBatis 的配置文件包含了影响 MyBatis 行为的设置(settings)和属性(properties)信息. 配置文件结构 123456 ...

  8. Mybatis之配置文件与映射文件的“那些事”,你真的知道吗?

    编译软件:IntelliJ IDEA 2019.2.4 x64 操作系统:win10 x64 位 家庭版 Maven版本:apache-maven-3.6.3 Mybatis版本:3.5.6 目录 一 ...

  9. Hibernate配置文件与映射文件详解

    Hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架. 我们先看一下官方文档所给出的,Hibernate 体系结构的高层视图: 其中PO=P ...

  10. Spring Boot 注解配置文件自动映射到属性和实体类

    官网给出的配置文件大全: https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#comm ...

最新文章

  1. Actuator 端点监控
  2. [转]Angular 单元测试讲解
  3. 中的实践 中兴_中兴数字化转型:做“极致的云公司” 用5G制造5G
  4. PyRobot开辟 AI 机器人框架
  5. HR别掉坑里了,送你最精确的计薪算法!
  6. 关键词: iostream 和 iostream.h
  7. 好东西再安利一遍!!
  8. linux中文输入法配置
  9. 怎么大量转换图片格式为tiff
  10. model.compile
  11. C++ 打印表格到屏幕或文件
  12. 命令与征服3 凯恩之怒
  13. JAVA 对接钉钉API(人员、部门、官方智能工作流)20210527
  14. Javascript特效之删除内容效果
  15. 数据库的表、字段、字段的值的相关操作
  16. oracle 空值排序,排在最前面或者最后面
  17. classify线性判别分析函数
  18. (java)求最大公约数
  19. shader学习之路——更复杂的光照之Base Pass 和 Additional Pass和光照的衰减
  20. c#中应用skinEngine给应用程序换皮肤

热门文章

  1. 序列化和反序列化(一)——概述
  2. Java对数组的操作(三)—比较两个数组中的元素是否相同的最简单方法
  3. controller报错MissingServletRequestParameterException: Required xxx parameter ‘xxx’ is not present
  4. 帮你轻松理解Commonjs、AMD、CMD、ES6的区别
  5. 理解Java - 线程池
  6. 阿里开源框架Jarslink1.6.1新特性
  7. 快速启动器工具 Maye(转载)
  8. 07. Declare destructors virtual in polymorphic base classes
  9. CSS的一些常用知识点
  10. 航空信息指挥调度系统_应急指挥中心指挥调度系统解决方案(一)