1、环境配置
工具:
- IntelliJ IDEA 2021
- Mysql 5.7
- postman (接口测试工具)
pom.xml
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
...
application.yml
server:
port: 81
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/studentsmanager?serverTimezone=UTC
username: root
password: 123456
mybatis-plus:
global-config:
db-config:
# 数据表的前缀
table-prefix: s_
configuration:
# 开启MP的运行日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2、实体类
Student.class
package com.vvu.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class Student {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private String gender;
private Integer num;
}
@Data 是lombok的注解,里面有get(),set()等方法,就不用自己再去写一遍了。
数据库
DROP TABLE IF EXISTS `s_student`;
CREATE TABLE `s_student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
`gender` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`num` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1565032453 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
INSERT INTO `s_student` VALUES (1, 'lily', 17, '1', 75);
INSERT INTO `s_student` VALUES (2, 'nanxy', 17, '1', 75);
INSERT INTO `s_student` VALUES (3, 'jonh', 18, '2', 75);
INSERT INTO `s_student` VALUES (4, 'mike', 16, '2', 75);
INSERT INTO `s_student` VALUES (5, 'alidrida', 17, '1', 75);
INSERT INTO `s_student` VALUES (6, 'lisi', 17, '2', 75);
INSERT INTO `s_student` VALUES (7, 'samdy', 16, '1', 75);
INSERT INTO `s_student` VALUES (9, 'tom', 17, '2', 74);
SET FOREIGN_KEY_CHECKS = 1;
3、数据层
在dao包下创建StuDao.interface
package com.vvu.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.vvu.domain.Student;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StuDao extends BaseMapper<Student> {
}
注解@Mapper 的作用是为了把这个dao交給Spring管理,不用再写mapper映射文件,并且可以自动根据@Mapper注解生成一个实现类。
BaseMapper<>是mybatis-plus提供的接口,里面有很多现成的增删改查方法,可以直接调用,括号里面填写的是自己的实体类名字。
service层
在service包下创建StuService接口
package com.vvu.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.vvu.domain.Student;
public interface StuService extends IService<Student>{
}
IService<>是mybatis-plus的接口,实现CRUD的时候减少了相对应的代码工作量,括号里面填写的是自己需要操作的实体类名字。也可以自己定义crud的方法,这里知识简单应用就不写了。
在sevice包写创建Impl包,在这个包中创建StuService的实现类StuServiceImpl.class
package com.vvu.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vvu.dao.StuDao;
import com.vvu.domain.Student;
import com.vvu.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StuServiceImpl extends ServiceImpl<StuDao,Student> implements StuService {
}
@Service注解,标记当前类是一个service类,会自动注入到spring容器中,不需要再配置文件中定义bean了。
ServiceImpl<>,是mybatisplus的一个类,里面有丰富的crud方法,节省代码时间。括号中<数据层接口名字,实体类名字>。
4、controller层
在controller包下创建StuController.class
package com.vvu.controller;
import com.vvu.domain.Student;
import com.vvu.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/student")
public class StuController {
@Autowired
private StuService service;
//查询所有
@GetMapping
public List<Student> getAll(){
return service.list();
}
//根据id查询
@GetMapping("{id}")
public Student getById(@PathVariable Integer id){
return service.getById(id);
}
//根据id删除行
@DeleteMapping("{id}")
public Boolean delById(@PathVariable Integer id){
return service.removeById(id);
}
//增加
@PostMapping
public Boolean save(@RequestBody Student student){
return service.save(student);
}
//更改
@PutMapping
public Boolean update(@RequestBody Student student){
return service.updateById(student);
}
}
5、测试
打开Stu02Application 启动类启动项目。箭头指的是项目启动的端口。
打开postman工具
根据id=2 查询数据
http://localhost:81/student/2
输入http://localhost:81/student/2,点击Send。
查询全部数据 测试
http://localhost:81/student
增加数据 测试
{
"id": 10,
"name": "李四",
"age": 17,
"gender": "1",
"num": 75
}
打开navicat,刷新表数据。添加成功了。
更改功能测试
{
"id": 10,
"name": "小红",
"age": 16,
"gender": "2",
"num": 75
}
打开navicat,刷新表,修改成功。
根据id=10 删除功能测试
http://localhost:81/student/10
删除成功!
6、总结
此案例主要是利用mybatis-plus提供的封装好的crud方法进行快速便捷开发,上面只是mybatis-plus的简单运用。