No MyBatis mapper was found in '[com.briup.demo]' package. Please check your

本文介绍如何在SpringBoot项目中整合MyBatis,包括引入依赖、编写Mapper接口及配置文件、添加扫描注解等步骤,实现数据库操作。

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

刚刚开始用spring boot和mybatis整合,有点粗糙。遇到这个问题记录一下。

步骤:

1.首先pom引入依赖

<!-- mybatis依赖 -->

        <dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

2.然后写mappper接口(这里有两种方法【1.使用注解 2.配置文件】)

2.1.注解:

@Mapper
public interface UserMapper {

    @Delete("drop table user if exists")
    void dropTable();

    @Insert("insert into user(name,age) values(#{name},#{age})")
    void insert(User user);

    @Select("select id,name,age from user where name like #{name}")
    List<User> findByNameLike(String name);

    @Delete("delete from user")
    void deleteAll();

}

2.2.配置(注意mapper.xml中namespace)

3.扫包,在spring主程序上面添加这个注解

@MapperScan(basePackages = "com.xxx.mapper")

4.读取配置文件(在application.xxx中写)

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

这几部做好整个mybatis就整合在spring boot里了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值