编写若依的商品CRUD五个接口并实现前后端联通
参考:https://www.bilibili.com/video/BV1Nh4y1s75p/?spm_id_from=333.337.search-card.all.click&vd_source=0dcc3acedc4fc1c77ebc9bfbcbd12cec
一、结果展示:
1、新建的商品列表模块
2、查询所有商品功能
3、C:新增
4、D
5、U
6、R
二、代码
1、前端
good.js
```javascript
import request from '@/utils/request'
// 查询商品列表
export function listPost(query) {
return request({
url: '/goods/good/list',
method: 'get',
params: query
})
}
// 查询商品详细
export function getPost(id) {
return request({
url: '/goods/good/' + id,
method: 'get'
})
}
// 新增商品
export function addPost(data) {
return request({
url: '/goods/good',
method: 'post',
data: data
})
}
// 修改商品
export function updatePost(data) {
return request({
url: '/goods/good',
method: 'put',
data: data
})
}
// 删除商品
export function delPost(id) {
return request({
url: '/goods/good/' +id,
method: 'delete'
})
}
index.vue仿照post的index.vue进行改写,注意细节,代码多,不放了。
细节改天写
2、后端
在创建ruoyi-goods模块的过程中重视命名和代码的规范性,和其余功能保持一致性。
规范化的重要性:一份扫描即可扫描所有文件
src/main/java/com/ruoyi/web/controller/goods/GoodsGoodController.java
package com.ruoyi.web.controller.goods;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.goods.domain.GoodsGood;
import com.ruoyi.goods.service.IGoodsGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static com.ruoyi.common.utils.PageUtils.startPage;
@RestController
@RequestMapping("goods/good")
public class GoodsGoodController extends BaseController {
@Autowired
private IGoodsGoodService goodsGoodService;
/**
* 查找商品列表
*/
@PreAuthorize("@ss.hasPermi('goods:good:list')")
@GetMapping("/list")
public TableDataInfo list(GoodsGood goodsGood)
{
startPage();
List<GoodsGood> list = goodsGoodService.selectGoodsGoodList(goodsGood);
return getDataTable(list);
}
/**
* 查询商品详情
*/
@PreAuthorize("@ss.hasPermi('goods:good:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
return success(goodsGoodService.selectGoodsGoodById(id));
}
/**
* 新建商品详情
* @return
*/
@PreAuthorize("@ss.hasPermi('goods:good:add')")
@Log(title = "【新建商品详情】", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody GoodsGood goodsGood)
{
return toAjax(goodsGoodService.insertGoodsGood(goodsGood));
}
/**
* 修改商品详情
*/
@PreAuthorize("@ss.hasPermi('goods:good:edit')")
@Log(title = "【修改商品详情】", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody GoodsGood goodsGood)
{
return toAjax(goodsGoodService.updateGoodsGood(goodsGood));
}
/**
* 删除商品详情
*/
@PreAuthorize("@ss.hasPermi('goods:good:remove')")
@Log(title = "【删除商品详情】", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids)
{
return toAjax(goodsGoodService.deleteGoodsGoodByIds(ids));
}
数据库表如下图所示
ruoyi-goods模块中的内容使用若依的代码生成功能写好,会自动按照数据库中的字段生成代码,将生成的代码仿照ruoyi-system的post进行修改。
细节改天补充,先写swagger
早上:先肝功能 冲!
傍晚:肝多了(…)