0 Preface
A high concurrent second kill system based on SSM framework is developed using Idea+maven+ssm+mysql+redis+jetty and Bootstrap/jquery.
Through this small project, the process of developing Web applications based on the SSM framework and common methods of avoiding pits was cleared, and the project was optimized for high concurrency in the end with the Redis cache and MySQL procedure.
Next, the whole project development process is combed from the DAO layer, service layer, Web layer development and high concurrency optimization 4 aspects.
Source Address Https://github.com/Allegr0/seckill
Project preparation;
Create a new project, use the MAVEN organization framework, create New from Archtype:webapp, and add dependencies in Pom.xml. Modify the default servlet support version in Web. XML to 3.1, add the dependencies required for the project in Pom.xml, and add the jetty server plug-in
1 DAO layer related development 1.1 database design and coding
Database DDL in Main/sql/schema.sql, where the use of pure handwritten DDL to create a database, such a good habit is equivalent to each database changes are left to record, facilitate the maintenance of the project later.
1.2 DAO entity and Interface encoding
Placed under the Org/seckill/entity directory and the Org/seckill/dao directory, respectively. The table <--> Javapojo class entity in the database is a one-to-one relationship, the database table corresponds to the entity class, and the columns in the database table correspond to the attributes of the entity class.
Since we adopt the MyBatis ORM Framework, there are only interfaces and abstract methods, descriptive functions in the DAO directory, and the specific SQL is implemented in the MyBatis mapper.
1.3 Implementing DAO based on MyBatis
MyBatis is equivalent to the advantages of other ORM frameworks: Providing SQL with XML provides sufficient flexibility to mapper the DAO interface method automatically and mapping the result auto set to Javapojo.
Configure the Resources/mybatis-config.xml configuration file. Some of the global properties of the MyBatis attribute (using column alias substitution column name, camel name conversion) can automatically convert the underscore name in SQL to the Hump naming form in Java, for example: Create_time (SQL) <-> Createtime (Java )。
Write XML (SQL statements) in the Resources/mapper directory, corresponding to all abstract methods of interface definitions in DAO Tips: (Note the XML tag escape character <! [cdata[<=]]>, use of federated primary key, insert ignore tips]
1.4 Based on spring managed Mybatissqlsessfactory objects
In the resources/spring directory to write spring-dao.xml configuration file (note here jdbc.properties best Use jdbc.username can not directly use username, otherwise will error), to spring The beans required to assemble the DAO layer in the IOC include the database connection pool, the MyBatis critical Sqlsessionfactory object, and the scan DAO interface implementation class and inject into the IOC.
1.5 DAO layer Unit test
Generate the JUnit unit test class with command+shift+t and test. The automatic injection of Tips:bean is @resource and @Autowired in two ways. One is by name, one by type.
Unit tests have a small pit: Exception: Org.apache.ibatis.binding.BindingException. Because Java does not have a record of formal parameters, because of the XML SQL parameter binding, the DAO interface parameter is not saved. Queryall (int offset, int limit), Querryall (arg0, arg1), when there are multiple parameters, there is an issue with the parameter not found. Therefore, before the DAO layer interface function parameters to be labeled @Param ("parameter name") For example: Queryall (@Param ("offset") int offset, @Param ("limit") int limit);
1.6 Summary
Because of the Mybatisorm framework, the DAO layer has evolved into: interface design +sql written. Code and SQL are separated to facilitate late review.
The DAO layer only realizes the basic function of adding and deleting the relevant database, and the specific DAO splicing and other complex logic are completed in the service layer.
2 service layer related development 2.1 preparation: New DTO, exception, service three package
DTO: A data transfer layer that stores some types of data. DTOs and entity are more like, entity is the encapsulation of business (database table), DTOs are concerned about the data passing between the web and service, and the DTO layer also includes the encapsulation class in JSON format.
Spring declarative transactions only receive RuntimeException (run-time exceptions), and only the run-time exceptions are rolled back.
2.2 Seconds to kill service interface design (standing in the "user" angle design)
Service/seckillservice.java second Kill interface contains 4 methods: query All Seconds to kill records, query a single second kill record, the exposure of the second kill address, execute seconds kill. In conjunction with the method in the interface, we should design the DTO class and the exception class at the same time.
2.3-Second Kill service interface implementation
Service/impl/seckillserviceimpl.java. New enumeration Class Enums/seckillstateenum holds the state, the constant corresponding to the state information.
In IntelliJ idea 2017.2 @resourse or @Autowired a warning or error prompt (Could not autowire. No beans of "XXX" type found.). But the program runs correctly. You can configure the warning level in idea to reduce this type of problem.
2.4 Based on the spring managed service implementation class
Write the Spring-service configuration file under the Resources/spring directory to assemble the service layer's beans to the IOC. Pass
<!-- Scan All types of annotations under service package - < base-package= "Org.seckill.service"/>
The Scan class package automatically converts all @component, @Service, @Controller, @Respository classes that annotate the spring annotations into a bean and injects them into the IOC container, while labeling the code with @autowired, @ Resource methods or member variables are dependent injected by name or type.
2.5 Using Spring declarative transactions
There are two common uses of spring declarative transactions: 1) The Tx:advice+aop namespace, in which one configuration is permanently active. 2) @Transcational, annotate methods that require transactions by annotations.
Not all methods require transactions, such as only one modification operation, read-only operation, and no transaction control, so it is recommended to annotate methods that require transactions with annotation control (@Transcational), rather than using the TX-ADVICE+AOP namespace to label all methods with transactions. Personal understanding: When there are more than two modifications or select for update, the transaction is required, which also requires further learning of MySQL row-level locks. The @transcational method is marked, a transaction is opened, and then the last return or throw
runtimeexceptionWhen a commit or rollback is configured in the Spring-service.xml configuration transaction manager, the annotation-based declarative transaction is configured. 2.6 Integrated Test
Implementation class Logback configuration for the SLF4J interface: Resources/logback.xml. Declare the logger variable before the test class you need to use
Private final Logger Logger = Loggerfactory.getlogger (This.getclass ());
3 Web layer related development 3.1 restful interface and SPRINGMVC overview
Rest is the English representational state transfer (representational state transition) or representational states transfer; Rest is an architectural style of Web services, using widely popular standards and protocols such as http,uri,xml,json,html, lightweight, cross-platform, cross-language architecture design; it is a design style, not a standard, is a kind of thought. RESTful architecture: (1) Each URI represents a resource. (2) between the client and the server, the transmission of this kind of resources of the performance layer, (3) the client through four HTTP verbs, the team segment resources to operate, to achieve "presentation layer State transformation." For space reasons, there is no detailed introduction to the Spring MVC framework principle in the RESTful interface
A high-power generator second-kill web system developed based on SSM framework