SpringBoot整合MyBatis --- mapper映射文件无法加载及路径问题

mapper映射文件无法加载及路径问题

问题:

在SpringBoot整合MyBatis时,若mapper映射文件在java路径下,就会一直报错Invalid bound statement (not found): xxx.xxxMapper.selectxx
在这里插入图片描述

说明Mapper接口是可以调用到的,问题就出在无法映射到Mapper.xml文件。

同时查看输出target目录下,没有xxxmapper.xml文件的编译结果。

原因:

SpringBoot默认配置情况下,只有resources路径下的xml文件会被扫描。

解决办法:

方法一:【推荐使用】

将mapper映射文件放在resources目录下,通常会在resources目录下建一个mapper目录,存放各种mapper的映射文件,
在这里插入图片描述
并且在application.properties中配置 Mapper映射文件的路径为:

#指定myBatis的核心配置文件与Mapper映射文件
mybatis.mapper-locations=classpath:mapper/*.xml

测试成功,查看target目录下,有xxxMapper.xml文件的编译结果。
在这里插入图片描述

方法二:

mapper映射文件依旧放在java目录下,
在这里插入图片描述
此时,需要在pom.xml文件中添加java路径下xml文件扫描配置,

 <build>
     <resources>
         <resource>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
         </resource>
         <resource>
            <directory>${basedir}/src/main/java</directory>
            <excludes>
               <exclude>**/*.xml</exclude>
            </excludes>
          </resource>
     </resources>
</build>

并且在application.properties中配置 Mapper映射文件的路径时要加上包名,

#mybatis.mapper-locations=classpath:包名/*.xml
mybatis.mapper-locations=classpath:org.xiao.dao/*.xml

测试成功,查看target目录下,有xxxMapper.xml文件的编译结果。
在这里插入图片描述

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是 Spring Boot 整合 MyBatis-Plus 进行分页查询的示例代码: 首先,需要在 pom.xml 中添加 MyBatis-Plus 和 MySQL 的依赖: ```xml <dependencies> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 然后,需要在 application.properties 中配置 MySQL 数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 接下来,创建一个实体类和对应的 Mapper 接口: ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } @Mapper public interface UserMapper extends BaseMapper<User> { IPage<User> selectUserPage(Page<User> page, @Param("name") String name); } ``` 其中,User 类是实体类,UserMapperMapper 接口,继承了 MyBatis-Plus 的 BaseMapper 接口。 注意,在 UserMapper 接口中,我们定义了一个名为 selectUserPage 的方法,它接受一个 Page 对象和一个 name 参数,返回一个 IPage 对象。这个方法将在 Controller 中调用,用于分页查询用户信息。 最后,编写 Controller 类,通过调用 UserMapper 中定义的 selectUserPage 方法来进行分页查询: ```java @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public IPage<User> getUserList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String name) { Page<User> page = new Page<>(pageNum, pageSize); return userMapper.selectUserPage(page, name); } } ``` 在 getUserList 方法中,我们首先创建了一个 Page 对象,表示要查询的分页信息。然后,调用 userMapper.selectUserPage 方法来进行分页查询,并将结果返回给前端。 最后,这里提供一个 Mapper.xml 文件的示例,用于实现分页查询: ```xml <select id="selectUserPage" resultMap="BaseResultMap"> select * from user <if test="name != null"> where name like concat('%', #{name}, '%') </if> <if test="orderBy != null"> order by ${orderBy} </if> </select> ``` 其中,selectUserPage 方法的实现与上面的示例代码类似,这里不再赘述。注意,这里使用了 MyBatis 的动态 SQL 功能,根据传入的参数来动态生成 SQL 语句。具体来说,如果传入了 name 参数,则会在 SQL 语句中添加一个 where 子句,用于按照用户名进行模糊查询;如果传入了 orderBy 参数,则会在 SQL 语句中添加一个 order by 子句,用于按照指定字段排序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值