Classroom learning Day04 | Spring Boot basics-Spring Boot integration MyBatis

table of Contents

Words written in the front

1. For reference, learn from: https://blog.csdn.net/howard2005/article/details/107926234
2. If there is something wrong with the content, I hope you can point it out or add it.
3. Class review.

I. Overview

MyBatis: Persistence layer framework, it supports customized SQL, stored procedures and advanced mapping. Avoid almost all JDBC (Java Database Connectivity, Java database connection) code and manual setting of parameters and obtaining result sets. You can use simple XML or annotations to configure and map native information, and combine the interface with Java's POJOs (Plain Ordinary Java Object) 映射成数据库中的记录.

Two, operation

(1) Preparation

1. Database (data source)

Insert picture description here


2. Create project

Create the Spring Boot project MyBatisDemo, which needs to be noted as follows.

Insert picture description here


Comment (comment entity class), Article (article entity class)

Insert picture description here


Insert picture description here


3. Configuration file

The global configuration file application.properties is renamed application.yaml, and the following content is written in it.

# 配置数据库
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 【这里填写自己数据库的密码】

    # 配置Druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    druid.initial-size: 20 #初始连接数
    druid.min-idle: 10 #最小空闲数
    druid.max-active: 100 #最大连按数

# 配置mybatis
mybatis:
  configuration:
    map-underscore-to-camel-case: true # 允许将下划线命名法转换为驼峰命名法(a_id-->aId)
  mapper-locations: classpath:mapper/*.xml # 映射器配置文件路径
  type-aliases-package: net.ch.lesson06.bean # 指定的实体类别名路径

pom.xml file (add Alibaba's Druid data source in it)

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.6</version>
</dependency>

(2) The effect is achieved

1. Use annotations to integrate MyBatis

CommentMapper (comment mapper interface)

Insert picture description here


2. Use configuration files to integrate MyBatis

ArticleMapper (article mapping interface)

Insert picture description here


ArticleMapper.xml (mapper configuration file), see [Refer to the content of the learning link] for details.

Insert picture description here

(3) Test results display

1. Use annotations to integrate MyBatis

testFindById()

Insert picture description here


testInsertComment()

Insert picture description here


testDeleteComment()

Insert picture description here


2. Use configuration file to integrate MyBatis

testFindArticleById()

Insert picture description here


Insert picture description here


testUpdateArticle()

Insert picture description here

Three, supplement

1. Frequently asked questions about SpringBoot integration mybatis (summary)