java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis报错:org.apache.ibatis.binding.BindingException:Invalid bound statement(not found):xx

解决java中mybatis报错:org.apache.ibatis.binding.BindingException:Invalid bound statement(not found):xx问题

作者:水巷石子

这篇文章主要介绍了解决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) ......

代码如下

/**
 * 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;
    }
}
/**
 * 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();
}

/**
 * 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();
    }
}

/**
 * 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();
}

<?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>
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注解。

其实这两个只需要加一个即可。

我这边习惯全加了

代码运行展示:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文