Java game technology sharing: springboot multi module development

Time:2021-8-15

In Java Web project development, in order to facilitate later maintenance, we generally carry out layered development, which is commonly divided into model (domain model layer), Dao (database access layer), service (business logic layer), controller (controller layer) and web (presentation layer). After such layering, the responsibilities of each layer will be more clear, Later maintenance is also relatively easy.

The previous development is in one module, and different layers are distinguished by packages, as follows:

1. Create a springboot project

The structure is:

1

The codes are:

  public class User {

  private String username;

  private String password;

  private String realname;

… omit the get \ set constructor

  }

  @Repository

  public class UserDAO {

  public List

Return arrays.aslist (new user (“Zhangsan”, “111”, “Zhang San”),

New user (“Lisi”, “111”, “Li Si”),

New user (“Wangwu”, “111”, “Wang Wu”);

  }

  }

  @Service

  public class UserService {

  @Resource

  private UserDAO userDAO;

  public List

  return userDAO.selectAllUsers();

  }

  }

  @Controller

  @RequestMapping(“/user”)

  public class UserController {

  @Resource

  private UserService userService;

  @RequestMapping(“/list”)

  @ResponseBody

  public String list(){

  List

  StringBuilder stringBuilder = new StringBuilder();

  for(User user :allUsers){

  stringBuilder.append(“

Name: “;

  stringBuilder.append(user.getRealname());

  stringBuilder.append(“

“);

  }

  return stringBuilder.toString();

  }

  }

  @SpringBootApplication

  public class DemoApplication {

  public static void main(String[] args) {

  SpringApplication.run(DemoApplication.class, args);

  }

  }

Start project, test

0

Multi module development:

As the project becomes more and more complex, the scale of each layer gradually increases. Developing in one module will bring inconvenience to testing and maintenance. For large projects, each layer is generally placed in its own module, and then each layer is connected and maintained separately.

Next, we will reconstruct the previous single module project into a multi module project:

  gameThe project is divided into four modules

  controller

  service

  dao

  model

Dependencies between modules are

Controller depends on service and model

Service depends on Dao and model

Dao dependent model

Refactoring a multi module project

1. First, select pom.xml of the project and change the packaging method to POM

  

2. Create a new model module:

Right click the item – > New – > module – > select Maven – > next

2

Enter the module name model – > next – > finish

3

In the pom.xml of the project:

  

  

  

This is the completion of module creation

Then copy the entire model package in the original project to the Java directory of the model module:

3. Create a new Dao module. The process is similar to the previous one

After the creation is completed, the code will report an error because the user class cannot be found because the Dao module has not added a dependency on the model module

Add dependencies in pom.xml of Dao module

4. Add controller module

Add dependencies: then copy the demoapplication class of the original project to the basic package under the Java directory of the controller module. This example is com.qianfeng.springboot

5. Click build project to compile the project, and then start the program for testing. If it can be successfully started and the original execution effect can be seen, the reconstruction of multiple modules of the project is completed.