1.创建SpringBoot项目并引入MybatisPlus及相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springBootProject01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBootProject01</name>
<description>springBootProject01</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.18</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.4.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.配置yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring
username: root
password:
#处理实体类与数据库表不一致的问题
mybatis-plus:
global-config:
db-config:
table-prefix: tb_
3.编写实体类代码(Model)
package com.by.model;
import lombok.Data;
@Data
public class student {
private Integer id;
private String name;
private String sex;
private String birthday;
private String address;
private String hobby;
private String telephone;
private String introduce;
}
4.编写数据层代码(Dao)
package com.by.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.by.model.student;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface studentMapper extends BaseMapper<student> {
}
5.编写业务层代码(Service)接口类
package com.by.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.by.model.student;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface studentService extends IService<student> {
// 添加数据
boolean saveStu(student student);
// 根据id修改数据
boolean updateById(student student);
//根据id删除数据
boolean deleteById(int id);
//查询一条数据
student selectOne(int id);
// 查询全部数据
List<student> selectAll();
}
6.编写业务层代码(Service)实现类
package com.by.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.mapper.studentMapper;
import com.by.model.student;
import com.by.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class studentServiceImpl extends ServiceImpl<studentMapper, student> implements studentService {
@Autowired
private studentMapper studentMapper;
@Override
public boolean saveStu(student student) {
return studentMapper.insert(student)>0;
}
@Override
public boolean updateById(student student) {
return studentMapper.updateById(student)>0;
}
@Override
public boolean deleteById(int id) {
return studentMapper.deleteById(id)>0;
}
@Override
public student selectOne(int id) {
return studentMapper.selectById(id);
}
@Override
public List<student> selectAll() {
return studentMapper.selectList(null);
}
}
7.编写表现层代码(Controller)
package com.by.controller;
import com.by.model.student;
import com.by.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api")
public class studentController {
@Autowired
private studentService studentService;
//根据id查询数据
@GetMapping("{id}")
student selectById(@PathVariable int id) {
return studentService.selectOne(id);
}
//查询所有数据
@GetMapping
List<student> selectAll() {
return studentService.selectAll();
}
// 新增数据
@PostMapping
Boolean insert(@RequestBody student student) {
return studentService.saveStu(student);
}
// 根据id删除数据
@DeleteMapping("{id}")
Boolean delete(@PathVariable int id) {
return studentService.deleteById(id);
}
// 根据id修改数据v
@PutMapping
Boolean update(@RequestBody student student) {
return studentService.updateById(student);
}
}