本文介绍下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. SMProxy,让你的数据库操作快三倍!

    SMProxy GITHUB:https://github.com/louislivi/smproxy 喜欢请star 中文 | English /$$$$$$ /$$ /$$ /$$$$$$$ /$ ...

  2. 创建raid5(3个raid2个备份)

    创建raid5(3个raid2个备份) 1.首先创建五个磁盘 2.创建RAID5并设置2块备份故障盘: 3.可查看下阵列的详细信息(Spare Devices数量为1): 4.将磁盘阵列格式化为ext ...

  3. 【django后端分离】rbac组件(文件源代码+使用)

    1:用户,角色,权限,菜单表设计 from django.db import models # 用户菜单 class UserMenu(models.Model): title = models.Ch ...

  4. 初步认知jQuery

    jQuery:是JavaScript的一个类库全写JavaScript query   write less do more JavaScript查询写的更少做的更多 第一步先导入js文件: < ...

  5. Python字符串学习

    Python字符串(不可变的): 一.相关的运算: 1.字符串的拼接: str = str1 + str2 2.字符串的重复: print(str * 3) 3.下标访问字符串某个字符: str[1] ...

  6. [TimLinux] django WSGI入口分析及自定义WSGIHandler思路

    1. 命令行启动 命令行是通过runserver子命令来启动的,对应的django模块为django.core.management.commands.runserver,调用关系结构: # 简化的运 ...

  7. [TimLinux] JavaScript 获取设置在CSS类中的属性值

    1. 设置属性值 // 常用方式 var myEl = document.getElementById('idMyEl'); myEl.style.display = "none" ...

  8. HDU1079 Calender Game

    Adam and Eve enter this year’s ACM International Collegiate Programming Contest. Last night, they pl ...

  9. 5分钟教你看大神操作keepalived服务

    第11章 高可用服务(keepalived)的配置 11.1 高可用服务的概念 11.1.1 高可用服务总体概念 为了解决单点故障 减轻服务器的压力 11.1.2 高可用keepalived的概念 为 ...

  10. JS中的防抖和节流

    JS-防抖和节流 在进行窗口的resize.scroll,输入框内容校验等操作时,如果事件处理函数调用的频率无限制,会加重浏览器的负担,导致用户体验非常糟糕.此时我们可以采用debounce(防抖)和 ...