广告位联系
返回顶部
分享到

解决java中mybatis报错:org.apache.ibatis.binding.BindingException:

java 来源:互联网 作者:佚名 发布时间:2024-03-29 22:54:55 人浏览
摘要

报错信息 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.ja

报错信息

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

/**

 * 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/**

 * 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

/**

 * 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

/**

 * 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

1

2

3

4

5

6

7

8

9

10

11

<?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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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文件中添加以下配置:

1

mybatis.mapper-locations=classpath:mapper/*.xml

拓展

上述代码中即在DemoApplication中添加了@MapperScan注解,又在UserMapper类中添加了@Mapper注解。

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

我这边习惯全加了

代码运行展示:


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计