BES-Multi-Module Springboot Project MyBatis Universal Mapper Configuration (Controller Service Dao in Different Submodules)

Keywords: Mybatis Spring xml MySQL

The project structure previously written is similar to the figure below and is not separated into sub-modules.

The new project requires the separation of modules, which are structured as follows

Project and sub-module structure

 

Configuration in Controller Submodule

pom.xml

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

        <!--mybatis currency mapper rely on-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>

General Mapper-related configurations are placed in application.yml in the Controller sub-module, not in the dao or service module. Only the controller module is the SpringBoot project, and the rest are common maven projects.

application.yml

server:
  port: 10001
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/db_user_center?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: XXXX
    password: XXXX
#mybatis configuration
mybatis:
  #The package name of the entity class
  type-aliases-package: cn.ztanker.bes.userpermission.pojo.entity
#Universal mapper configuration
mapper:
  #Common Interface Class Path
  mappers: tk.mybatis.mapper.common.Mapper
  identity: MYSQL

 

Configuration in Service Submodule

No related configuration, all through the call dao layer interface to get data.

Configuration in Dao sub-module

pom.xml

        <!--Mybatis rely on-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--mybatis currency mapper rely on-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--Database Connection Dependence-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
            <scope>runtime</scope>
        </dependency>

About the configuration of MyBatis:

1. MyBatis's xml file should be placed in the resources folder of the dao project.

2. The configuration of MyBatis is in resources/application.yml of the controller sub-module. Note that the classpath has a "*" sign, otherwise the xml file in the dao subproject will not be scanned.

mybatis:
  mapper-locations: classpath*:mapper/*.xml

Note: As multiple modules are split, the Maven cycle dependency problem caused by interdependence should be avoided.

Posted by jomama on Mon, 30 Sep 2019 07:37:35 -0700