• MyBatis 查询数据库


    1.什么是 MyBatis

    • MyBatis 是一款优秀的持久层框架,它支持 自定义 SQL存储过程 以及 高级映射
    • MyBatis 去除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
    • MyBatis 可以通过简单的 XML注解 来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

    简单来说 MyBatis更简单完成程序和数据库交互的工具,也就是更简单的操作和读取数据库工具。

    2.MyBatis 环境搭建

    开始搭建 MyBatis 之前,MyBatis 在整个框架中的定位,框架交互流程图:
    在这里插入图片描述
    MyBatis 也是一个 ORM 框架,ORM(Object Relational Mapping),即对象关系映射。在面向对象编程语言中,将关系型数据库中的数据与对象建立起映射关系,进而自动的完成数据与对象的互相转换

    1. 将输入数据(即传入对象)+SQL 映射成原生 SQL
    2. 将结果集映射为返回对象,即输出对象

    ORM 把数据库映射为对象

    1. 数据库表(table)–> 类(class)
    2. 记录(record,行数据)–> 对象(object)
    3. 字段(field) --> 对象的属性(attribute)

    一般的 ORM 框架,会将数据库模型的每张表都映射为一个 Java 类。 也就是说使用 MyBatis 可以像操作对象一样来操作数据库中的表,可以实现对象和数据库表之间的转换。

    2.1 创建数据库和表

    -- 创建数据库
    drop database if exists mycnblog;
    create database mycnblog DEFAULT CHARACTER SET utf8mb4;
    
    -- 使用数据数据
    use mycnblog;
    
    -- 创建表[用户表]
    drop table if exists  userinfo;
    create table userinfo(
        id int primary key auto_increment,
        username varchar(100) not null,
        password varchar(32) not null,
        photo varchar(500) default '',
        createtime datetime default now(),
        updatetime datetime default now(),
        `state` int default 1
    ) default charset 'utf8mb4';
    
    -- 创建文章表
    drop table if exists  articleinfo;
    create table articleinfo(
        id int primary key auto_increment,
        title varchar(100) not null,
        content text not null,
        createtime datetime default now(),
        updatetime datetime default now(),
        uid int not null,
        rcount int not null default 1,
        `state` int default 1
    )default charset 'utf8mb4';
    
    -- 创建视频表
    drop table if exists videoinfo;
    create table videoinfo(
      	vid int primary key,
      	`title` varchar(250),
      	`url` varchar(1000),
    		createtime datetime default now(),
    		updatetime datetime default now(),
      	uid int
    )default charset 'utf8mb4';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    2.2 添加 MyBatis 框架支持

    ① 在已有的项目中添加:

    
    <dependency>
    	<groupId>org.mybatis.spring.bootgroupId>
    	<artifactId>mybatis-spring-boot-starterartifactId> 
    	<version>2.1.4version>
    dependency>
    
    <dependency>
    	<groupId>mysqlgroupId>
    	<artifactId>mysql-connector-javaartifactId> 
    	<scope>runtimescope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ② 在创建新项目时添加:

    在这里插入图片描述

    2.3 配置数据库连接和MyBatis

    在 application.yml 配置数据库连接:

    # 配置数据库连接
    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1/mycnblog?charsetEncoding=utf8
        username: root
        password: 12345678
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    配置 MyBatis 中的 XML 路径:

    # 设置Mybatis的xml保存路径
    mybatis:
      mapper-locations: classpath:mybatis/**Mapper.xml
    
    • 1
    • 2
    • 3

    2.4 添加代码

    按照后端开发的工程思路来实现 MyBatis 查询所有用户的功能:
    在这里插入图片描述
    目录结构:
    在这里插入图片描述

    2.4.1 添加实体类

    添加用户的实体类:

    @Setter
    @Getter
    @ToString
    public class UserInfo {
        private int id;
        private String username;
        private String password;
        private String photo;
        private String createtime;
        private String updatetime;
        private int state;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.4.2 添加 mapper 接口

    数据持久层的接口定义:

    @Mapper
    public interface UserMapper {
    	public List<UserInfo> getAll(); 
    }
    
    • 1
    • 2
    • 3
    • 4

    2.4.3 添加 UserMapper.xml

    数据持久层的实现,mybatis 的固定 xml 格式:

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.demo.mapper.UserMapper"> 
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    UserMapper.xml 查询所有用户的具体实现 SQL:

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.demo.mapper.UserMapper">
    	<select id="getAll" resultType="com.example.demo.model.UserInfo"> 
            select * from userinfo
    	select> 
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    标签说明:

    • 标签:需要指定 namespace 属性,表示命名空间,值为 mapper 接口的全限定名,包括全包名.类名。