秒杀项目之秒杀商品操作
作者:互联网
一、秒杀商品显示
1、使用生成器生成对应的表
记得在每个mapper类加入注解供spring扫描:
@Repository
2、后端写得到秒杀商品的方法
①、建实体类vo
用于连表查询,得到商品名字
package com.example.seckill.vo;
import com.example.seckill.pojo.SeckillGoods;
import lombok.Data;
@Data
public class SeckillGoodsVo extends SeckillGoods {
private String goodsName;
}
②、在SexkillGoodsMapper.xml文件中定义sql
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.seckill.mapper.SeckillGoodsMapper">
<select id="queryAll" resultType="com.example.seckill.vo.SeckillGoodsVo">
select sg.*,
g.goods_name
from t_seckill_goods sg,
t_goods g
where sg.goods_id = g.gid;
</select>
</mapper>
③、在mapper中定义
package com.example.seckill.mapper;
import com.example.seckill.pojo.SeckillGoods;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.seckill.vo.SeckillGoodsVo;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* <p>
* 秒杀商品信息表 Mapper 接口
* </p>
*
* @author lv
* @since 2022-03-19
*/
@Repository
public interface SeckillGoodsMapper extends BaseMapper<SeckillGoods> {
List<SeckillGoodsVo> queryAll();
}
④、service层与controller层
service:
ISeckillGoodsService:
package com.example.seckill.service;
import com.example.seckill.pojo.SeckillGoods;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.seckill.util.response.ResponseResult;
import com.example.seckill.vo.SeckillGoodsVo;
import java.util.List;
/**
* <p>
* 秒杀商品信息表 服务类
* </p>
*
* @author lv
* @since 2022-03-19
*/
public interface ISeckillGoodsService extends IService<SeckillGoods> {
ResponseResult<List<SeckillGoodsVo>> queryAll();
}
SeckillGoodsServiceImpl:
package com.example.seckill.service.impl;
import com.example.seckill.pojo.SeckillGoods;
import com.example.seckill.mapper.SeckillGoodsMapper;
import com.example.seckill.service.ISeckillGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.seckill.util.response.ResponseResult;
import com.example.seckill.vo.SeckillGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
* 秒杀商品信息表 服务实现类
* </p>
*
* @author lv
* @since 2022-03-19
*/
@Service
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements ISeckillGoodsService {
@Autowired
private SeckillGoodsMapper seckillGoodsMapper;
@Override
public ResponseResult<List<SeckillGoodsVo>> queryAll() {
List<SeckillGoodsVo> list= seckillGoodsMapper.queryAll();
return ResponseResult.success(list);
}
}
controller:
SeckillGoodsController:
package com.example.seckill.controller;
import com.example.seckill.service.ISeckillGoodsService;
import com.example.seckill.util.response.ResponseResult;
import com.example.seckill.vo.SeckillGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <p>
* 秒杀商品信息表 前端控制器
* </p>
*
* @author lv
* @since 2022-03-19
*/
@RestController
@RequestMapping("/seckillGoods")
public class SeckillGoodsController {
@Autowired
private ISeckillGoodsService seckillGoodsService;
@RequestMapping("/queryAll")
public ResponseResult<List<SeckillGoodsVo>> queryAll(){
return seckillGoodsService.queryAll();
}
}
得到秒杀商品数据:
3、前端显示数据
①、编辑跳转秒杀界面
goodList.ftl:
<!DOCTYPE html>
<html lang="en">
<head>
<#include "../common/head.ftl">
<style>
.layui-this{
background: deepskyblue !important;
}
</style>
</head>
<body class="layui-container layui-bg-orange">
<div class="layui-tab">
<ul class="layui-tab-title">
<li class="layui-this">普通商品</li>
<li>秒杀商品</li>
</ul>
<#-- 普通商品-->
<div class="layui-tab-content">
<div class="layui-tab-item layui-show">
<div class="layui-form-item">
<label class="layui-form-label">搜索栏</label>
<div class="layui-input-inline">
<input type="text" id="normal_name" name="text" placeholder="请输入搜索内容" class="layui-input">
</div>
<div class="layui-input-inline">
<button class="layui-btn layui-btn-primary" id="normal_search">标签:goods,seckill,title,商品,秒杀,import,操作,com,example
来源: https://blog.csdn.net/weixin_60389087/article/details/123601288