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();
}
}