解决java中mybatis报错:org.apache.ibatis.binding.BindingException:Invalid bound statement(not found):xx问题
报错信息
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): springtransactionaldemo.demo.mapper.UserMapper.selectAll at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.9.jar:3.5.9] at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.9.jar:3.5.9] at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:108) ......
代码如下
- UserController
/** * Created By 李柏霖 * 2024/1/13 23:01 */ package springtransactionaldemo.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import springtransactionaldemo.demo.pojo.User; import springtransactionaldemo.demo.service.IUserService; import java.util.List; @Controller public class UserController { @Autowired private IUserService userService; @GetMapping("/getAllUsers") @ResponseBody public List<User> getAllUsers() { List<User> users = userService.selectAllUsers(); System.out.println(users); return users; } }
- IUserService
/** * Created By 李柏霖 * 2024/1/13 22:24 */ package springtransactionaldemo.demo.service; import springtransactionaldemo.demo.pojo.User; import java.util.List; public interface IUserService { List<User> selectAllUsers(); }
- IUserServiceImpl
/** * Created By 李柏霖 * 2024/1/13 22:25 */ package springtransactionaldemo.demo.service.Impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import springtransactionaldemo.demo.mapper.UserMapper; import springtransactionaldemo.demo.pojo.User; import springtransactionaldemo.demo.service.IUserService; import java.util.List; @Service public class IUserServiceImpl implements IUserService { @Autowired private UserMapper userMapper; @Override @Transactional public List<User> selectAllUsers() { return userMapper.selectAll(); } }
- UserMapper
/** * Created By 李柏霖 * 2024/1/13 22:22 */ package springtransactionaldemo.demo.mapper; import org.apache.ibatis.annotations.Mapper; import springtransactionaldemo.demo.pojo.User; import java.util.List; @Mapper public interface UserMapper { List<User> selectAll(); }
- UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="springtransactionaldemo.demo.mapper.UserMapper"> <!-- MyBatis 映射文件 --> <select id="selectAll" resultType="springtransactionaldemo.demo.pojo.User"> SELECT * FROM user </select> </mapper>
- DemoApplication
package springtransactionaldemo.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @EnableTransactionManagement @MapperScan("springtransactionaldemo.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
解决办法
在 application.properties文件中添加以下配置:
mybatis.mapper-locations=classpath:mapper/*.xml
拓展
上述代码中即在DemoApplication中添加了@MapperScan注解,又在UserMapper类中添加了@Mapper注解。
其实这两个只需要加一个即可。
我这边习惯全加了
代码运行展示:
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
- mybatis整合springboot报BindingException:Invalid bound statement (not found)异常解决
- 使用mybatis报Invalid bound statement解决分析
- 解决微服务下Mybatis xml无效绑定问题及分析Invalid bound statement
- mybatis创建项目报Invalid bound statement (not found)错误解决方法
- SpringBoot使用MyBatis-Plus解决Invalid bound statement异常
- 解决mybatis-plus通用mapper调用报错:Invalid bound statement
- mybatis Invalid bound statement(not found)排坑记录
相关文章
Spring @Scheduler使用cron表达式时的执行问题详解
Spring给程序猿们带来了许多便利。下面这篇文章主要给大家介绍了关于Spring @Scheduler使用cron表达式时的执行问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧2018-09-09springboot+mybatis配置clickhouse实现插入查询功能
这篇文章主要介绍了springboot+mybatis配置clickhouse实现插入查询功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-08-08
最新评论