springboot集成Mybatis出现mapper层接口扫描不到的错误

本文介绍了在SpringBoot中集成Mybatis时,遇到Mapper层接口扫描不到的问题及解决方法。需要在主程序中配置@MapperScan指定扫描的包名,Controller层使用@Autowired注解进行自动注入。此外,还需要在SpringBoot全局配置文件中设置mapper.xml和mybatis配置文件的路径。若要启用驼峰式命名,可在mybatis的全局配置文件中进行设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

springboot中集成mybatis使用mapper.xml文件

  1. 主程序中要配置@MapperScan(“扫描的包名”)
@MapperScan("com.guigu.springboot.mapper")
@SpringBootApplication

public class Springboot06DataMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot06DataMybatisApplication.class, args);
    }

}

  1. controller层用@Autowired注解进行自动注入
    @Autowired
    TeacherMapper mapper;
  1. springboot全局配置文件,配置了mapper.xml文件和mybatis配置文件的路径才会将这些属性和properties绑定

mybatis:
  config-location: classpath:mybatis-config.xml
  check-config-location: true
  mapper-locations: classpath:/mappers/*.xml

前面这几部分都必须要有.

如果想要设置驼峰式命名,在mybatis的全局配置文件中这样设置.
它其他的设置参数可以参考github上发布的文档
settings配置参数文档

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <settings>
      <setting name="mapUnderscoreToCamelCase" value="true"/>
  </settings>

</configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值