本文介绍下SpringBoot整合Mybatis(XML配置方式)的过程。

一、什么是 MyBatis?

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

二、整合方式

SpringBoot整合Mybatis也有两种方式,分别为XML配置方式和注解方式,主要优势点如下:

  1. 注解方式:代码更加精简,方便。
  2. XML配置方式:隔离sql和业务代码,清晰表达sql,尤其对于较长的sql。

XML映射文件也很简单,只有很少的几个顶级元素:

  • cache – 对给定命名空间的缓存配置。
  • cache-ref – 对其他命名空间缓存配置的引用。
  • resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。
  • sql – 可被其他语句引用的可重用语句块。
  • insert – 映射插入语句。
  • update – 映射更新语句。
  • delete – 映射删除语句。
  • select – 映射查询语句。

本文介绍XML配置方式,后续文章再介绍注解方式。

三、实战

新建一个spring boot项目spring-boot-mybatis-xml,按照下面步骤操作。

1.pom.xml中引入jar

整合MyBatis的核心是依赖MyBatis-Spring-Boot-Starter,它提供了:

  • 自动检测现有的DataSource。
  • 将创建并注册SqlSessionFactory的实例,该实例使用SqlSessionFactoryBean将该DataSource作为输入进行传递。
  • 将创建并注册从SqlSessionFactory中获取的SqlSessionTemplate的实例。
  • 自动扫描您的mappers,将它们链接到SqlSessionTemplate并将其注册到Spring上下文,以便将它们注入到您的bean中。

pom.xml重要内容如下:

<!-- mybatis-starter  -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency> <!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

2.application.yml中添加配置

application.yml中添加数据源和mybatis的配置,内容如下:

spring:
#数据源
datasource:
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#mybatis配置
mybatis:
typeAliasesPackage: com.example.springboot.mybatisxml.entity
mapperLocations: classpath:mapper/*.xml
config-location: classpath:mybatis-config.xml

3.添加User的映射文件

UserMapper.xml内容如下:

<?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.example.springboot.mybatisxml.dao.mapper.UserMapper" >
<resultMap id ="UserMap" type="com.example.springboot.mybatisxml.entity.User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="password" property="password"/>
<result column="des" property="des"/>
</resultMap> <select id = "queryAllUsers" resultType= "com.example.springboot.mybatisxml.entity.User">
select * from user
</select>
</mapper>

4.添加dao接口

接口的名字和映射文件的名字相同,接口中方法的名字和要调用的映射文件中的标签的id相同。

UserMapper.java代码如下:

public interface UserMapper {

    List<User> queryAllUsers();
}

5.添加访问控制层

UserController代码如下:

/**
* UserController
*
* @Author: java_suisui
*
*/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService; /**
* 查询 所有用户
*
*/
@GetMapping("/queryAllUsers")
public List<User> queryAllUsers(){
return userService.queryAllUsers();
}
}

四、测试

本地打开浏览器,访问http://localhost:8080/user/queryAllUsers,成功后返回如下结果:

[{"id":1,"name":"张三","password":"123456","sex":0,"des":"无备注"},
{"id":2,"name":"李四","password":"123456","sex":0,"des":"无备注"}]

到此SpringBoot整合Mybatis(XML配置方式)的功能已经全部实现,有问题欢迎留言沟通哦!

完整源码地址: https://github.com/suisui2019/springboot-study

推荐阅读

1.Java中打印日志,这4点很重要!

2.SpringBoot集成JWT实现权限认证

3.一分钟带你了解JWT认证!

4.SpringBoot中如何优雅的读取yml配置文件?

5.SpringBoot中如何灵活的实现接口数据的加解密功能?


限时领取免费Java相关资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高并发分布式、大数据、机器学习等技术。

关注下方公众号即可免费领取:

