Spring Boot + MyBatis module project construction

Keywords: Java Spring Boot intellij-idea

1, Foreword

Recently, the company's project is ready to start reconstruction. The framework is selected as SpringBoot+Mybatis. This article mainly records the process of building a 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

2, Construction steps
1. Create parent project
IDEA toolbar, select menu file - > New - > Project

Select Spring Initializr. By default, select Default and click Next

Fill in the input box and click Next

You do not need to select Next directly in this step

Click Finish to create the project

The final project directory structure is as follows

Delete the useless. mvn directory, src directory, mvnw and mvnw.cmd files, and finally leave only. gitignore and pom.xml

2. Create sub module
Select the project root directory beta, right-click the call out menu, and select new - > module

Select Maven and click Next

Fill in ArifactId and click Next

Modify the Module name, add a horizontal bar to improve readability, and click Finish

Similarly, add beta Dao and beta web sub modules to get the project directory structure, as shown in the figure below

3. Run project
Create the com.yibao.beta.web package in 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

@SpringBootApplication
public class BetaWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(BetaWebApplication.class, args);
    }
}

Add the controller directory in the com.yibao.beta.web package, create a new controller, and add the test method to test whether the interface can be accessed normally

@RestController
@RequestMapping("demo")
public class DemoController {

    @GetMapping("test")
    public String test() {
        return "Hello World!";
    }
}

Run the main method in the BetaWebApplication class to start the project. The default port is 8080. Access http://localhost:8080/demo/test The following results are obtained

4. Configure dependencies between modules
Dependencies of each sub module: biz layer depends on dao layer, and web layer depends on biz layer
Declare all sub module dependencies in the parent pom file (refer to the documentation for the difference between dependency management and dependencies)

<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
Add a beta biz dependency to the pom file in the beta web layer

<dependencies>
    <dependency>
        <groupId>com.yibao.beta</groupId>
        <artifactId>beta-biz</artifactId>
    </dependency>
</dependencies>
stay beta-biz In layer pom Add to file beta-dao rely on
<dependencies>
    <dependency>
        <groupId>com.yibao.beta</groupId>
        <artifactId>beta-dao</artifactId>
    </dependency>
</dependencies>
  1. web layer calling biz layer interface test
    Create the com.yibao.beta.biz package in the beta biz layer, add the service directory, create the DemoService interface class in it, and search the web through wechat_ Resource get more push
public interface DemoService {
    String test();
}
@Service
public class DemoServiceImpl implements DemoService {

    @Override
    public String test() {
        return "test";
    }
}

DemoController injects DemoService through @ Autowired annotation, and modifies the test method of DemoController to call the test method of DemoService. The final result is as follows:

package com.yibao.beta.web.controller;@RestController
@RequestMapping("demo")
public class DemoController {

    @Autowired
    private DemoService demoService;

    @GetMapping("test")
    public String test() {
        return demoService.test();
    }
}

Run the main method in the BetaWebApplication class again to start the project. The following error is found

***************************
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 the DemoService class cannot be found. At this time, you need to add package scanning in the BetaWebApplication entry class, and set the value of scanBasePackages in the @ SpringBootApplication annotation to com.yibao.beta, as shown below

package com.yibao.beta.web;

@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);
    }
}

After setting, re run the main method, the project starts normally, and access http://localhost:8080/demo/test The following results are obtained

  1. Integrated Mybatis
    Declare mybatis spring boot starter and lombok dependencies 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>

Add 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>

Create the com.yibao.beta.dao package in the beta Dao layer, and generate the Dao layer related files (DO, Mapper, xml) through the mybatis genertaor tool. The storage directory is as follows

Add corresponding configuration items for jdbc and mybatis in the applicatio.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 the @ Autowired annotation into UserMapper, and modifies the test method of DemoService to call the selectByPrimaryKey method of UserMapper, as shown below
package com.yibao.beta.biz.service.impl;

@Service
public class DemoServiceImpl implements DemoService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String test() {
        UserDO user = userMapper.selectByPrimaryKey(1);
        return user.toString();
    }
}

Run the main method in the BetaWebApplication class again to start the project. The following error is found

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 time, you need to add Dao layer package scanning in the BetaWebApplication entry class, add @ MapperScan annotation and set its value to com.yibao.beta.dao.mapper, as shown below

package com.yibao.beta.web;

@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);
    }
}

After setting, re run the main method, the project starts normally, and access http://localhost:8080/demo/test The following results are obtained

So far, a simple SpringBoot+Mybatis multi module project has been built. We also verify its correctness by starting the project call interface.
4, Summary
A hierarchical multi module engineering structure is not only convenient for maintenance, but also conducive to the follow-up microservice. On the basis of this structure, you can also extend the common layer (public components), the server layer (such as dubbo's external services) and wechat search web_resource get more push
This is the first step of the project reconfiguration, and the components such as logback, discon, redis and dubbo will be integrated in the subsequent frameworks
5, Pits not mentioned
In the process of building Maven private server, we also encountered a problem. The reason is that the central warehouse configured for Maven private server in the company is the remote warehouse of Alibaba. Compared with the remote warehouse provided by maven, some jar packages have incomplete versions. As a result, the project cannot be started because the corresponding jar package is not pulled several times during the process of building.

Posted by Backara_Drift on Mon, 25 Oct 2021 18:10:30 -0700