MyBatis教程(2)——Mapper.xml详解

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> MyBatis教程(2)——Mapper.xml详解

MyBatis教程(2):Mapper.xml详解

前言:

我们知道,每一款框架产品在实际开发中,都是通过XML文件来培训框架的相关流程的,MyBatis也不例外,主要有两个配置文件:config.xml和Mapper.xml,当然,这两种配置文件可以自定义文件名。

 

config.xml是全局配置文件,主要配置MyBatis的数据源(DataSource),事务管理(TransactionManager),以及打印SQL语句,开启二级缓存,设置实体类别名等功能。

 

Mapper.xml的作用是什么?我们之前介绍过,MyBatis是”半自动”的ORM框架,即SQL语句需要开发者自定义,MyBatis的关注点在POJO与SQL之间的映射关系。那么SQL语句在哪里配置自定义呢?就在Mapper.xml中配置。

首先来介绍Mapper.xml常用属性:

parameterType:参数数据类型

 

(1)基本数据类型,通过id查询User。

 

UserDAO:

 


//通过id查询User
public User getById(int id);

 

UserDAO.xml:

 


select id="getById" parameterType="int" resultType="com.southwind.entity.User"
    select * from user where id=#{id}
/select

 

(2)String类型,通过name查询User。

 

UserDAO:

 


//通过name查询User
public User getByName(String name);

 

UserDAO.xml:

 


select id="get2" parameterType="java.lang.String" resultType="com.southwind.entity.User"
    select * from user where name = #{name}
/select

 

(3)包装类,通过id查询User。

 

UserDAO:

 


//统计id查询User
public User getById(Integer id);

 

UserDAO.xml:

 


select id="getById" parameterType="java.lang.Integer" resultType="com.southwind.entity.User"
    select * from user where id=#{id}
/select

 

(4)多个参数,通过name和age查询User。两个参数分别是String类型和int类型,类型不一致,所以此时parameterType可以省略,通过参数下标取出参数值。

 

UserDAO:

 


//通过name和age查询User
public User getByNameAge(int id,String name);

 

UserDAO.xml:

 


select id="getByNameAge" resultType="com.southwind.entity.User"
    select * from user where name = #{0} and age = #{1}
/select

 

(5)POJO,很显然,当有多个参数时,一个一个写太麻烦了,这时候我们可以将参数列表进行封装,将封装对象作为parameterType的值。

 

UserDAO:

 


//根据Usesr封装对象查询User
public User getByUser(User user);

 

UserDAO.xml:

 


select id="getByUser" parameterType="com.southwind.entity.User" resultType="com.southwind.entity.User"
    select * from user where name = #{name} and age = #{age}
/select

 

resultType:结果类型

 

(1)基本数据类型,统计User总数。

 

UserDAO:

 


//通过User总数量
public int getCount();

 

UserDAO.xml:

 


select id="getCount" resultType="int"
    select count(*) from user
/select

 

(2)包装类,统计User总数。

 

UserDAO:

 


//通过User总数量
public Integer getCount();

UserDAO.xml:

 


select id="getCount" resultType="java.lang.Integer"
    select count(*) from user
/select

 

(3)String类型,根据id查询User的name值。

 

UserDAO:

 


//根据id查询User的name
public String getNameById(int id);

 

UserDAO.xml:

 


select id="getNameById" parameterType="int" resultType="java.lang.String"
    select name from user where id = #{name}
/select

 

(4)POJO,如通过id查询User,上面已经介绍过了,这里就不再重复了。

 

该内容来自 GitChat 平台的《案例上手 Spring 全家桶》达人课,需要查看全部内容的读者,可以扫描下方二维码订阅本课程。

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> MyBatis教程(2)——Mapper.xml详解


 上一篇
MyBatis教程(1)——初识MyBatis MyBatis教程(1)——初识MyBatis
前言: 什么是MyBatis MyBatis 是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2
2021-04-05
下一篇 
MyBatis入门(二) MyBatis入门(二)
MyBatis入门 开发环境 jdk 1.8 jdk 编辑器 IntelliJ IDEA 2017.3 管理工具 Maven 数据库 mysql 案例一:使用mapper映射文档结构 mybaiti
2021-04-05