• MyBatis详细介绍


    MyBatis详细介绍

    二话不分三次说,先来看图
    在这里插入图片描述

    MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。Mybatis是ORM框架,能够帮助我们完成面向对象到关系型数据库的映射, 不用写复杂的JDBC 代码以及进行设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(PlainOldJavaObjects,普通老式Java 对象)为数据库中的记录。使用Mybatis框架能否很好的提高我们开发的速度和较少不必要的复杂代码。

    不过在此之前我们先来了解一下ORM

    什么是ORM

    Object-Relationl Mapping,它的作用是在关系型数据库和对象之间作一个映射,这样,我们在具体的操作数据库的时候,就不需要再去和复杂的SQL语句打交道,只要像平时操作对象一样操作它就可以了 。
    在这里插入图片描述

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

    Mybatis基本依赖配置

    maven项目加入以来即可

    Mybatis的依赖配置

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.4</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    MySQL的依赖配置

    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    这是在项目开始时创建添加依赖的,难道在过程中如果想添加只能一个一个敲吗?当然不必:
    介绍一个插件:
    在这里插入图片描述
    这是干啥的呢,就是在过程中添加依赖的:
    在pom.xml中右击点击生成:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    诺,到了熟悉的界面。。。。

    配置连接字符串和MyBatis

    配置连接字符串

    在这里插入图片描述
    当然贴心的为大家准备好了代码:

    datasource:
        url: jdbc:mysql://127.0.0.1:3306/java_gobang?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: wjhyd521
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    配置 MyBatis 中的 XML 路径

    在这里插入图片描述

    mybatis:
      mapper-locations: classpath:mapper/**Mapper.xml
      
    
    • 1
    • 2
    • 3

    举个栗子

    先来建一个数据库:

    -- 创建数据库
    drop database if exists mycnblog;
    create database mycnblog DEFAULT CHARACTER SET utf8;
    
    -- 使用数据数据
    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
    );
    
    -- 创建文章表
    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
    );
    
    -- 创建视频表
    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
    );
    
    -- 添加一个用户信息
    INSERT INTO `mycnblog`.`userinfo` (`id`, `username`, `password`, `photo`, `createtime`, `updatetime`, `state`) VALUES
    (1, 'admin', 'admin', '', '2021-12-06 17:10:48', '2021-12-06 17:10:48', 1);
    
    -- 文章添加测试数据
    insert into articleinfo(title,content,uid) values('Java','Java正文',1);
    
    -- 添加视频
    insert into videoinfo(vid,title,url,uid) values(1,'java title','http://www.baidu.com',1);
    
    • 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

    MyBatis信息配置

    加下来我们根据配置配置好项目的信息:

    在这里插入图片描述

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