如何用springboot+mybatis+vue实现简单的增删改查
开发工具
需要准备工具如下:
- IntelliJ IDEA 2020.2.3 ;
- Mysql8.0.12;
- jdk1.8;
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</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>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
项目结构
连接数据库
在resources文件夹下新建一个applicatio.yml文件
server:
port: 8081
servlet:
context-path: /hhh
resources:
static-locations: classpath:/imgs
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/yaofang?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.hnucm.demo.pojo
logging:
level:
com:
hnucm:
springboot: debug
引入异常处理类和返回数据封装类
结构如下:
异常处理类:
package com.hnucm.demo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org