对springboot整合mybatis -- 不一样的看法

springboot整合mybatis

springboot整合mybatis,其实就是一句话:把mybatis的mapper接口放到spring容器中来进行管理,下面是整合的步骤

pom依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<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>
    <version>2.2.1.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.3</version>
    <scope>compile</scope>
</dependency>

application配置文件

spring.datasource.url= jdbc:mysql://127.0.0.1:3306/?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.mapper-locations=classpath:mapping/*Mapper.xml
# 如果这里统一指定了别名,在mapper.xml中,resultType可以直接使用bean中的类名,无需加上包名;
#mybatis.type-aliases-package=com.example.springboot.mybatis.bean

注解信息

至于controller、service、mapper.xml这些我就不写了,因为都是一些比较公共的配置

在整合的时候,需要添加一些注释,有两种方案:

  1. 在mapper接口(dao接口)中,使用@Mapper注解,这种方式,无需使用配置类,无需使用@MapperScan注解,即可整合
  2. 在mapper接口中,使用@Repository注解或者不添加任务注解,在全配置类上添加@MapperScan注解,并指定要扫描的包

第二点中,全配置类是指:既可以在springboot启动类上直接加@MapperScan注解
也可以自己添加一个配置类,加上@Configuration注解,这样就表示当前类是一个全配置类; 然后加上@MapperScan

# 这是方案一
@Configuration
@MapperScan("com.example.springboot.mybatis.dao")
public class OperChannelConfig {
    
    
}

//@Mapper
@Repository
public interface OperChannelMapper {
    
    
    OperChannel getOperChannelById(int id);
}

# 方案二

@Configuration
//@MapperScan("com.example.springboot.mybatis.dao")
public class OperChannelConfig {
    
    
}

@Mapper
//@Repository
public interface OperChannelMapper {
    
    
    OperChannel getOperChannelById(int id);
}

对于第一种方式:如果加了@MapperScan注解,同时也在dao接口中加了@Mapper注解,我目前debug了源码,没看到@MapperScan注解这种方式在扫描包的时候,哪里用到了@Mapper注解;我也不敢妄言说,加了@Mapper注解是无用的,只是在整合的时候,如果加了@MapperScan注解,确实是没必要加@Mapper这个注解(目前我用的是mybatis-springboot-autoconfigure-1.3.3版本)

这两种方案我都验证过,确信是可以行得通的;

这两种方案都是通过 org.mybatis.spring.mapper.ClassPathMapperScanner#doScan(java.lang.String…)来扫描的,但是两者的执行时机不一样,并且两种方式的处理类也不一样:
对于使用@MapperScan注解的这种方式,是org.mybatis.spring.annotation.MapperScannerRegistrar来处理的
对于使用@Mapper注解这种方式,是org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.AutoConfiguredMapperScannerRegistrar来完成扫描的
我在网上看到好多整合mybatis的博客中,大部分说的是:既需要添加@MapperScan注解,也要加@Mapper注解,这种也不是不对,我只是觉得,做技术还是要严谨一点

猜你喜欢

转载自blog.csdn.net/CPLASF_/article/details/107532863