该整是在做秒杀业务写的代码,测试通过,后续将秒杀程序做更新 ,做一个记录,在整合的时候也遇到了小小的bug,希望后面做的朋友不要入坑!
一、到spring官网创建spring-boot模板(当然也可以在开发工具上自己创建),创建地址:https://start.spring.io/
二、添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.seckill</groupId>
<artifactId>seckill</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、创建实体类
package com.example.demo.seckill.pojo;
import java.io.Serializable;
import java.util.Date;
public class Goods implements Serializable {
private Long goodsId;
private String goodsName;
private Long goodsPrice;
private Integer goodStock;
private Date startTime;
private Date endTime;
public Long getGoodsId() {
return goodsId;
}
public void setGoodsId(Long goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Long getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(Long goodsPrice) {
this.goodsPrice = goodsPrice;
}
public Integer getGoodStock() {
return goodStock;
}
public void setGoodStock(Integer goodStock) {
this.goodStock = goodStock;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
@Override
public String toString() {
return "Goods{" +
"goodsId=" + goodsId +
", goodsName='" + goodsName + '\'' +
", goodsPrice=" + goodsPrice +
", goodStock=" + goodStock +
", startTime=" + startTime +
", endTime=" + endTime +
'}';
}
}
四、创建持久层类:注意添加mapper注解就可以的
package com.example.demo.seckill.mapper;
import com.example.demo.seckill.pojo.Goods;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
public interface SecKillMapper {
String findAllGoods = "select * from t_seckill_goods";//goods_id,goods_name,good_price,good_stock,start_time,end_time
//查询秒杀商品列表
@Select(findAllGoods)
public List<Goods> findAllGoods();
//查询单个秒杀商品
@Select("select * from t_seckill_goods where goods_id = #{goodsId}")
public Goods findOneGoods(@Param("goodsId") Integer goodsId);
//秒杀地址暴露:核实时间,核实商品数量,核实用户是否唯一
public boolean verifyKillGoods(Long goodsId,Long userId);
//执行秒杀
public void secKillGoods(Long goodsId,Long userId);
}
五、虽然没有xml文件,但是有些基本配置还是要有的,比如说tomcat端口配置,数据库的相关参数等,这个时候我们就需要用到application.yml配置文件,也可以用application.properties做配置,两个配置文件的格式不一样的哦!我遇到的问题就在这个里面,下面是正确的配置:
#端口号
server:
port: 55555
#datasource
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
# 基本属性
url: jdbc:mysql://localhost:3306/seckill?serverTimezone=GMT%2B8
username: root
password: lihui
jackson:
#设置空如何序列化
defaultPropertyInclusion: ALWAYS
#mybatis
mybatis:
type-aliases-package: com.example.demo
#若是出现返回的实体类是null 添加这个,开启驼峰式命名,没有开始的话没有办法进行映射
configuration:
map-underscore-to-camel-case: true
六、业务层的接口代码和实现代码
package com.example.demo.seckill.inter;
import com.example.demo.seckill.pojo.Goods;
import java.util.List;
public interface SecKill {
//查询秒杀商品列表
public List<Goods> findAllGoods();
//查询单个秒杀商品
public Goods findOneGoods(Integer goodsId);
//秒杀地址暴露:核实时间,核实商品数量,核实用户是否唯一
public boolean verifyKillGoods(Long goodsId,Long userId);
//执行秒杀
public void secKillGoods(Long goodsId,Long userId);
}
package com.example.demo.seckill.sevrce;
import com.example.demo.seckill.inter.SecKill;
import com.example.demo.seckill.mapper.SecKillMapper;
import com.example.demo.seckill.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SecKillImpl implements SecKill {
@Autowired
private SecKillMapper secKillMapper;
@Override
public List<Goods> findAllGoods() {
List<Goods> allGoods = secKillMapper.findAllGoods();
System.out.println(allGoods);
return allGoods;
}
@Override
public Goods findOneGoods(Integer goodsId) {
Goods oneGoods = secKillMapper.findOneGoods(goodsId);
return oneGoods;
}
@Override
public boolean verifyKillGoods(Long goodsId, Long userId) {
return false;
}
@Override
public void secKillGoods(Long goodsId, Long userId) {
}
}
七、控制层
package com.example.demo.seckill.controller;
import com.example.demo.seckill.inter.SecKill;
import com.example.demo.seckill.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("seckill")
public class SecKillController {
@Autowired
private SecKill secKillImpl;
@RequestMapping("findAllGoods")
@ResponseBody
public List<Goods> findAllGoods(){
List<Goods> allGoods = secKillImpl.findAllGoods();
return allGoods;
}
@RequestMapping("findOneGoods")
@ResponseBody
public Goods findOneGoods(){
Goods oneGoods = secKillImpl.findOneGoods(1001);
System.out.println(oneGoods);
return oneGoods;
}
}
后续的秒杀业务更新......