1、数据库设计
1.1 数据库表关系
1.2 表的建立
drop database if exists lucky_draw;
create database lucky_draw character set utf8mb4;
use lucky_draw;
drop table if exists user;
create table user(
id int primary key auto_increment,
username varchar(20) not null unique comment '用户账号',
password varchar(20) not null comment '密码',
nickname varchar(20) comment '用户昵称',
email varchar(50) comment '邮箱',
age int comment '年龄',
head varchar(255) comment '头像url',
create_time timestamp default NOW() comment '创建时间'
) comment '用户表';
drop table if exists setting;
create table setting(
id int primary key auto_increment,
user_id int not null comment '用户id',
batch_number int not null comment '每次抽奖人数',
create_time timestamp default NOW() comment '创建时间',
foreign key (user_id) references user(id)
) comment '抽奖设置';
drop table if exists award;
create table award(
id int primary key auto_increment,
name varchar(20) not null comment '奖项名称',
count int not null comment '奖项人数',
award varchar(20) not null comment '奖品',
setting_id int not null comment '抽奖设置id',
create_time timestamp default NOW() comment '创建时间',
foreign key (setting_id) references setting(id)
) comment '奖项';
drop table if exists member;
create table member(
id int primary key auto_increment,
name varchar(20) not null comment '姓名',
no varchar(20) not null comment '工号',
setting_id int not null comment '抽奖设置id',
create_time timestamp default NOW() comment '创建时间',
foreign key (setting_id) references setting(id)
) comment '抽奖人员';
drop table if exists record;
create table record(
id int primary key auto_increment,
member_id int not null comment '中奖人员id',
award_id int not null comment '中奖奖项id',
create_time timestamp default NOW() comment '创建时间',
foreign key (member_id) references member(id),
foreign key (award_id) references award(id)
) comment '中奖记录';
2、前后端接口设计
2.1 用户登录
请求:POST api/user/login
Content-Type: application/json
{username: "abc", password: "123"}
响应:
{ "success" : true }
2.2 用户注册
请求:POST api/user/register
Content-Type: multipart/form-data; boundary=---- WebKitFormBoundarypOUwkGIMUyL0aOZT
username: abc
password: 123
nickname: 权志龙
email: 666@163.com
age: 66
headFile: (binary)
响应:
{ "success" : true }
2.3 查询抽奖设置
请求:GET api/setting/query 响应:
{
"success" : true,
"data" : {
"id" : 1,
"userId" : 1,
"batchNumber" : 8,
"createTime" : "2020-08-14 08:16:31",
"user" : {
"id" : 1,
"username" : "123",
"password" : "123",
"nickname" : "蜡笔小新",
"email" : "1111@163.com",
"age" : 18,
"head" : "img/test-head.jpg",
"createTime" : "2020-08-14 08:16:31",
"settingId" : 1
},
"awards" : [ {
"id" : 1,
"name" : "一等奖",
"count" : 1,
"award" : "火箭",
"settingId" : 1,
"createTime" : "2020-08-14 08:16:31",
"luckyMemberIds" : [ 5 ]
}, {
"id" : 2,
"name" : "二等奖",
"count" : 5,
"award" : "红旗 H5",
"settingId" : 1,
"createTime" : "2020-08-14 08:16:31",
"luckyMemberIds" : [ 56, 40, 32, 65, 81 ]
}, {
"id" : 3,
"name" : "三等奖",
"count" : 20,
"award" : "国内任意游",
"settingId" : 1,
"createTime" : "2020-08-14 08:16:31",
"luckyMemberIds" : [ 48, 68, 43, 73, 13, 83, 63, 25 ]
} ],
"members" : [ {
"id" : 1,
"name" : "李寻欢",
"no" : "水果刀",
"userId" : 1,
"createTime" : "2020-08-14 08:16:31"
}, {
"id" : 2,
"name" : "郭靖",
"no" : "降猪十八掌",
"userId" : 1,
"createTime" : "2020-08-14 08:16:31"
}, {
"id" : 3,
"name" : "韦小宝",
"no" : "龙爪手",
"userId" : 1,
"createTime" : "2020-08-14 08:16:31"
} ]
}
}
2.4 修改抽奖人数
请求:GET api/setting/update?batchNumber=5 响应:
{ "success" : true }
2.5 新增奖项
请求:POST api/award/add
Content-Type: application/json
{name: "牛哄哄", count: 3, award: "华为手机"}
响应:
{ "success" : true }
2.6 修改奖项
请求:POST api/award/update
Content-Type: application/json
{name: "牛哄哄", count: 3, award: "小米手机", id: 4}
响应:
{ "success" : true }
2.7 删除奖项
请求:GET api/award/delete/4 响应:
{ "success" : true }
2.8 新增抽奖人员
请求:POST api/member/add
Content-Type: application/json
{name: "羞羞的粉拳", no: "007"}
响应:
{ "success" : true }
2.9 修改抽奖人员
请求:POST api/member/update
Content-Type: application/json
{name: "泰山", no: "000", id: 96}
响应:
{ "success" : true }
2.10 删除抽奖人员
请求:GET api/member/delete/97 响应:
{ "success" : true }
2.11 抽奖
请求:POST api/record/add/3
Content-Type: application/json
[92, 22, 43, 76]
响应:
{ "success" : true }
2.12 删除当前奖项某个获奖人员
请求:GET api/record/delete/member?id=22 响应:
{ "success" : true }
2.13 删除当前奖项全部获奖人员
请求:GET api/record/delete/award?id=3 响应:
{ "success" : true }
3、开发环境准备
3.1 配置项目pom.xml
<?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>
<!-- 默认使用的Spring Framework版本为5.2.10.RELEASE -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>lucky-draw</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring-boot-starter-web: 基于SpringBoot开发的依赖包,
会再次依赖spring-framework中基本依赖包,aop相关依赖包,web相关依赖包,
还会引入其他如json,tomcat,validation等依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除tomcat依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加 Undertow 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--引入AOP依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- mybatis-spring-boot-starter: Mybatis框架在SpringBoot中集成的依赖包,
Mybatis是一种数据库对象关系映射Object-Relationl Mapping(ORM)框架,
其他还有如Hibernate等 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- Mybatis代码生成工具 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- druid-spring-boot-starter: 阿里Druid数据库连接池,同样的运行时需要 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!-- JDBC:mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
<scope>runtime</scope>
</dependency>
<!-- spring-boot-devtools: SpringBoot的热部署依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<!-- 不能被其它模块继承,如果多个子模块可以去掉 -->
<optional>true</optional>
</dependency>
<!-- lombok: 简化bean代码的框架 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- spring-boot-starter-test: SpringBoot测试框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- SpringBoot的maven打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 明确指定一些插件的版本,以免受到 maven 版本的影响 -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
3.2 Springboot配置文件
server.port=8085
debug=true
# 设置打印日志的级别,及打印sql语句
#日志级别:trace,debug,info,warn,error
#基本日志
logging.level.root=INFO
#扫描的包:druid.sql.Statement类和frank包
logging.level.druid.sql.Statement=DEBUG
logging.level.org.example=DEBUG
# 美化JSON数据格式
spring.jackson.serialization.indent-output=true
# 设置JSON数据的日期格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
# JSON数据属性为null时不返回
spring.jackson.default-property-inclusion=non_null
# get请求参数及表单提交数据的日期格式
spring.mvc.format.date=yyyy-MM-dd HH:mm:ss
#servlet上下文路径
#server.servlet.context-path=/lucky-draw
# 自定义属性:用户头像本地保存根路径
user.head.local-path=E:/TMP
user.head.remote-path=http://localhost:8080${server.servlet.context-path:}
#user.head.filename=head.jpg
# 静态资源映射:将路径映射为/,即/static/xxx,映射为/xxx,支持多个字符串,逗号间隔
# 默认为/META-INF/resources/, /resources/, /static/, /public/
# 指定外部web资源文件夹:访问的路径为/
spring.resources.static-locations=classpath:/static/,classpath:/public/,file:${user.head.local-path}
#druid数据库连接池配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/lucky_draw?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
#指定Mybatis表和实体映射关系xml配置文件,包含表与实体的映射,字段和属性的映射,及各个sql语句
mybatis.mapper-locations=classpath:mapper
3.3 SpringBoot启动类
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
|