• MyBatis环境配置及查询操作


    1. MyBatis 是什么?

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

    通俗的来说:MyBatis 它是一个优秀的 ORM(对象关系映射)持久层框架

    特点:灵活

    2. 如何创建 MyBatis项目

    2.1 新建项目添加 MyBatis 依赖

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    2.2 配置数据库连接字符串和 MyBatis

    首先创建三个配置文件,分别如下:
    在这里插入图片描述

    在这里插入图片描述

    2.21 配置了数据库的连接信息

    application-dev.yml 中 请添加如下内容:

    # 开发环境的配置文件
    
    # 第一步:配置数据库的连接
    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/mycnblog?characterEncoding=utf8
        username: root   # 用户名(默认是root)
        password: 707703 # 自己的密码
        driver-class-name: com.mysql.cj.jdbc.Driver # mysql的驱动名称
    
    
    # 开启 MyBatis SQL 打印
    logging:
      level:
        com:
          example:
            demo: debug
    mybatis:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    2.22 配置 MyBatis 的XML 保存路径

    application.yml 中添加如下内容

    # 配置当前运行的环境(配置文件)
    
    spring:
      profiles:
        active: dev # 使用开发环境的配置文件
    
    
    # 配置 MyBatis 的 XML 的保存路径
    # 在resource底下新建一个文件夹,用来放 XML
    mybatis:
      mapper-locations: classpath:mybatis/**Mapper.xml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    3. 添加业务代码

    我们配置好环境之后,按照后端开发的工程思路,也就是下面的流程来实现 MyBatis 查询所有用户功能

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    4. 使用 MyBatis 实现查询操作

    4.1 在数据库中构建表

    打开 mysql 复制粘贴 如下代码:

    -- 创建数据库
    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';
    
    -- 添加一个用户信息
    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
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    4.2 添加实体类

    先添加用户的实体类:

    package com.example.demo.model;
    import lombok.Data;
    /**
     * 普通的实体类
     */
    @Data // 组合注解 包含了 get set 和 toString 方法
    public class UserInfo {
        private int id;
        private String username;
        private String password;
        private String photo;
        private String createtime;
        private String updatetime;
        private String state;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4.3 添加 mapper 接口

    数据化持久层的接口定义:

    package com.example.demo.mapper;
    import com.example.demo.model.UserInfo;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    
    
    @Mapper // 加上这个注解,使得 interface 从普通的 interface 变成了
    // mybatis 的一个 interface
    public interface UserMapper {
        
        // 根据用户 id 查询用户
        public UserInfo getUserById(@Param("id") Integer id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4.4 添加 UserMapper.xml

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

    
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    UserMapper.xml 查询用户的集体实现 SQL:

    
    
    
    
        
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    4.5 添加 Service

    服务层代码实现如下:

    package com.example.demo.service;
    
    import com.example.demo.mapper.UserMapper;
    import com.example.demo.model.UserInfo;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    
    @Service
    public class UserService {
        
        @Resource
        private UserMapper userMapper;
        
        public UserInfo getUserById(Integer id) {
            return userMapper.getUserById(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4.6 添加 Controller

    控制层的实现代码如下:

    package com.example.demo.controller;
    
    import com.example.demo.model.UserInfo;
    import com.example.demo.service.UserService;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Resource
        private UserService userService;
        @RequestMapping("/getuserbyid")
        public UserInfo getUserById(Integer id) {
            if(id == null) return null;
            return userService.getUserById(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4.7 查询并返回到前端

    不输入 id 如下
    在这里插入图片描述

    输入 id = 1 如下
    在这里插入图片描述

  • 相关阅读:
    uwb模块实现人员精确定位,超宽带脉冲技术方案,实时厘米级定位应用
    个人开源项目如何上传maven中央仓库
    一文搞懂百度强推的Redis天花板笔记,原来数据库是这样理解的
    Qt样式表应用
    js如何实现一个简单的节流函数?
    PHP生成二维码带图标代码实例
    《HarmonyOS开发 – OpenHarmony开发笔记(基于小型系统)》第5章 WiFi联网(STA模式)
    【FreeRTOS-----笔记(基础)】
    C++中的深拷贝和浅拷贝
    JavaScript的面向对象
  • 原文地址:https://blog.csdn.net/Biteht/article/details/125978937