SpringBoot构建电商基础秒杀项目总结-交易模块开发
Posted hequnwang10
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot构建电商基础秒杀项目总结-交易模块开发相关的知识,希望对你有一定的参考价值。
秒杀模块开发
一、秒杀模型管理——活动模型创建
1、pom.xml
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.1</version>
</dependency>
2、sql
DROP TABLE IF EXISTS `promo`;
CREATE TABLE `promo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`promo_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`start_date` datetime NOT NULL DEFAULT '2018-12-31 23:59:59' COMMENT '默认为当前时间',
`item_id` int(11) NOT NULL DEFAULT '0',
`promo_item_price` double(10,2) NOT NULL DEFAULT '0.00',
`end_date` datetime NOT NULL DEFAULT '2099-12-31 23:59:59',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DROP TABLE IF EXISTS `order_info`;
CREATE TABLE `order_info` (
`id` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(11) NOT NULL DEFAULT '0',
`item_id` int(11) NOT NULL DEFAULT '0',
`item_price` double(10,2) NOT NULL DEFAULT '0.00',
`amount` int(11) NOT NULL DEFAULT '0',
`order_price` double(10,2) NOT NULL DEFAULT '0.00',
`promo_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
3、PromoModel.java
import org.joda.time.DateTime;
import java.math.BigDecimal;
public class PromoModel {
private Integer id;
//秒杀活动状态:1表示还未开始,2表示正在进行,3表示已结束
private Integer status;
//秒杀活动名称
private String promoName;
//秒杀活动的开始时间
private DateTime startDate;
//秒杀活动的结束时间
private DateTime endDate;
//秒杀活动的适用商品
private Integer itemId;
//秒杀活动的商品价格
private BigDecimal promoItemPrice;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getPromoName() {
return promoName;
}
public void setPromoName(String promoName) {
this.promoName = promoName;
}
public DateTime getStartDate() {
return startDate;
}
public void setStartDate(DateTime startDate) {
this.startDate = startDate;
}
public DateTime getEndDate() {
return endDate;
}
public void setEndDate(DateTime endDate) {
this.endDate = endDate;
}
public Integer getItemId() {
return itemId;
}
public void setItemId(Integer itemId) {
this.itemId = itemId;
}
public BigDecimal getPromoItemPrice() {
return promoItemPrice;
}
public void setPromoItemPrice(BigDecimal promoItemPrice) {
this.promoItemPrice = promoItemPrice;
}
}
mybatis-generator.xml
<table tableName="promo" domainObjectName="PromoDO"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" ></table>
重新生成一次
二、活动模型与商品模型结合
1、PromoService.java接口
public interface PromoService {
//根据itemId获取即将进行的或正在进行的秒杀活动
PromoModel getPromoByItemId(Integer itemId);
}
2、PromoServiceImpl.java
@Service
public class PromoServiceImpl implements PromoService {
@Autowired
private PromoDOMapper promoDOMapper;
@Override
public PromoModel getPromoByItemId(Integer itemId) {
//获取商品对应的秒杀信息
PromoDO promoDO = promoDOMapper.selectByItemId(itemId);
//dataobject->model
PromoModel promoModel = convertFromDataObject(promoDO);
if (promoModel == null) {
return null;
}
//判断当前时间是否秒杀活动即将开始或正在进行
DateTime now = new DateTime();
if (promoModel.getStartDate().isAfterNow()) {
promoModel.setStatus(1);
} else if (promoModel.getEndDate().isBeforeNow()) {
promoModel.setStatus(3);
} else {
promoModel.setStatus(2);
}
return promoModel;
}
private PromoModel convertFromDataObject(PromoDO promoDO) {
if (promoDO == null) {
return null;
}
PromoModel promoModel = new PromoModel();
BeanUtils.copyProperties(promoDO, promoModel);
promoModel.setPromoItemPrice(new BigDecimal(promoDO.getPromoItemPrice()));
promoModel.setStartDate(new DateTime(promoDO.getStartDate()));
promoModel.setEndDate(new DateTime(promoDO.getEndDate()));
return promoModel;
}
}
3、PromoDOMapper.java
PromoDO selectByItemId(Integer itemId);
4、PromoDOMapper.xml
<mapper namespace="com.miaoshaproject.dao.PromoDOMapper">
<resultMap id="BaseResultMap" type="com.miaoshaproject.dataobject.PromoDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri May 28 18:01:34 CST 2021.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="promo_name" jdbcType="VARCHAR" property="promoName" />
<result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
<result column="item_id" jdbcType="INTEGER" property="itemId" />
<result column="promo_item_price" jdbcType="DOUBLE" property="promoItemPrice" />
<result column="end_date" jdbcType="TIMESTAMP" property="endDate" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri May 28 18:01:34 CST 2021.
-->
id, promo_name, start_date, item_id, promo_item_price, end_date
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri May 28 18:01:34 CST 2021.
-->
select
<include refid="Base_Column_List" />
from promo
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByItemId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from promo
where item_id = #{itemId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri May 28 18:01:34 CST 2021.
-->
delete from promo
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.miaoshaproject.dataobject.PromoDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri May 28 18:01:34 CST 2021.
-->
insert into promo (id, promo_name, start_date,
item_id, promo_item_price, end_date
)
values (#{id,jdbcType=INTEGER}, #{promoName,jdbcType=VARCHAR}, #{startDate,jdbcType=TIMESTAMP},
#{itemId,jdbcType=INTEGER}, #{promoItemPrice,jdbcType=DOUBLE}, #{endDate,jdbcType=TIMESTAMP}
)
</insert>
<insert id="insertSelective" parameterType="com.miaoshaproject.dataobject.PromoDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri May 28 18:01:34 CST 2021.
-->
insert into promo
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="promoName != null">
promo_name,
</if>
<if test="startDate != null">
start_date,
</if>
<if test="itemId != null">
item_id,
</if>
<if test="promoItemPrice != null">
promo_item_price,
</if>
<if test="endDate != null">
end_date,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="promoName != null">
#{promoName,jdbcType=VARCHAR},
</if>
<if test="startDate != null">
#{startDate,jdbcType=TIMESTAMP},
</if>
<if test="itemId != null">
#{itemId,jdbcType=INTEGER},
</if>
<if test="promoItemPrice != null">
#{promoItemPrice,jdbcType=DOUBLE},
</if>
<if test="endDate != null">
#{endDate,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.miaoshaproject.dataobject.PromoDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri May 28 18:01:34 CST 2021.
-->
update promo
<set>
<if test="promoName != null">
promo_name = #{promoName,jdbcType=VARCHAR},
</if>
<if test="startDate != null">
start_date = #{startDate,jdbcType=TIMESTAMP},
</if>
<if test="itemId != null">
item_id = #{itemId,jdbcType=INTEGER},
</if>
<if test="promoItemPrice != null">
promo_item_price = #{promoItemPrice,jdbcType=DOUBLE},
</if>
<if test="endDate != null">
end_date = #{endDate,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.miaoshaproject.dataobject.PromoDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri May 28 18:01:34 CST 2021.
-->
update promo
set promo_name = #{promoName,jdbcType=VARCHAR},
start_date = #{startDate,jdbcType=TIMESTAMP},
item_id = #{itemId,jdbcType=INTEGER},
promo_item_price = #{promoItemPrice,jdbcType=DOUBLE},
end_date = #{endDate,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
5、ItemVO.java
//用于前端展示
public class ItemVO {
private Integer id;
//商品名称
private String title;
//商品价格
private BigDecimal price;
//商品的库存
private Integer stock;
//商品的描述
private String description;
//商品的销量
private Integer sales;
//商品描述图片的url
private String imgUrl;
//商品是否在秒杀活动中,以及对应的状态:0表示没有秒杀活动,1表示秒杀活动等待开始,2表示进行中
private Integer promoStatus;
//秒杀活动价格
private BigDecimal promoPrice;
//秒杀活动id
private Integer promoId;
//秒杀活动开始时间
private String startDate;
//get set 方法
}
6、ItemController.java
@Controller("/item")
@RequestMapping("/item")
//跨域请求中,不能做到session共享
@CrossOrigin(origins = "http://localhost:63343",allowCredentials = "true",allowedHeaders="*")
public class ItemController extends BaseController{
@Autowired
private ItemService itemService;
//创建商品的controller以上是关于SpringBoot构建电商基础秒杀项目总结-交易模块开发的主要内容,如果未能解决你的问题,请参考以下文章