使用 MyBatis 来查询

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

使用 MyBatis 来查询数据库

创建一个数据库和表

        在对数据库操作的前提得现有一个数据库,下面我提供了一个数据库:

-- 创建数据库
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 timestamp default current_timestamp,
    updatetime timestamp default current_timestamp,
    `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 timestamp default current_timestamp,
    updatetime timestamp default current_timestamp,
    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 timestamp default current_timestamp,
		updatetime timestamp default current_timestamp,
  	uid int
)default charset 'utf8mb4';

-- 添加一个用户信息
INSERT INTO `mycnblog`.`userinfo` (`id`, `username`, `password`, `photo`, `createtime`, `updatetime`, `state`) VALUES 
(1, 'admin', 'admin', '', '2023-12-06 17:10:48', '2023-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);

创建项目

设置配置文件

编写代码

添加用户实体类

这里实体类的属性需要和数据库的字段对应。

package com.example.mybatisdemo.entity;
import lombok.Data;
import java.time.LocalDateTime;

@Data
public class UserEntity {
    private Integer id;
    private String username;
    private String password;
    private String photo;
    private LocalDateTime createtime;
    private LocalDateTime updatetime;
    private Integer state;
}

添加 mapper 接口

数据持久层的接口定义:

添加 UserMapper.xml

数据持久层的实现,UserMapper.xml 查询所有⽤户的具体实现 SQL:

添加 Service 服务层

package com.example.mybatisdemo.service;

import com.example.mybatisdemo.entity.UserEntity;
import com.example.mybatisdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<UserEntity> getAll() {
        return userMapper.getAll();
    }
}

添加 Controller 控制器层

package com.example.mybatisdemo.controller;

import com.example.mybatisdemo.entity.UserEntity;
import com.example.mybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getAll")
    public List<UserEntity> getAll() {
        return userService.getAll();
    }
}

运行

小结:

相关推荐

  1. springboot的mybatis使用CONCAT模糊查询

    2024-06-12 20:08:02       26 阅读
  2. SpringBoot使用mybatis-plus分页查询无效解决方案

    2024-06-12 20:08:02       71 阅读
  3. MyBatis之关联查询

    2024-06-12 20:08:02       53 阅读
  4. 2. Mybatis案例(查询

    2024-06-12 20:08:02       65 阅读
  5. Mybatis多表查询

    2024-06-12 20:08:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-12 20:08:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 20:08:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 20:08:02       87 阅读
  4. Python语言-面向对象

    2024-06-12 20:08:02       96 阅读

热门阅读

  1. MySQL 运算符绕过

    2024-06-12 20:08:02       26 阅读
  2. 【react】useState 使用指南

    2024-06-12 20:08:02       32 阅读
  3. linux常用的基础命令

    2024-06-12 20:08:02       24 阅读
  4. CountDownLatch闭锁

    2024-06-12 20:08:02       33 阅读
  5. uniapp怎么实现条形码

    2024-06-12 20:08:02       34 阅读
  6. 7. 通配符和正则表达式

    2024-06-12 20:08:02       21 阅读
  7. Kotlin可空类型与非空类型以及`lateinit` 的作用

    2024-06-12 20:08:02       36 阅读
  8. Python 实现简单的超图

    2024-06-12 20:08:02       31 阅读
  9. MySQL练习题

    2024-06-12 20:08:02       30 阅读
  10. spring 常用注解

    2024-06-12 20:08:02       30 阅读
  11. torchvision笔记 torchvision.ops.sigmoid_focal_loss

    2024-06-12 20:08:02       31 阅读