首先我们快速搭建一个SpringBoot项目出来,因此这个项目的重心在后端逻辑,因此前端页面简单搭建:
1.数据库建表
首先将我们未来所需要的数据建表:
item商品表,存放所有商品信息
CREATE TABLE `item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL COMMENT '商品名',
`code` varchar(255) DEFAULT NULL COMMENT '商品编号',
`stock` bigint(20) DEFAULT NULL COMMENT '库存',
`purchase_time` date DEFAULT NULL COMMENT '采购时间',
`is_active` int(11) DEFAULT '1' COMMENT '是否有效(1=是;0=否)',
`create_time` datetime DEFAULT NULL,
`update_time` timestamp NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品表'
item_kill待秒杀商品表,存放待秒杀的商品信息
CREATE TABLE `item_kill` (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`item_id` int(11) DEFAULT NULL COMMENT '商品id',
`total` int(11) DEFAULT NULL COMMENT '可被秒杀的总数',
`start_time` datetime DEFAULT NULL COMMENT '秒杀开始时间',
`end_time` datetime DEFAULT NULL COMMENT '秒杀结束时间',
`is_active` tinyint(11) DEFAULT '1' COMMENT '是否有效(1=是;0=否)',
`create_time` timestamp NULL DEFAULT NULL COMMENT '创建的时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='待秒杀商品表'
item_kill_success秒杀成功订单表,存放秒杀成功的订单
CREATE TABLE `item_kill_success` (
`code` varchar(50) NOT NULL COMMENT '秒杀成功生成的订单编号',
`item_id` int(11) DEFAULT NULL COMMENT '商品id',
`kill_id` int(11) DEFAULT NULL COMMENT '秒杀id',
`user_id` varchar(20) DEFAULT NULL COMMENT '用户id',
`status` tinyint(4) DEFAULT '-1' COMMENT '秒杀结果: -1无效 0成功(未付款) 1已付款 2已取消',
`create_time` timestamp NULL DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='秒杀成功订单表'
user用户表,存放用户信息的表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) CHARACTER SET utf8mb4 NOT NULL COMMENT '用户名',
`password` varchar(200) CHARACTER SET utf8mb4 NOT NULL COMMENT '密码',
`phone` varchar(50) NOT NULL COMMENT '手机号',
`email` varchar(100) CHARACTER SET utf8mb4 NOT NULL COMMENT '邮箱',
`is_active` tinyint(11) DEFAULT '1' COMMENT '是否有效(1=是;0=否)',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_user_name` (`user_name`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息表'
2.项目框架的搭建
新建一个springboot项目,命名为SecondKill,项目结构如下所示:
接下来引入依赖,为了让大家免受maven无法引入的痛苦,我在项目中加入了阿里的镜像,现在引入依赖应该不会受到网络的影响。
4.0.0
org.example
SecondKill
1.0-SNAPSHOT
UTF-8
1.8
${java.version}
${java.version}
2.1.7.RELEASE
1.2.0.RELEASE
1.7.12
1.2.17
5.1.37
1.1.10
19.0
2.10.3
3.4.14
2.12.0
1.4.2
2.8.5
3.8.2
3.8.1
2.1.1
alimaven
Maven Aliyun Mirror
http://maven.aliyun.com/nexus/content/repositories/central/
true
false
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.springframework.boot
spring-boot-starter-thymeleaf
ch.qos.logback
logback-classic
1.2.3
org.slf4j
slf4j-log4j12
${slf4j.version}
com.google.guava
guava
${guava.version}
joda-time
joda-time
${joda-time.version}
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.springframework.boot
spring-boot-starter-web
${spring-boot.version}
ch.qos.logback
logback-classic
org.slf4j
log4j-over-slf4j
org.slf4j
slf4j-log4j12
log4j
log4j
org.springframework.boot
spring-boot-starter-tomcat
provided
org.apache.tomcat.embed
tomcat-embed-jasper
provided
javax.servlet
jstl
org.apache.zookeeper
zookeeper
${zookeeper.version}
org.slf4j
slf4j-log4j12
log4j
log4j
org.apache.curator
curator-framework
${curator.version}
org.apache.curator
curator-recipes
${curator.version}
org.springframework.boot
spring-boot-starter-test
test
org.apache.shiro
shiro-ehcache
${shiro.version}
org.apache.shiro
shiro-core
${shiro.version}
org.apache.shiro
shiro-web
1.4.0
org.apache.shiro
shiro-spring
${shiro.version}
com.google.code.gson
gson
${gson.version}
org.springframework.boot
spring-boot-starter-redis
1.3.5.RELEASE
commons-fileupload
commons-fileupload
1.3.3
commons-io
commons-io
2.4
org.springframework.boot
spring-boot-starter-amqp
${spring-boot.version}
org.redisson
redisson
${redisson.version}
org.apache.commons
commons-lang3
${common-lang.version}
org.springframework
spring-jdbc
org.projectlombok
lombok
1.18.8
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.fasterxml.jackson.core
jackson-annotations
2.9.0
kill-${project.parent.version}
org.springframework.boot
spring-boot-maven-plugin
${spring-boot.version}
repackage
org.apache.maven.plugins
maven-war-plugin
3.2.2
false
org.apache.maven.plugins
maven-compiler-plugin
6
6
src/main/resources
true
3.让项目跑起来
创建启动类MainApplication
package com.sdxb.secondkill;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(value = {"classpath:spring/spring-jdbc.xml"})
public class MainApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MainApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
在application.properties中配置数据库的连接信息和mybatis的配置信息:(换成自己的数据库信息)
#数据源配置
datasource.url=jdbc:mysql://127.0.0.1:3306/db_second_kill?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
datasource.username=root
datasource.password=123456
在spring-jdbc.xml中配置数据源信息
在mybatis-config.xml中配置mybatis,主要是开启缓存,设置默认数据库超时时间以及开启驼峰命名规则,可以把数据库中类似user_id的字段解析为userId。
以上都是基础配置,接下来创建一个测试Controller和测试页面测试项目是否启动成功,在controller包下新建一个testController
package com.sdxb.secondkill.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class testController {
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String index(){
return "test";
}
}
在templates中新建一个test.html
Title
test
运行项目,访问地址http://localhost:8080/test,页面上显示test,表示项目构建成功。
到现在为止的代码放在github上,https://github.com/OliverLiy/SecondKill/tree/version1.0
我搭建了一个微信公众号《Java鱼仔》,如果你对本项目有任何疑问,欢迎在公众号中联系我,我会尽自己所能为大家解答。