第一步:引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
第二步:根据自己的需求创建三层架构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/08ceb975257c5c272c99a095992d3f74.png)
第三步:配置数据源
server:
port: 80
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/goods?serverTimeZone=UTF&characterEncoding-utf-8
username: root
password: root
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
第四步:编写后端接口层
package com.controller;
import com.pojo.GoodsInfo;
import com.pojo.vo.VoGoodsAndType;
import com.service.impl.GoodsServiceImpl;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class GoodsController {
@Autowired
private GoodsServiceImpl goodsService;
@GetMapping("/select")
@ResponseBody
public R select(){
List<VoGoodsAndType> goods = goodsService.selectAll();
return R.ok().code(200).data("goods",goods);
}
@GetMapping("/delete")
@ResponseBody
public R delete(@RequestParam("id")Integer id){
Integer i = goodsService.deleteById(id);
return R.ok().code(200).data("i",i);
}
@GetMapping("/insert")
@ResponseBody
public R insert(@RequestParam("name")String name,@RequestParam("price")Integer price,@RequestParam("type")Integer type,@RequestParam("status")Integer status,@RequestParam("spanme")String spname){
GoodsInfo good = new GoodsInfo(0,name, price, type, status, spname);
Integer i = goodsService.add(good);
return R.ok().code(200).data("i",i);
}
@GetMapping("/update")
@ResponseBody
public R update(@RequestParam("id")Integer id,@RequestParam("name")String name,@RequestParam("price")Integer price,@RequestParam("type")Integer type,@RequestParam("status")Integer status,@RequestParam("spanme")String spname){
GoodsInfo good = new GoodsInfo(id, name, price, type, status, spname);
Integer i = goodsService.update(good);
return R.ok().code(200).data("i",i);
}
}
最后测试就行啦
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fb12ffcaf6759cc4419b0f083e89ba53.png)