报错信息:
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注解。其实这两个只需要加一个即可。我这边习惯全加了
文章评论