首页 > 技术文章 > Spring Boot+Mybatis mapper文件与mapper.xml分开放

notably 2021-02-02 13:36 原文

Mybatis与SpringBoot的集成:

有关mybatis的maven依赖:

        
        <mybatis-spring-boot-starter.version>2.1.3</mybatis-spring-boot-starter.version>
        
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot-starter.version}</version>
        </dependency>

SpringBoot相关依赖:

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

application.yml配置:

mybatis:
  #开启驼峰命名
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath*:/mybatis-mapper/*Mapper.xml 

上面mapper-locations配置只要能扫描定位到Mapper.xml 即可

目录如下:

 

 如果mapper接口想与mapper.xml文件分开放的话,需要额外指定mapper接口扫描路径:

在启动类上加上注解@MapperScan  指定mapper接口扫描路径,如:

@SpringBootApplication
@MapperScan(basePackages = {"com.dabai.dao"})
public class ApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}

 

 这也mapper接口就能和mapper.xml分开放了

 

推荐阅读