1. Create a parent project, file > New > project, as shown in the following figure:
Click Project to open the following interface:
Click Next, as shown below:
Note: because springboot is an external network, it may not be able to be opened. It needs to be opened through an agent. The agent recommends using SSR win 4.7.0-fix (download address: https://www.myitmx.com/30.html )After downloading, unzip it, as shown in the following figure:
Click Next, as shown below:
Click Next, as shown below:
Click Finish and the parent project is created successfully. Delete the redundant directories, as shown in the following figure:
2. Create subproject (web layer)
Click Project Structure, as shown below:
Click the + sign, as shown below:
The interface after selecting New Module is the same as the page of closing parent project, as shown in the following figure:
After creation, delete redundant directories as well. The copied parts are required for external tomcat startup, as shown in the following figure:
The methods of creating core (including service, mapper, model, etc.) and common (tool class) are the same as those of web. We will not talk about them one by one here. The final project structure is as follows:
3. Configure pom file
Configure pom.xml of the parent directory (test)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> </parent> <groupId>com.example</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!--This has to be pom,Otherwise, when the subproject is introduced jar Unable to import--> <name>test</name> <!--Configure subprojects--> <modules> <module>web</module> <module>core</module> <module>common</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
Configure pom.xml of web
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>web</name> <!--Configure parent project and version, import parent, parent pom Introduced in jar,properties Configuration and so on. The children can inherit and do not need to be reconfigured.--> <parent> <groupId>com.example</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <!--Introduce the public part( service,common)Of jar--> <dependency> <groupId>com.example</groupId> <artifactId>core</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> </project>
Configure pom. xml of core
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>core</artifactId> <version>0.0.1-SNAPSHOT</version> <name>core</name> <!--Configure parent project and version, import parent, parent pom Introduced in jar,properties Configuration and so on. The children can inherit and do not need to be reconfigured.--> <parent> <groupId>com.example</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <!--Introduce the public part( service,common)Of jar--> <dependency> <groupId>com.example</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project>
Configure pom.xml of common
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>common</name> <!--Configure parent project and version, import parent, parent pom Introduced in jar,properties Configuration and so on. The children can inherit and do not need to be reconfigured.--> <parent> <groupId>com.example</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> </dependencies> </project>
4. Configure the application.properties file
application.properties under web
server.port=8080 #What is used here is that each subproject uses its own application.properties configuration #You can also use only web application.properties to write all configurations here #Introduce the application.properties configuration of each subproject # Subproject name application name. properties can only write the name after the bar when it is introduced, separated by multiple commas # Both include and active are property files. # The function of include is to activate a new profile by superposition, and the profile contained in active may be overridden by a higher priority definition. spring.profiles.active=core,common #spring.profiles.include=
application-core.properties under core
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8 spring.datasource.username=mysql spring.datasource.password=root mybatis.mapperLocations=classpath:mybatis/**/*.xml mybatis.typeAliasesPackage=com.example.core.model #Print the path of mapper after sql log logging.level logging.level.com.example.core.mapper=debug
The application-common.properties under common is configured according to the requirements, which is not configured here.
5. test
Create a tool class under common
StringUtil.java
package com.example.common; import org.apache.commons.lang.StringUtils; public class StringUtil { public static boolean isNotEmpty(Object obj) { return (null != obj && StringUtils.isNotBlank(String.valueOf(obj))); } public static boolean isEmpty(Object obj) { return (null == obj || StringUtils.isBlank(String.valueOf(obj))); } }
Create mode, mapper, mapper.xml and service under core
TestModel.java
package com.example.core.model; public class TestModel { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
TestMapper.java
package com.example.core.mapper; import com.example.core.model.TestModel; import org.springframework.stereotype.Component; @Component public interface TestMapper { public TestModel get(Integer id); }
test.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.core.mapper.TestMapper"> <select id="get" parameterType="Integer" resultType="testModel"> select ID, NAME from test where ID = #{id} </select> </mapper>
TestService.java
package com.example.core.service; import com.example.core.model.TestModel; public interface TestService { public TestModel get(Integer id); }
TestServiceImpl.java
package com.example.core.service.impl; import com.example.core.mapper.TestMapper; import com.example.core.model.TestModel; import com.example.core.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class TestServiceImpl implements TestService { @Autowired private TestMapper testMapper; @Override public TestModel get(Integer id) { return testMapper.get(id); } }
Creating control layer classes under the web
TestController.java
package com.example.web.controller; import com.example.common.StringUtil; import com.example.core.model.TestModel; import com.example.core.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("test") public class TestController { //A red line will appear under the annotation of this service in the idea, which means that this bean has not been found. //That's because when scanning automatically, you only scan the same level directory. //The interface of other subprojects is introduced, so the ComponentScan annotation needs to be added to the WebApplication.java startup class. @Autowired private TestService testService; @ResponseBody @RequestMapping("show") public String ShowAll() { TestModel testModel = testService.get(1); if(null != testModel && StringUtil.isNotEmpty(testModel.getName())){ return testModel.getName(); } return "No results found"; } }
Configure the springboot startup class WebApplication.java
package com.example.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication //ComponentScan annotation configures the path for automatic scanning. //Here, you can scan all the items under com, or you can refer to the directory. Multiple items are separated by commas. @ComponentScan({"com"}) //Configure the directory where the mapper class is located //If it is not configured, the failure to create a mapper at startup cannot be injected into the service. @MapperScan("com.example.core.mapper") public class WebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(WebApplication.class); } public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } }
6. Start up project
Spring boot can be used
You can also start with external tomcat, but you need some configuration, which is already configured in this article
pom.xml under web needs to be introduced
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
WebApplication.java startup class, as shown in the following figure:
Note: if spring boot2.2 uses tomcat to start, an error will be reported, which is like mybaits compatibility problem:
Solve this problem
Reduce spring boot to 2.1.5 and mybaits to 2.0.1
Or add spring.properties configuration under core, as shown below:
Note: if tomcat 8.0 is used in springboot 2.2, the compatible version will be reported, but the startup will not be affected. However, tomcat 8.5 and above is recommended. The error message is as follows:
Open the browser after starting successfully and enter the address to view the result