I. Preface
Recently, the company's project is ready to start refactoring. The framework is chosen as SpringBoot+Mybatis. This article mainly records the process of building SpringBoot multi-module project in IDEA.
1. Development Tools and System Environment
-
IDE: IntelliJ IDEA 2018.2
-
System environment: mac OSX
2. Project directory structure
-
biz layer: business logic layer
-
dao layer: data persistence layer
-
web Layer: Request Processing Layer
II. Construction steps
1. Creating a parent project
IDEA toolbar selection menu File - > New - > Project...
img
(2) Select Spring Initializr, Initializr defaults Default, and click Next
img
(3) Fill in the input box and click Next
img
(4) This step does not require the selection of the direct point Next
img
Click Finish to create the project
img
The structure of the final project catalogue is as follows.
img
_Delete useless. mvn directory, src directory, mvnw and mvnw.cmd files, and ultimately only. gitignore and pom.xml
img
2. Creating sub-modules
(1) Select the project root directory beta right-click call-out menu, and select New-> Module
img
Select Maven and click Next
img
(3) Fill in ArifactId and click Next
img
(4) Modify Module name to increase readability of horizontal bar, click Finish
img
(5) Similarly, add sub-modules of [beta-dao], [beta-web], and finally get the project directory structure as follows
img
3. Operation Project
(1) Create a com.yibao.beta.web package at the beta-web layer (note: this is a multi-layer directory structure, not a single directory name, com > > Yibao > > beta > web) and add the entry class BetaWebApplication.java
package com.yibao.beta.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author linjian * @date 2018/9/29 */ @SpringBootApplication public class BetaWebApplication { public static void main(String[] args) { SpringApplication.run(BetaWebApplication.class, args); } }
(2) Add the controller directory to the com.yibao.beta.web package and create a new controller. Add the test method to test whether the interface can be accessed normally.
package com.yibao.beta.web.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author linjian * @date 2018/9/29 */ @RestController @RequestMapping("demo") public class DemoController { @GetMapping("test") public String test() { return "Hello World!"; } }
(3) Run the main method in the Beta Web Application class to start the project. The default port is 8080. Visit http://localhost:8080/demo/test to get the following results.
img
Although the above project can start normally, but the dependencies between modules have not been added, the following continue to improve.
4. Dependency between configuration modules
Dependencies of each sub-module: the biz layer relies on the dao layer, and the web layer relies on the biz layer
(1) Declare all dependency management and dependencies in the parent pom file for self-referring to the document
<dependencyManagement> <dependencies> <dependency> <groupId>com.yibao.beta</groupId> <artifactId>beta-biz</artifactId> <version>${beta.version}</version> </dependency> <dependency> <groupId>com.yibao.beta</groupId> <artifactId>beta-dao</artifactId> <version>${beta.version}</version> </dependency> <dependency> <groupId>com.yibao.beta</groupId> <artifactId>beta-web</artifactId> <version>${beta.version}</version> </dependency> </dependencies> </dependencyManagement>
Where ${beta.version} is defined in the properties tag
(2) Adding beta-biz dependencies to pom files in beta-web layer
<dependencies> <dependency> <groupId>com.yibao.beta</groupId> <artifactId>beta-biz</artifactId> </dependency> </dependencies>
(3) Adding beta-dao dependency to the pom file in beta-biz layer
<dependencies> <dependency> <groupId>com.yibao.beta</groupId> <artifactId>beta-dao</artifactId> </dependency> </dependencies>
5. web Layer Calls biz Layer Interface Testing
(1) Create com.yibao.beta.biz package in beta-biz layer, add service directory and create DemoService interface class in it.
package com.yibao.beta.biz.service; /** * @author linjian * @date 2018/9/29 */ public interface DemoService { String test(); }
package com.yibao.beta.biz.service.impl; import com.yibao.beta.biz.service.DemoService; import org.springframework.stereotype.Service; /** * @author linjian * @date 2018/9/29 */ @Service public class DemoServiceImpl implements DemoService { @Override public String test() { return "test"; } }
(2) DemoController injects DemoService through the @Autowire annotation, modifies DemoController's test method to call DemoService's test method, and finally the following
package com.yibao.beta.web.controller; import com.yibao.beta.biz.service.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author linjian * @date 2018/9/29 */ @RestController @RequestMapping("demo") public class DemoController { @Autowired private DemoService demoService; @GetMapping("test") public String test() { return demoService.test(); } }
③ run the main method in the betawetapplication class again to start the project, and an error is reported as follows:
*************************** APPLICATION FAILED TO START *************************** Description: Field demoService in com.yibao.beta.web.controller.DemoController required a bean of type 'com.yibao.beta.biz.service.DemoService' that could not be found. Action: Consider defining a bean of type 'com.yibao.beta.biz.service.DemoService' in your configuration.
The reason is that you can't find the DemoService class. At this point, you need to add a package scan to the Beta Web Application entry class and set the scanBasePackages value in the @SpringBootApplication annotation to com.yibao.beta, which is finally as follows
package com.yibao.beta.web; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author linjian * @date 2018/9/29 */ @SpringBootApplication(scanBasePackages = "com.yibao.beta") @MapperScan("com.yibao.beta.dao.mapper") public class BetaWebApplication { public static void main(String[] args) { SpringApplication.run(BetaWebApplication.class, args); } }
Rerun the main method after setting up, and the project starts normally. Visit http://localhost:8080/demo/test to get the following results
img
6. Integrating Mybatis
(1) The mybatis-spring-boot-starter and lombok dependencies are declared in the parent pom file
dependencyManagement> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> </dependency> </dependencies> </dependencyManagement>
(2) Adding the above dependencies to the pom file in the beta-dao layer
<dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
(3) Create com.yibao.beta.dao package in beta-dao layer and generate Dao layer related files (DO, Mapper, xml) through mybatis-genertaor tool. The directory is as follows.
img
(4) jdbc and mybatis configuration items are added to application. properties file
spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://192.168.1.1/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = test spring.datasource.password = 123456 mybatis.mapper-locations = classpath:mybatis/*.xml mybatis.type-aliases-package = com.yibao.beta.dao.entity
DemoService injects UserMapper through the @Autowired annotation, modifies DemoService's test method to call UserMapper's selectByPrimaryKey method, and ultimately the following
package com.yibao.beta.biz.service.impl; import com.yibao.beta.biz.service.DemoService; import com.yibao.beta.dao.entity.UserDO; import com.yibao.beta.dao.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author linjian * @date 2018/9/29 */ @Service public class DemoServiceImpl implements DemoService { @Autowired private UserMapper userMapper; @Override public String test() { UserDO user = userMapper.selectByPrimaryKey(1); return user.toString(); } }
(5) Run the main method in the Beta Web Application class again to start the project and find the following error
APPLICATION FAILED TO START *************************** Description: Field userMapper in com.yibao.beta.biz.service.impl.DemoServiceImpl required a bean of type 'com.yibao.beta.dao.mapper.UserMapper' that could not be found. Action: Consider defining a bean of type 'com.yibao.beta.dao.mapper.UserMapper' in your configuration.
The reason is that the UserMapper class cannot be found. At this point, you need to add a Dao layer package scan to the Beta Web Application entry class, add the @MapperScan annotation and set its value to com.yibao.beta.dao.mapper, which is finally shown below.
package com.yibao.beta.web; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author linjian * @date 2018/9/29 */ @SpringBootApplication(scanBasePackages = "com.yibao.beta") @MapperScan("com.yibao.beta.dao.mapper") public class BetaWebApplication { public static void main(String[] args) { SpringApplication.run(BetaWebApplication.class, args); } }
Rerun the main method after setting up, and the project starts normally. Visit http://localhost:8080/demo/test to get the following results
img
So far, a simple SpringBoot+Mybatis multi-module project has been built, and we also verify its correctness by starting the project call interface.
Four, summary
A hierarchical multi-module engineering structure is not only convenient to maintain, but also conducive to the subsequent micro-service. On the basis of this structure, the common layer (common components) and server layer (such as dubbo services) can also be extended.
This is the first step in project refactoring, and the following frameworks will integrate logback, disconf, redis, dubbo and other components.
5. Unmentioned pits
In the process of building, there is also a problem of Maven private service. The reason is that the central warehouse of Maven private service is Ali's remote warehouse. Compared with maven's own remote warehouse, some jar package versions are incomplete, which leads to several times that the project can not start because the corresponding jar package is not pulled in the process of building.