Abstract: This blog is the “Java seconds kill system actual combat series article” the second article, mainly share how to use IDEA, based on SpringBoot+SpringMVC+Mybatis+ distributed middleware to build a multi-module project, that is, “seconds kill system”! .

Content: The traditional SpringBoot project based on IDEA is directly built with the help of the Spring Initializr plug-in, but in most cases, this way can only serve as a “single module” project, which cannot well achieve the principle of “clear division of labor, clear responsibility”.

Therefore, in order to better manage the project code and try to achieve “module name” and quickly locate the given class file or other files, we will build a multi-module project based on IDEA and using Maven. The construction IDEA is as follows:



The detailed build process will not be covered in this article! At the end of the article provides the source code address and the build process of the video tutorial! The following highlights the build steps associated with the Java seckill System.

(1) The overall directory structure of the finally successfully constructed project is shown in the figure below:


As you can see from the directory structure, this project is an “aggregation project”, in which the Model module depends on the API module, the Server module depends on the Model module, layer upon layer! Finally in the Server module to achieve the “big summary”, that is, the server module is the core of the whole project, such as “configuration files”, “entry start class” and so on in this module!

Moreover, the responsibilities of each module are not the same, the division of labor is very clear, just like the model module, the general people will know that the things put here should be with Mybatis or mysql related class files and configuration files and so on.

Once you’ve built the corresponding module, you need to add dependencies to the corresponding module. You just need to add the corresponding dependencies to pom.xml, so we won’t post them here!

(2) The MainApplication code of the server module entry is mainly pasted here, as follows:

@SpringBootApplication
@ImportResource(value = {"classpath:spring/spring-jdbc.xml"})
@MapperScan(basePackages = "com.debug.kill.model.mapper")
@EnableScheduling
public class MainApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        returnbuilder.sources(MainApplication.class); } public static void main(String[] args) { SpringApplication.run(MainApplication.class,args); }}Copy the code

Where this startup class loads the configuration file spring-jdbc.xml (the configuration file for database link information)!

After the completion of the construction, you can run the entire project using external Tomcat. During the running process, observe the output information of the Console Console. If there is no error message, it represents that the construction of the whole project is no problem! If there is a problem, research it and try to fix it! If still can not solve, you can provide the contact information at the end of Gavin to solve!

(4) In addition, in order to make the whole project under the condition of separate development of the front and back ends, the interface interaction of the front and back ends is more standardized (such as the specification of response information, etc.). Here we use a general state code enumeration class StatusCode and a general response result class BaseResponse. It is used for unified encapsulation when the back end returns the response information to the front end.

The source code for the StatusCode enumeration class StatusCode is as follows:

public enum StatusCode {
    Success(0,"Success"),
    Fail(-1,"Failure"),
    InvalidParams(201,"Illegal parameter!"),
    UserNotLogin(202,"User not logged in"),; private Integer code; // code private String MSG; MSG StatusCode(Integer code, String MSG) {this.code = code; this.msg = msg; } public IntegergetCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) { this.msg = msg; }}Copy the code

The source code for the response result class BaseResponse is as follows:

public class BaseResponse<T> { private Integer code; // code private String MSG; MSG private T data; Public BaseResponse(Integer code, String MSG) {this.code = code; this.msg = msg; } public BaseResponse(StatusCode statusCode) { this.code = statusCode.getCode(); this.msg = statusCode.getMsg(); } public BaseResponse(Integer code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } public IntegergetCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) { this.data = data; }}Copy the code

In the future, you will find that the combination of BaseResponse and StatusCode is very convenient, and if you look closely, you will see that this pattern is very similar to the “HTTP response model”!

(5) Finally, to test the usability of the whole project and the use of BaseResponse and StatusCode, let’s create a BaseController controller and develop a simple request method in it, as shown below:

@Controller
@RequestMapping("base")
public class BaseController {
    private static final Logger log= LoggerFactory.getLogger(BaseController.class); /** * @requestMapping (value ="/response",method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse response(String name){
        BaseResponse response=new BaseResponse(StatusCode.Success);
        if (StringUtils.isBlank(name)){
            name="This is welcome!";
        }
        response.setData(name);
        returnresponse; }}Copy the code

(6) the whole running, if there is no corresponding error message console, open a Postman, and a corresponding request: http://localhost:8092/kill/base/response? Name =Java seconds kill system (port with the following is their own Settings!) , you can view the response information, as follows:



(7) In addition, the Java second kill system project also supports the front-end to initiate a request, the back-end to assist the page jump, which the project uses the template engine for Jsp, The jump page is located in the/web-INF /views/ directory (this is done mainly through the configuration in the application.properties file). The following code develops a method to jump to the welcome page in BaseController, as follows:

/** * jump page - Jump successfully carries the name parameter to the welcome page * @param name * @param modelMap * @return
 */
@GetMapping("/welcome")
public String welcome(String name, ModelMap modelMap){
    if (StringUtils.isBlank(name)){
        name="This is welcome!";
    }
    modelMap.put("name",name);
    return "welcome";
}Copy the code

(8) to open the browser, visit: http://localhost:8092/kill/base/welcome? Name =Java seckill system can jump to the corresponding page! At this point, the construction of the multi-module project for the “Java Seckill System” is complete! It is worth mentioning that this multi-module project can be applied to any other SpringBoot business application scenario and can be used as a cornerstone project.

Next, we can start doing some bad things, and that’s where the whole Java seckill system comes into play!

supplement

1, at present, the overall construction of the second kill system and code combat has been completed, the complete source code database address can be downloaded here: gitee.com/steadyjack/… Remember Fork and Star!!