一、介绍

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

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

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

本文主要讨论XML配置方式,后续文章讨论注解方式。

二、SpringBoot整合Mybatis连接Mysql数据库

1、添加MySQL 连接驱动依赖、SpringBoot Mybatis 依赖,完整pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <name>hello</name>
<description>Demo project for Spring Boot</description> <!--版本信息-->
<properties>
<mybatis-spring-boot>1.3.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
</properties> <!-- SpringBoot启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency> <!-- SpringBoot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency> <!-- lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency> <!-- logback 依赖-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency> <!--Slf4j 依赖-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
</dependencies> </project>

注:

整合MyBatis的核心依赖MyBatis-Spring-Boot-Starter提供:

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

因此,引入该Starter之后,只需要定义一个DataSource即可(application.properties中可配置),它会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。会自动扫描你的Mappers,连接到SqlSessionTemplate,并注册到Spring上下文中。

2、配置application.yml文件

  • 配置数据库连接信息(数据源)

spring:
#数据源
datasource:
url: jdbc:mysql://localhost:3306/springbootdb??useUnicode=true&amp;characterEncoding=UTF-8
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
  • 配置mybatis

#mybatis配置
mybatis:
typeAliasesPackage: com.example.mybaitsxml.dao.entity
mapperLocations: classpath:mapper/*.xml
#configLocation: classpath:/mybatis-config.xml

注:通常,若mybatis配置信息较少,只是针对基本配置无需复杂配置,则只需在application.yml文件中配置即可,否则最好配置在 mybatis-config.xml中。

3、代码实现

demo工程文件结构:

  • controller层

@Slf4j
@RestController
@RequestMapping("/web")
public class UserController {
@Autowired
private UserService userService; @GetMapping("/queryAllUsers")
public List<User> queryAllUsers(){
return userService.queryAllUsers();
}
}
  • service层(实现)

@Slf4j
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper; @Override
public List<User> queryAllUsers() {
log.info("/queryAllUsers start...");
return userMapper.queryAllUsers();
}
}
  • dao层

dao层分为数据库实体类(entity)和数据库操作mapper接口(mapper)

entity:

@Data
public class User {
private String name ;
private String sex;
private Integer age;
private Integer classNo; }

mapper:

public interface UserMapper {
List<User> queryAllUsers();
}
  • mapper.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.mybaitsxml.dao.mapper.UserMapper" >
<resultMap id ="UserInfoMap" type="com.example.mybaitsxml.dao.entity.User">
<result column="name_" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
<result column="class_no" property="classNo"/>
</resultMap> <select id = "queryAllUsers" resultMap="UserInfoMap">
select
name_,
sex,
age,
class_no
from
tbl_user
</select>
</mapper>

4、执行效果

5、Demo源码

  本文示例源码已托管到本人Github仓库,有需要的朋友请自行拉取。

地址:https://github.com/gavincoder/SpringBoot--Learning

SpringBoot 整合 Mybatis + Mysql——XML配置方式的更多相关文章

  1. SpringBoot系列-整合Mybatis(XML配置方式)

    目录 一.什么是 MyBatis? 二.整合方式 三.实战 四.测试 本文介绍下SpringBoot整合Mybatis(XML配置方式)的过程. 一.什么是 MyBatis? MyBatis 是一款优 ...

  2. SpringBoot整合MyBatis之xml配置

    现在业界比较流行的数据操作层框架 MyBatis,下面就讲解下 Springboot 如何整合 MyBatis,这里使用的是xml配置SQL而不是用注解.主要是 SQL 和业务代码应该隔离,方便和 D ...

  3. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  4. SpringBoot集成MyBatis的Bean配置方式

    SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis spri ...

  5. SpringBoot整合Mybatis之xml

    SpringBoot整合Mybatis mybatis ORM框架.几个重要的概念: Mapper配置 : 可以使用基于XML的Mapper配置文件来实现,也可以使用基于Java注解的Mybatis注 ...

  6. springboot整合mybatis(映射文件方式和注解方式)

    springboot作为一个微服务框架,给我们开发人员提供极大的便利,秉着约定大于配置的原则,通过starter包的形式为我们做了许多默认的配置,在进行数据持久化到关系型数据库时,我们一般都会首选sp ...

  7. ssm整合(基于xml配置方式)

    本文是基于xml配置的方式来整合SpringMVC.Spring和Mybatis(基于注解的方式会再写一篇文章),步骤如下: (1)首先自然是依赖包的配置文件 pom.xml <project ...

  8. Spring Boot 2.x基础教程:使用MyBatis的XML配置方式

    上一篇我们介绍了如何在Spring Boot中整合我们国人最常用的MyBatis来实现对关系型数据库的访问.但是上一篇中使用了注解方式来实现,而对于很多MyBatis老用户还是习惯于XML的开发方式, ...

  9. springboot整合mybatis的两种方式

    https://blog.csdn.net/qq_32719003/article/details/72123917 springboot通过java bean集成通用mapper的两种方式 前言:公 ...

随机推荐

  1. 生成allure测试报告之后,服务器端口无法访问查看生成的report,可能是这样引起的。

    1. 检查防火墙 2. 如果机器有安装ADsafe,请关闭adsafe后重试

  2. Hexo博客系列(三)-将Hexo v3.x个人博客发布到GitLab Pages

    [原文链接]:https://www.tecchen.xyz/blog-hexo-env-03.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力 ...

  3. springcloud(六)-Ribbon配置自定义算法

    前言 很多场景下,可能根据需要自定义Ribbon的配置,例如修改Ribbon的负载均衡规则等.Spring Cloud Edgware允许使用java代码或属性自定义Ribbon 的配置,两种方式等价 ...

  4. Python pip离线部署

    因为生产环境不能联网,必须使用离线部署pip包,倒也不用部署Pypi镜像那么大工作量,其实蛮简单的,贴出了备忘 pip download -r requirements.txt -d packages ...

  5. 在国内运行Flutter配置的正确姿势--如出现Oops; flutter has exited unexpectedly.

    如果根据flutter的官网教程,在运行flutter doctor时无法下载依赖,请根据以下步骤 export PUB_HOSTED_URL=https://pub.flutter-io.cn ex ...

  6. Mac下使用Eclipse的Show in Terminal提示command not found: mvn

    在Mac下一般配置了Maven的环境变了一般都不会提示,但是如果使用zsh的扩展之后,系统默认的环境变量配置文件会发生变化,尤其使用Eclipse打开终端时,默认不会去读取用户目录下的~/.bashr ...

  7. 【Kafka】Kafka数据可靠性深度解读

    转帖:http://www.infoq.com/cn/articles/depth-interpretation-of-kafka-data-reliability Kafka起初是由LinkedIn ...

  8. Microsoft Power BI Desktop概念学习系列之Microsoft Power BI Desktop的下载和安装(图文详解)

    不多说,直接上干货! 官网 https://powerbi.microsoft.com/zh-cn/downloads/ 这里,一般用126邮箱. 因为对于163这样的邮箱是不行. 欢迎大家,加入我的 ...

  9. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  10. <数据挖掘导论>读书笔记9聚类分析

    1. 聚类分析仅根据在数据中发现的描述对象及其关系的信息,将数据对象分组. 其目标是组内的对象相互之间是相似的或者相关的,而不同组中的对象是不同的或者不相关的. 2.聚类分析的重要技术 K均值:K均值 ...