SpringBoot-整合MyBatis

SpringBoot-整合MyBatis

Spring没有提供MyBatis的启动器,由MyBatis提供启动器(整合依赖包)

mybatis-spring-boot-starter


干掉配置类!!!

SpringBoot的主要目的就是尽量干掉配置类,使用application.yaml配置文件的方式替换配置类

1
2
3
4
5
6
7
8
# mybatis配置(彻底抛弃mybatis-config.xml)
mybatis:
mapper-locations: classpath:/mapper/*.xml # 指定mapper.xml文件的位置
type-aliases-package: com.xiaobai.pojo # 给实体类起别名
configuration:
map-underscore-to-camel-case: true # 开启自动驼峰式映射
auto-mapping-behavior: full # 开启resultMap自动映射
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl # 开启logback日志输出

当我们使用SpringBoot配置了mapper-locations属性后,软件包和资源目录的mapper结构也就无须相同


包扫描

我们之前在配置类中向IoC容器注入mapperScannerConfigurer对象,以完成mapper包的扫描

而使用SpringBoot,我们只需要在启动类上使用@MapperScan注解,即可完成mapper包的扫描

1
2
3
4
5
6
7
@SpringBootApplication
@MapperScan("com.xiaobai.mapper")
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}

配置pageHelper

Spring没有提供pageHelper的启动器,由pageHelper提供启动器(整合依赖包)

pagehelper-spring-boot-starter

1
2
pagehelper:
helper-dialect: mysql


SpringBoot-整合MyBatis
http://blog.170827.xyz/2024/07/02/SpringBoot-整合MyBatis/
作者
XIAOBAI
发布于
2024年7月2日
许可协议