(一)Spring Boot集成MyBatis快速入门

一、在IDEA中创建Spring Boot项目

 

二、添加依赖(把以下三个依赖都勾上)

 

 三、Maven POM

(把上面的依赖选中后,创建项目,打开pom.xml文件就可以看到以下代码,即添加成功)

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhangguo</groupId>
    <artifactId>mybatisdemo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatisdemo2</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Maven POM文件代码

 

四、创建数据库与表

create database mybatis

use mybatis

CREATE TABLE `category` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '类型编号',
  `name` varchar(64) NOT NULL COMMENT '类型名称',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

INSERT into category values(1,'五金');
INSERT into category values(2,'数码');
INSERT into category values(3,'服饰');
INSERT into category values(4,'生鲜');
INSERT into category values(5,'书籍');
INSERT into category values(6,'珠宝');

select * from `category`
数据库代码

 

五、添加实体类

package com.zhangguo.mybatisdemo2.entity;

import java.io.Serializable;

public class Category implements Serializable {
    // 字段
    private int id;
    private String name;

    // 访问器
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    //重写toString方法 
    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
实体类代码

 

六、定义数据访问接口

package com.zhangguo.restdemo.dao;

import com.zhangguo.restdemo.entity.Category;

import java.util.List;

public interface CategoryDao {
    List<Category> findAll();
}
接口代码

 

七、定义sql映射文件

(在resources目录下创建一个mapper目录,专门用于存放sql映射文件,在目录中创建一个CategoryDao.xml文件,如下图所示:)

 CategoryDao.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.fairy.mybatisdemo2.dao.UserDao">
    <select id="findById" resultType="User">
        select
               uid,
               uname,
               usex
        from userinfo
        where uid=#{uid}
    </select>
</mapper>
CategoryDao.xml代码

注意:

 

八、在application.yaml中定义配置文件

(application.yaml映射文件中主要包含了数据库连接信息与mybatis配置信息)

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8

mybatis:
  type-aliases-package: com.fairy.mybatisdemo2.entity
  mapper-locations: classpath:/mapper/*.xml
连接信息与配置信息

注意:

 

 九、配置映射接口所在的包

(mybatisdemo2Application.java启动文件的内容如下)

package com.fairy.mybatisdemo2;

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

@SpringBootApplication
@MapperScan("com.fairy.mybatisdemo2.dao") // dao包路径
public class Mybatisdemo2Application {

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

}
启动文件代码

@MapperScan作用是指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。

 

十、编写单元测试

package com.zhangguo.mybatisdemo2;

import com.zhangguo.mybatisdemo2.dao.CategoryDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Mybatisdemo2ApplicationTests {

    @Autowired
    CategoryDao categoryDao;

    @Test
    void categoryDaoTest() {
        System.out.println(categoryDao.findAll());
    }

}
测试类代码

测试结果:

 

 

 

 

 

 

 

 

 

posted @ 2022-04-28 20:33  __fairy  阅读(97)  评论(0编辑  收藏  举报