引入Mybatis的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>

不管是注解版还是配置文件形式,Mapper都需要使用@Mapper注解标注加入Spring容器,或者使用MapperScan来规定范围,不用在单独的设置
@Mapper
public interface DeptDao {
//使用@MapperScan标注,表示该包下的所有类都会自动标注为Mapper
@MapperScan("com.bootdo.*.dao")
@SpringBootApplication
public class BootdoApplication {
public static void main(String[] args) {
SpringApplication.run(BootdoApplication.class, args);
创建对应的Mapper.xml文件

在全局配置文件中配置Mybatis的相关配置,让SpringBoot能够找到对应Mapper与其xml
mybatis:
configuration: //可以使用configuration的形式指定全局配置,也可以使用config-location指定Mybatis的全局配置文件
map-underscore-to-camel-case: true
mapper-locations: mybatis/**/*Mapper.xml //设置mapper映射文件
typeAliasesPackage: com.bootdo.**.domain
//config-location: 全局配置文件位置
本文详细介绍如何在SpringBoot项目中引入Mybatis依赖,并通过@Mapper注解或@MapperScan扫描包内所有Mapper接口,实现数据库操作。同时,配置全局配置文件让SpringBoot识别并加载对应的Mapper与XML文件。
938

被折叠的 条评论
为什么被折叠?



