1. 环境准备

1.1 数据库准备

  1. -- 创建数据库
  2. CREATE database i_boot;
  3. -- 创建表
  4. DROP TABLE IF EXISTS `t_user`;
  5. CREATE TABLE `t_user` (
  6. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  7. `name` varchar(255) NOT NULL,
  8. `age` tinyint(4) unsigned DEFAULT NULL,
  9. `email` varchar(255) DEFAULT NULL,
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=2;
  12. INSERT INTO `t_user` VALUES ('1', 'jack', '18', 'jack@126.com');

1.2 创建项目

image.png
不选择任何依赖,手动导入:
image.png
image.png

1.3 数据源配置

pom 文件添加依赖:

<!-- mysql驱动 -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

此时启动 SpringBoot 工程会出现一个错误:需要配置数据库相关信息。
image.png
application.yml 配置数据库信息:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/i_boot?serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver

2. 整合 MyBatis

2.1 引入依赖

引入 MyBatis 和 SpringBoot 整合的 Starter

<!-- MyBatis 和 SpringBoot 整合的 Starter -->
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.2.2</version>
</dependency>

2.2 创建实体类

image.png

package com.imcode.entity;

public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String email;
   ......
}

2.3 注解方式开发

编写 Mapper 接口

image.png

package com.imcode.mapper;

import com.imcode.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    @Select("select * from t_user where id = #{id}")
    User getById(Integer id);
}

测试

image.png

package com.imcode.mapper;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class UserMapperTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    void getById() {
        System.out.println(userMapper.getById(1));
    }
}

2.4 XML 方式开发

编写 Mapper 接口

package com.imcode.mapper;

import com.imcode.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    @Select("select * from t_user where id = #{id}")
    User getById(Integer id);

    // 添加新增用户接口
    void insert(User user);
}

编写 Mapper 配置文件

<?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.imcode.mapper.UserMapper">

    <insert id="insert" parameterType="com.imcode.entity.User">
        insert into t_user(name,age,email) values (#{name},#{age},#{email})
    </insert>

</mapper>

Mapper 映射文件配置

在 application.yml 中添加 mapper 配置文件的位置:

mybatis:
  mapper-locations: classpath:mapper/*.xml

测试

package com.imcode.mapper;

import com.imcode.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class UserMapperTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    void insert() {
        User user = new User();
        user.setName("rose");
        user.setAge(16);
        user.setEmail("rose@126.com");
        userMapper.insert(user);
    }
}

2.5 集中扫描 Mapper 接口

在启动类上添加以下注解:@MapperScan(“com.imcode.mapper”)

package com.imcode;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.imcode.mapper")
public class IBootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(IBootMybatisApplication.class, args);
    }
}

去掉 Mapper 接口的 @Mapper 注解:
image.png

2.6 使用 Druid

SpringBoot 默认使用的是 HikariPool 数据库连接池。运行测试用例,查看控制台输出:
image.png
添加 Druid 启动器:在 pom.xml 添加 Druid 的 Starter。

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

再次运行测试用例,查看控制台输出:
image.png