idea design spring boot multi-module development one [build]

1. In order to facilitate program development, people gradually use the springboot multi-module approach to build projects
    This case adopts the design structure of a question and answer project: project name answers

  1. Create parent project
  2. new module creates several module projects
  3. Configure the pom.xml dependency of the parent project answers
  4. Configure the interdependence between modules pom.xml
  5. Show project structure
  6. create user
  7. service code
  8. control layer code
  9. postman test
  10. Pack

1. Use the spring initializr that comes with idea to create a new parent project answers, delete other components and leave only pom.xml

2. Right-click on the project name, new module, and create a new sub-project answers-web [for the controller layer]

                                       New sub-project answers-service [for service layer]

                                       New sub-project answers-domain [for entity entity class layer]

3. Configure the pom.xml dependency of the parent project answers

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.zes</groupId>
	<artifactId>answers</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>answers</name>
	<description>Demo many project for one Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<modules>
		<module>answers-web</module>
		<module>answers-service</module>
		<module>answers-domain</module>
	</modules>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.42</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</dependency>
	</dependencies>

</project>

4. Configure the pom.xml of answers-web and add the required project dependencies to other modules in the same way

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.zes</groupId>
	<artifactId>answers-web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>answers-web</name>
	<description>Demo-web for Spring Boot</description>

	<parent>
		<groupId>com.zes</groupId>
		<artifactId>answers</artifactId>
		<version>1.0.0-SNAPSHOT</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.zes</groupId>
			<artifactId>answers-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.zes</groupId>
			<artifactId>answers-domain</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
        <!-- Whichever module of the following build is responsible for starting the project will configure the pom.xml of which project module-this answers-web project is the main startup project, so add this configuration here-->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

5. At this time, the project structure is as follows


6. Write user class in answers-domain


7. Write logical business code in answer-service


UserService.java

package com.zes.answers.service;

import com.zes.answers.domain.User;

/**
 * TODO class description
 *
 * @author honghe
 */
public interface UserService {
    User findUserById(Long id);
}

UserServiceImpl.java

package com.zes.answers.service.Impl;

import org.springframework.stereotype.Service;

import com.zes.answers.domain.User;
import com.zes.answers.service.UserService;

/**
 * TODO class description
 *
 * @author honghe
 */
@Service
public class UserServiceImpl implements UserService {

    @Override
    public User findUserById(Long id) {
        User u = new User();
        u.setId (id);
        u.setName("admin"+String.valueOf(id));
        return u;
    }
}

8.answer-web write control layer

UserController.java

package com.zes.answers.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zes.answers.service.UserService;

/**
 * TODO class description
 *
 * @author honghe
 */
@RequestMapping("/users")
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/getUserById")
    @ResponseBody
    public Map<String,Object> getUserById(String id){
        Map<String,Object> resutl = new HashMap<>();
        resutl.put("code","100");
        resutl.put("user",userService.findUserById(Long.valueOf(id)));
        return resutl;
    }
}

9. Start application.java in answer-web to run the project and use postman to access the interface test


10. Compile and package mvn install -DskipTests



11. Project git address https://gitee.com/zhaoershuang/answers.git

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325744630&siteId=291194637