映射器配置文件和映射器接口
findStudentById”的 SQL 语句的,代码如下:
XML Code
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aming.training.mybatis.mappers.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="sex" column="sex" />
<result property="birthday" column="birthday" />
<result property="height" column="height" />
<result property="weight" column="weight" />
<result property="score" column="score" />
<result property="address" column="address" />
<result property="email" column="email" />
<result property="hobby" column="hobby" />
</resultMap>
<select id="findStudentById" parameterType="int" resultType="Student">
SELECT id,name,email,birthday,height,weight,score,address,email,hobby FROM Student WHERE id = #{id}
</select>
</mapper>
@Test
- public void testSelect() {
SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
try{
String mapper = "com.aming.training.mybatis.mappers.StudentMapper.findStudentById";
int id = 1;
Student student = sqlSession.selectOne(mapper,id);
System.out.println(student);
}finally{
sqlSession.close();
}
}
名 com.aming.training.mybatis.mappers.StudentMapper 和语句 id:findStudentById 组成)调用映射的 SQL 语句,但是这种方式
容易出错。你需要检查映射器配置文件中的定义,以保证你的输入参数类型和结果返回类型是有效的。
package com.aming.training.mybatis.mappers;
import java.util.List;
import com.aming.training.mybatis.pojo.Student;
public interface StudentMapper {
/**
* 根据id获取Student对象
* @param id id
* @return Student对象
*/
Student findStudentById(int id);
}
@Test
public void testSelect2(){
SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
try{
int id = 1;
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.findStudentById(id);
System.out.println(student);
}finally{
sqlSession.close();
}
}
致。另外, StudentMapper.xml 中语句 id, parameterType, returnType 应该分别和 StudentMapper 接口中的方法名,
参数类型,返回值相对应。
如果映射器 Mapper 接口中的方法和 XML 中的映射语句不能匹配,会在运行期抛出一个异常。
转载于:https://www.cnblogs.com/xiao2/p/5728093.html
映射器配置文件和映射器接口相关推荐
- [MyBatis日记](3)映射器配置文件与映射器接口
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/50659993 前一篇文章([MyBatis日 ...
- SpringMvc及三大组件(适配器、映射器、视图解析器)
文章目录 1 SpringMvc三大组件 适配器 映射器 视图解析器 1 @RequestMapping和@Controller **1. 什么是mvc** 2. SpringMvc三大组件及其作用 ...
- 13-容器的端口映射
13-容器的端口映射 部署一个简单web nginx容器 docker run -d --name web nginx nginx 默认的端口是 80 端口,此时我们是没有办法访问的. 好的,通过前面 ...
- ElasticSearch03_Mapping字段映射、常用类型、数据迁移、ik分词器、自定义分词器
文章目录 ①. Mapping字段映射概述 ②. 常用类型如下 - text.keyword ③. 映射中对时间类型详解 ④. ES的keyword的属性ignore_above ⑤. 映射的查看.创 ...
- 商城项目18_esMapping字段映射、常用类型、数据迁移、ik分词器、自定义分词器
文章目录 ①. Mapping字段映射概述 ②. 常用类型如下 - text.keyword ③. 映射中对时间类型详解 ④. ES的keyword的属性ignore_above ⑤. 映射的查看.创 ...
- java元婴期(23)----java进阶(mybatis(2)---mapper代理mybatis核心配置文件输入输出映射)
1.mapper代理 1.mybatis开发dao的方法----引入(为啥要使用mapper代理) 1.SqlSession使用范围 1.SqlSessionFactoryBuilder 通过SqlS ...
- MyBatis全局配置文件和映射文件
Mybatis文件的全局配置和映射文件: 配置文件 MyBatis 的配置文件包含了影响 MyBatis 行为的设置(settings)和属性(properties)信息. 配置文件结构 123456 ...
- Mybatis之配置文件与映射文件的“那些事”,你真的知道吗?
编译软件:IntelliJ IDEA 2019.2.4 x64 操作系统:win10 x64 位 家庭版 Maven版本:apache-maven-3.6.3 Mybatis版本:3.5.6 目录 一 ...
- Hibernate配置文件与映射文件详解
Hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架. 我们先看一下官方文档所给出的,Hibernate 体系结构的高层视图: 其中PO=P ...
- Spring Boot 注解配置文件自动映射到属性和实体类
官网给出的配置文件大全: https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#comm ...
最新文章
- Actuator 端点监控
- [转]Angular 单元测试讲解
- 中的实践 中兴_中兴数字化转型:做“极致的云公司” 用5G制造5G
- PyRobot开辟 AI 机器人框架
- HR别掉坑里了,送你最精确的计薪算法!
- 关键词: iostream 和 iostream.h
- 好东西再安利一遍!!
- linux中文输入法配置
- 怎么大量转换图片格式为tiff
- model.compile
- C++ 打印表格到屏幕或文件
- 命令与征服3 凯恩之怒
- JAVA 对接钉钉API(人员、部门、官方智能工作流)20210527
- Javascript特效之删除内容效果
- 数据库的表、字段、字段的值的相关操作
- oracle 空值排序,排在最前面或者最后面
- classify线性判别分析函数
- (java)求最大公约数
- shader学习之路——更复杂的光照之Base Pass 和 Additional Pass和光照的衰减
- c#中应用skinEngine给应用程序换皮肤
热门文章
- 序列化和反序列化(一)——概述
- Java对数组的操作(三)—比较两个数组中的元素是否相同的最简单方法
- controller报错MissingServletRequestParameterException: Required xxx parameter ‘xxx’ is not present
- 帮你轻松理解Commonjs、AMD、CMD、ES6的区别
- 理解Java - 线程池
- 阿里开源框架Jarslink1.6.1新特性
- 快速启动器工具 Maye(转载)
- 07. Declare destructors virtual in polymorphic base classes
- CSS的一些常用知识点
- 航空信息指挥调度系统_应急指挥中心指挥调度系统解决方案(一)