Spring Boot整合MyBatis

本文讲解Spring Boot如何整合MyBatis。

项目整体结构:

1 建立表结构

建立t_customer表

-- 创建客户表
CREATE TABLE t_customer(
   id INT PRIMARY KEY AUTO_INCREMENT,
   NAME VARCHAR(20),
   gender CHAR(1),
   telephone VARCHAR(20)
);

2 创建项目,导入依赖

<?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.yiidian</groupId>
    <artifactId>ch03_06_springboot_mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 导入springboot父工程. 注意:任何的SpringBoot工程都必须有的!!! -->
    <!-- 父工程的作用:锁定起步的依赖的版本号,并没有真正到依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
    </parent>

    <dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mybatis 起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- MySQL 连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
    </dependencies>
</project>

这里关键是要导入mybatis和mysql驱动程序

3 编写Pojo实体类

package com.yiidian.domain;
/**
 * 实体类
 * 一点教程网 - www.yiidian.com
 */
public class Customer {
	private Integer id;
	private String name;
	private String gender;
	private String telephone;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	@Override
	public String toString() {
		return "Customer{" +
				"id=" + id +
				", name='" + name + '\'' +
				", gender='" + gender + '\'' +
				", telephone='" + telephone + '\'' +
				'}';
	}
}

4 编写Dao接口

package com.yiidian.dao;

import com.yiidian.domain.Customer;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Dao接口
 * 一点教程网 - www.yiidian.com
 */
@Mapper
public interface CustomerDao {
    /**
     * 查询所有用户
     */
    public List<Customer> findAll();
}

必须给Dao接口加上@Mapper,这样Spring Boot在启动时才能扫描到Dao接口,并为其生成代理对象。

5 编写Dao映射配置

在Dao接口相同目录下建立同名的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.yiidian.dao.CustomerDao">

    <!-- 查询所有用户 -->
    <select id="findAll" resultType="customer">
        select * from t_customer
    </select>
</mapper>

6 编写Controller

package com.yiidian.controller;
import java.util.ArrayList;
import java.util.List;

import com.yiidian.dao.CustomerDao;
import com.yiidian.domain.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 控制器
 * 一点教程网 - www.yiidian.com
 */
@Controller
public class CustomerController {
	@Autowired
	private CustomerDao customerDao;

	/**
	 * 用户列表展示
	 */
	@RequestMapping("/list")
	@ResponseBody
	public List<Customer> list(){
		//模拟用户数据
		List<Customer> list = customerDao.findAll();
		return list;
	}
}

7 编写application.yml

内容如下:

spring:
  datasource: #修改数据库连接配置
    url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root

# mybatis配置
mybatis:
  type-aliases-package: com.yiidian.domain # 别名目录

该文件主要配置了数据源连接参数,及mybatis相关配置

8 编写引导类

package com.yiidian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * Spring Boot引导类
 * 一点教程网 - www.yiidian.com
 */
@SpringBootApplication
public class MyBootApplication {

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

}

9 运行测试

运行引导类,访问:http://localhost:8080/list,结果为

 

源码下载:https://pan.baidu.com/s/1SEa9iewf5gdbHrrpaIFpqg