Java高并发秒时啊API之业务分析与Dao层1
---2-2---------------------------------------------------------------------------------
1 推荐从官网获取资源:1.文档更全面权威,避免过时或错误
Spring config: http://spring.io/docs
http://docs.spring.io/spring/docs/4.2.9.RELEASE/spring-framework-reference/htmlsingle/
logback config:
manual: https://logback.qos.ch/manual/index.html
Chapter 3: Logback configuration: https://logback.qos.ch/manual/configuration.html
mybatis config: http://www.mybatis.org/mybatis-3/zh/index.html
2 maven命令创建web骨架项目
mvn archetype:create -DgroupId=org.seckill -DartifactId=seckill -DarchetypeArtifactId=maven-archetype-webapp
执行出错:原因([Maven] 从mvn archetype:create命令说起 http://blog.csdn.net/xiaofenglu/article/details/47614215)
[ERROR] Could not find goal 'create' in plugin org.apache.maven.plugins:maven-archetype-plugin:3.0.1 among available goals crawl, create-from-project, generate, help, integration-test, jar, update-local-catalog -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoNotFoundException
改为:
mvn archetype:generate -DgroupId=org.seckill -DartifactId=seckill -DarchetypeArtifactId=maven-archetype-webapp
IDE:import project
maven创建的web.xml中servlet版本为2.3不支持el表达式,需要改为高版本,具体内容可以从tomcat的示例文件中拷贝(从tomcat的webapps的examples/WEB-INF的web.xml参照)
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <!-- 修改servlet版本为3.1 --> </web-app>
3 补全目录结构
src/main/java
/resources
/webapp
/WEB-INF
src/test/java
/resources
4 pom.xml
4.1默认junit改为4.11(3.0为编程方式,4.0为annotation)
4.2 补全项目依赖
1.日志 java日志:slf4j,log4j,logback,common-log
slf4j 是规范/接口
日志实现:slf4j,log4j,logback,common-log
使用:slf4j + logback
<!-- 2: 数据库相关依赖 -->MySql+连接池c3p0
<!-- DAO框架:MyBatis依赖 -->
注意:
<!-- MyBatis自身实现的spring整合依赖 v1.2.3出错,替换为1.3.0
详细见:疑惑的 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L
http://www.cnblogs.com/molao-doing/p/6056380.html
-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
错误信息:
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L错误
pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.seckill</groupId> <artifactId>seckill</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>seckill Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 补全项目依赖--> <!--1:日志 java日志:slf4j,log4j,logback,common-log slf4j 是规范/接口 日志实现:slf4j,log4j,logback,common-log 使用:slf4j + logback --> <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> <!-- 实现slf4j接口并整合 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> </dependency> <!-- 2: 数据库相关依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> <scope>runtime</scope> </dependency> <!-- 连接池c3p0 --> <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.4.4</version> </dependency> <!-- MyBatis自身实现的spring整合依赖 v1.2.3出错,替换为1.3.0 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--3: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> <scope>provided</scope> </dependency> <!--4:spring依赖 --> <!-- 1) 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> <!-- 2) spring dao层依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- tx:transaction --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- 3) spring 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> <!-- 4) spring test相关依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.7.RELEASE</version> </dependency> </dependencies> <build> <finalName>seckill</finalName> </build> </project>
===3业务分析===============================================================
---3-1 秒杀业务分析---------------------------------------------------------------------------------
商家-库存-用户
数据落地与不数据落地: 落地数据:就是被持久化的数据,这种数据一般放在硬盘或是其他的持久化存储设备里,例如:图片、系统日志、在页面上显示的数据以及保存在关系数据库里的数据等等,落地数据一定会有一个固定的载体,他们不会瞬时消失的。 不落地数据:一般指存储在内存或者是网络传输里的数据,这些数据是瞬时,使用完毕就会消失,例如:我们在浏览器发送给服务器的请求;从数据库读取出来的一直到页面展示前的数据等等。 “不落地”传输能够满足用户在性能上的要求。
事务机制:最可靠的数据落地方案(目前)
数据落地
MySQL VS NoSQL
Nosql:追求高性能、分布式。 对事务支持不好。数据落地不安全?NoSQL 所有的非关系型数据库都可以归类在这里,比如redis
---3-2 MySQL实现的难点信息---------------------------------------------------------------------------------
难点问题:竞争
MySQL:对mysql来说,秒杀这种业务的难点问题就是解决竟争。事务+行级锁
Start Transaction
Update 库存数量(竞争)
Insert购买信息
Commit
行级锁
---3-3---------------------------------------------------------------------------------
秒杀功能:
1.秒杀接口暴露
2.执行秒杀
3.相关查询
代码开发阶段:
1.DAO设计编码
2.Service设计编码
3.Web设计编码