ssm实现秒杀总结 适合新手 **一**

@[TOC]ssm实现秒杀总结 适合新手

1.ssm学习视频看了点,书也看了点,就动手做个小demo巩固一下,这里时根据慕课上的秒杀系写的链接: link.

2.用到的技术栈 spring +springMVC +mybatis +mysql,前端用的Bootstrap4
工具用的是IDEA和maven
目录结构

配置pom.xml配置依赖包

//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>

  <groupId>zmf</groupId>
  <artifactId>secKill</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>secKill Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--补全依赖-->
    <!--1.日志java日志slf4j,logbank-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.12</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.1</version>
    </dependency>
    <!--数据库相关依赖-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.30</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    <!--dao层依赖 mybatis依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.3</version>
    </dependency>
    <!--servlet web 相关依赖-->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.5.4</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <!--spring 核心依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <!--springdao层依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.1.7.RELEASE</version>
  </dependency>

  <!--web层依赖-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.1.7.RELEASE</version>
  </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>

<!--测试依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>secKill</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

首先进行数据库设计
这里设计两张表 一张商品表,一张秒杀记录表,用户登录验证在前端实现,没有再建表,有兴趣的话当然可以再建一个用户表
代码

// 数据库sql
CREATE database seckill;
use seckill;
CREATE  TABLE  seckill(
seckill_id bigint not null Auto_INCREMENT comment '商品库存id',
name varchar(120) not null comment'商品名称',
number int not null comment'库存数量',
start_time TIMESTAMP not null comment'秒杀开启时间',
end_time TIMESTAMP not null comment'秒杀结束时间',
create_time TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP comment'创建时间',
PRIMARY key(seckill_id),
KEY idx_start_time(start_time),
key idx_end_time(end_time),
key idx_create_time(create_time)
)ENGING=InooDB Auto_INCREMENT=1000 DEFAULT charset=utf8 comment='秒杀库存表';
--初始化数据
INSERT into seckill(name,number,start_time,end_time)
 VALUES
('1000元秒杀iphoneX',100,'2019-11-11 00:00:00','2019-11-12 00:00:00'),
('500元秒杀ipad5',300,'2019-11-11 00:00:00','2019-11-12 00:00:00'),
('3000元秒杀小米9',200,'2019-11-11 00:00:00','2019-11-12 00:00:00'),
('1050元秒杀红米note7',100,'2019-11-11 00:00:00','2019-11-12 00:00:00');


CREATE  table success_killed(
seckill_id bigint not null comment '秒杀商品id',
user_phone bigint not null comment '用户手机号',
state tinyint not null DEFAULT -1 comment '状态标识:-1无效 0:成功 1:已付款',
create_time TIMESTAMP not null comment '创建时间',
PRIMARY key(seckill_id,user_phone),
key idx_create_time(create_time)
)ENGING=InooDB  DEFAULT charset=utf8 comment='秒杀成功明细表';

建立实体类entity,代码就不贴了,创建dao接口SeckillDaoSuccessKilledDao

public interface SeckillDao {
    //减库存
    int reduceNumber(@Param("seckillId") long seckillId,@Param("killTime") Date killTime);
    //根据id查询秒杀对象
    Seckill queryById(long SeckillId);
    //根据偏移量查询全部
    List<Seckill> queryAll(@Param("offset") int offset,@Param("limit") int limit);
}
public interface SuccessKilledDao {
    //插入购买明细,可过滤重复
    int insertSuccessKilled(@Param("seckillId") long seckillId,@Param("userPhone") long userPhone);
    //根据id查询succeesskilled并携带秒杀产品对象实体
    SuccessKilled queryByIdWithSeckill(@Param("seckillId") long seckillId,@Param("userPhone") long userPhone);
}

配置mybatis.xml,并实现接口SeckillDao.xmlSuccessKilledDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--配置全局属性-->
    <settings><!--使用jdbc的useGeneratedKeys获取自增主键值-->
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="useColumnLabel" value="true"/>
        <!--开启驼峰映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>
SeckillDao.xml
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zmf.dao.SeckillDao">
    <update id="reduceNumber">
        update
          seckill
        SET
          number = number-1
        WHERE seckill_id=#{seckillId}
        and start_time<![CDATA[<=]]>#{killTime}
        and end_time >=#{killTime}
        and number>0;
    </update>
    <select id="queryById" resultType="Seckill" parameterType="long">
        SELECT
        seckill_id,name,number,start_time,end_time,create_time
        FROM seckill
        WHERE seckill_id=#{seckillId}
    </select>
    <select id="queryAll" resultType="Seckill">
        SELECT
        seckill_id,name,number,start_time,end_time,create_time
        FROM seckill
        order by create_time DESC
        limit #{offset},#{limit}
    </select>
SuccessKilledDao.xml
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zmf.dao.SuccessKilledDao">
    <insert id="insertSuccessKilled">
    /*主键冲突返回0*/
        INSERT ignore into success_killed(seckill_id,user_phone,state)
        values(#{seckillId},#{userPhone},0)
    </insert>
    <select id="queryByIdWithSeckill" resultType="SuccessKilled">
        /*根据id查询并返回携带Seckill实体*/
        SELECT
         sk.seckill_id,
         sk.user_phone,
         sk.create_time,
         sk.state,
         s.seckill_id "seckill.seckill_id",
         s.name "seckill.name",
         s.number "seckill.number",
         s.start_time "seckill.start_time",
         s.end_time "seckill.end_time",
         s.create_time "seckill.create_time"
        FROM success_killed sk
        INNER JOIN seckill s ON sk.seckill_id=s.seckill_id
        where sk.seckill_id=#{seckillId} AND sk.user_phone=#{userPhone}
    </select>
</mapper>

至此,DAO层已经完成了
然后我们去拿spring整合mybatis
spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--1.配置数据库相关properties属性${}-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--2.数据库连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--配置连接池属性-->
        <property name="driverClass" value="${driver}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <!--c3p0连接池属性-->
        <property name="maxPoolSize" value="30"></property>
        <property name="minPoolSize" value="10"></property>
        <!--关闭连接后不自动提交,默认为false-->
        <property name="autoCommitOnClose" value="false"></property>
        <!--链接超时时间-->
        <property name="checkoutTimeout" value="10000"></property>
        <!--重连次数-->
        <property name="acquireRetryAttempts" value="2"></property>
    </bean>
    <!--3.配置SqlSessionFactory对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据库连接池-->
        <property name="dataSource" ref="dataSource"></property>
        <!--配置mybatis全局配置文件-->
        <property name="configLocation" value="classpath:mybatis.xml"></property>
        <!--扫描entity包 使用别名-->
        <property name="typeAliasesPackage" value="com.zmf.entity"></property>
        <!--扫描mapper.xml-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
    <!--4.扫描dao接口,注入spring容器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlsessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!--扫描dao-->
        <property name="basePackage" value="com.zmf.dao"></property>
    </bean>
</beans>

上面引用了jdbc.properties,这里也放一下

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
user=root
password=123456

链接视频里用的是username=root。建议改为user,这点容易出错,因为${username}引用的可能是你的用户名而不是properties里配置的
然后你就可以创建个test去测试一下是否能进行增删改查操作了
工程文件链接**: link.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小菲语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值