SpringBoot系列-整合Mybatis(XML配置方式)的更多相关文章

  1. SpringBoot系列-整合Mybatis(注解方式)

    目录 一.常用注解说明 二.实战 三.测试 四.注意事项 上一篇文章<SpringBoot系列-整合Mybatis(XML配置方式)>介绍了XML配置方式整合的过程,本文介绍下Spring ...

  2. spring 5.x 系列第15篇 —— 整合dubbo (xml配置方式)

    文章目录 一. 项目结构说明 二.项目依赖 三.公共模块(dubbo-common) 四. 服务提供者(dubbo-provider) 4.1 productService是服务的提供者( 商品数据用 ...

  3. spring 5.x 系列第13篇 —— 整合RabbitMQ (xml配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 本用例关于rabbitmq的整合提供简单消 ...

  4. spring 5.x 系列第9篇 —— 整合mongodb (xml配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 配置文件位于resources下,项目以单 ...

  5. spring 5.x 系列第17篇 —— 整合websocket (xml配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 项目模拟一个简单的群聊功能,为区分不同的聊 ...

  6. spring 5.x 系列第11篇 —— 整合memcached (xml配置方式)

    文章目录 一.说明 1.1 XMemcached客户端说明 1.2 项目结构说明 1.3 依赖说明 二.spring 整合 memcached 2.1 单机配置 2.2 集群配置 2.3 存储基本类型 ...

  7. SpringBoot 整合 Mybatis + Mysql——XML配置方式

    一.介绍 SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式. SpringBoot整合Mybatis也有两 ...

  8. SpringBoot整合Mybatis,TypeAliases配置失败的问题

    SpringBoot整合Mybatis,TypeAliases配置失败的问题 问题描述 在应用MyBatis时,使用对象关系映射,将对象和Aliase映射起来. 在Mybatis的文档明确写出,如果你 ...

  9. SpringBoot系列之集成Dubbo的方式

    SpringBoot系列之集成Dubbo的方式 本博客介绍Springboot框架集成Dubbo实现微服务的3种常用方式,对于Dubbo知识不是很熟悉的,请先学习我上一篇博客:SpringBoot系列 ...

随机推荐

  1. 高德APP全链路源码依赖分析工程

    一.背景 高德 App 经过多年的发展,其代码量已达到数百万行级别,支撑了高德地图复杂的业务功能.但与此同时,随着团队的扩张和业务的复杂化,越来越碎片化的代码以及代码之间复杂的依赖关系带来诸多维护性问 ...

  2. 如何不用BPM配置时间

    详细方案 配置时间 您需要同步消息接口(JDBC)和异步消息接口(对JEDBCReceiver的响应).对于SAP BAPI,我们不需要消息接口. 注意:CIMS是数据库系统 消息接口(请求/响应) ...

  3. Linux下基于Docker部署.Net Core web api项目

    Docker的好处我就不说啦,问问度娘就知道了

  4. 利用iPhone下载其他地区的App

    参考链接:http://www.anfan.com/news/gonglue/76225.html 有些App由于发布的地区不同,在中国地区未发布的App.使用中国地区的Apple ID只能看到中国地 ...

  5. git配置文件—— .editorconfig

    参考文档 editorconfig官方文档 github/editorconfig/wiki文档 一 概述 editorConfig不是什么软件,而是一个名称为.editorconfig的自定义文件. ...

  6. CSS_跳动的心

    详细教程CSS3 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  7. LightOJ 1253 Misere NIM(反NIM博弈)

    Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, eac ...

  8. ARTS-S ISO C

    一些简称 ANSI: American National Standards Institute. ANSI是the International Organization for Standardiz ...

  9. [状态模式]实现stopwatch

    1.模拟传统面向对象语言的状态模式实现 // Stopwatch类 状态机class Stopwatch {    constructor() {        this.button1 = null ...

  10. Vue AES+MD5加密 后台解密

    前端VUE vue项目中安装crypto-js npm install crypto-js --save-dev CryptoJS (crypto.js) 为 JavaScript 提供了各种各样的加 ...