用springmvc+spring+mybatis实现秒杀的DAO层实现
来源:互联网 发布:拜县旅游攻略 知乎 编辑:程序博客网 时间:2024/05/22 02:08
1、首先设计数据库表结构
--创建数据库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))ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 COMMENT='秒杀库存表';--初始化数据INSERT INTO seckill(name,number,start_time,end_time)VALUES('1000元秒杀iPhone6s',100,'2017-12-13 00:00:00','2017-12-14 00:00:00'),('600元秒杀iPad',200,'2017-12-13 00:00:00','2017-12-14 00:00:00'),('400元秒杀红米4',300,'2017-12-13 00:00:00','2017-12-14 00:00:00'),('200元秒杀小米4',400,'2017-12-13 00:00:00','2017-12-14 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:已付款;2:已退货','create_time' TIMESTAMP NOT NULL COMMENT '创建时间',PRIMARY KEY (seckill_id,user_phone),KEY idx_create_time(create_time))ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='秒杀成功明细表';--连接数据库控制台mysql -uroot -p
在mysql中执行以上的sql脚本,完成数据库表创建。
2、在src/main/java下面创建实体类和dao接口
代码结构如下
3、配置mybatis的配置文件(mybatis-config.xml)
文件头部的声明可参考这里的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的getGeneratedKeys获取数据库自增主键值--> <setting name="useGeneratedKeys" value="true"/> <!--使用列别名替换列名 默认true--> <setting name="useColumnLabel" value="true"/> <!--开启驼峰命名转换create_time -> createTime--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings></configuration>
4、编写提供sql语句的xml配置文件,可在resources下新建mapper文件夹,然后新建dao的xml文件,例子(SeckillDao.xml):
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.seckill.dao.SeckillDao"> <!--为DAO接口方法提供sql语句配置--> <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_timr,end_time,create_time FROM seckill order by create_time DESC limit #{offset},#{limit} </select></mapper>
5、继续在resources下新建spring文件夹,然后方spring的配置文件,如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"> <!--配置整合MyBatis过程--> <!--配置数据库相关参数--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--数据库的连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--配置连接池属性--> <property name="driverClass" value="${driver}"/> <property name="jdbcUrl" value="${url}"/> <property name="user" value="${username}"/> <property name="password" value="${password}"/> <!--c3p0私有属性--> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!--关闭连接后不自动提交--> <property name="autoCommitOnClose" value="false"/> <!--获取连接超时时间--> <property name="checkoutTimeout" value="1000"/> <!--获取连接失败的重试次数--> <property name="acquireRetryAttempts" value="2"/> </bean> <!--配置SqlSessionFactory对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入数据库连接池--> <property name="dataSource" ref="dataSource"/> <!--配置mybatis全局配置文件,mybatis-config.xml--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--扫描entity包,使用别名 --> <property name="typeAliasesPackage" value="org.seckill.entity"/> <!--扫描sql配置文件,mapper需要的配置文件--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!--配置扫描DAO接口包,动态实现Dao接口,并注入到spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--给出需要扫描的Dao接口包--> <property name="basePackage" value="org.seckill.dao"/> </bean></beans>
同样的,xml头部声明需要去官网的API中查看,(小技巧:可以全局搜索“encoding=”UTF-8”“)。
6、在resources下创建jdbc.properties文件,内容为:
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=utf8username=rootpassword=ug&eyEFeo08n
以上配置完成之后即可进行junit单元测试。
阅读全文
0 0
- 用springmvc+spring+mybatis实现秒杀的DAO层实现
- 基于Spring+SpringMVC+Mybatis的秒杀系统之Dao层(1)
- 基于Spring+SpringMVC+MyBatis实现高并发秒杀APIM
- springMVC+mybatis 之 借助SqlSessionTemplate 实现Dao层通用写法
- 用spring+springMVC+mybatis实现模拟秒杀项目(一)
- spring+mybatis通用dao层、service层的一些个人理解与实现
- spring+mybatis通用dao层、service层的一些个人理解与实现
- MyBatis学习笔记-Spring集成DAO层实现方式记录
- 基于Spring+SpringMVC+Mybatis的秒杀系统之Service层(2)
- 基于Spring+SpringMVC+Mybatis的秒杀系统之web层(3)
- 基于DAO层上的分库分表、及mybatis实现
- SpringMVC关于MyBatis通用公共Dao的实现
- springmvc 整合mybatis dao层不写实现类
- springmvc+mybatis整合dao层
- 通过Spring整合MyBatis实现DAO层不用写实现类
- 实现Spring+SpringMVC+MyBatis的整合
- Mybatis - Mybatis 增删改查之dao层实现
- MyBatis+JUnit测试Dao(Mapper)的用例实现( 不启动web工程 且 未使用SpringMVC管理MyBatis时 )
- 爱奇艺推荐系统架构与实践
- Opencv基本知识
- 求平均数
- Raft协议整理
- sublime text3代码格式化插件
- 用springmvc+spring+mybatis实现秒杀的DAO层实现
- java详解 --- 接口的优化
- redis的 rdb 和 aof 持久化的区别
- 10个Linux 网络和监控命令
- Docker基础镜像的制作
- STL-关于栈和队列的面试题
- 文件流之文件输出流FileOutputStream
- MySQL存储过程简单实例--游标使用
- Coursera 多态编程作业思考