本文介绍下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. Python常用转义字符

    \   在行尾时是     续行符 \\   反斜杠符号 \' 单引号 \"  双引号 \a  响铃 \b  退格 \e  转义 \000  空 \n     换行 \v 纵向制表符 \t  ...

  2. luogu P4677 山区建小学 |dp

    题目描述 政府在某山区修建了一条道路,恰好穿越总共nnn个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为did_idi​(为正整数),其中,0& ...

  3. react-native 信鸽推送集成

    目录 一. git链接: react-native-xinge-push 1.1 安装 1.2. link 二. android配置 2.1. android/settings.gradle 2.2. ...

  4. 移动前端不得不了解的HTML5 head 头标签 —— Meta 标签

    Meta 标签 meta标签是HTML中head头部的一个辅助性标签,它位于HTML文档头部的 <head> 和 <title> 标记之间,它提供用户不可见的信息.虽然这部分信 ...

  5. HTML5学习第二天!

    学习了一天,然后整理内容到现在,感觉昨天的学习效率有点差,哎! 感受尽在代码中,布局真的脑壳疼,仅仅只整理了CSS中的list: <!DOCTYPE html> <html> ...

  6. 第3节:Java基础 - 必知必会(上)

    第3节:Java基础 - 必知必会(上) 本篇是基础篇的第一小节,我们从最基础的java知识点开始学习.本节涉及的知识点包括面向对象的三大特征:封装,继承和多态,并且对常见且容易混淆的重要概念覆盖和重 ...

  7. CoderForces999E-Reachability from the Capital

    E. Reachability from the Capital time limit per test 2 seconds memory limit per test 256 megabytes i ...

  8. B.Silly Mistake

    题目:愚蠢的错误 题意:中心公司有一个办公室有一个成熟的安全系统,这里面有10^6个雇员,编号从1到10^6 安全系统有入口和出口,数字i表示第i个雇员进入,-i表示第i个雇员出去 公司有一些严格的规 ...

  9. 基于centos7.3 redhat7.3安装LAMP(php7.0 php7.1)生产环境实践

  10. ARTS-S ansible-playbook

    文件a.yml --- - hosts: cluster remote_user: ksotest gather_facts: false tasks: - name: delete dir if e ...