spring boot配置mybatis出现Invalid bound statement (not found)报错的解决办法
Posted 明月清辉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot配置mybatis出现Invalid bound statement (not found)报错的解决办法相关的知识,希望对你有一定的参考价值。
背景:
spring-boot-starter-parent 2.5.6
mybatis-spring-boot-starter 2.2.0
我遇到这个报错,是因为使用idea创建xml文件是没有后缀,举个例子,比如你创建的是AccountMapper.xml,结果使用idea创建的是AccountMapper,根本就没有后缀!解决办法也很简单,加上后缀就可以了,不需要做其他额外的配置!
参考资料:
https://blog.csdn.net/weixin_45807612/article/details/120807333
https://blog.csdn.net/weixin_45913922/article/details/129408350
极简的MyBatis在Spring Boot下的配置
以我的一个项目为例。
0、项目结构:
1、POM中添加MyBatis的依赖:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency>
2、配置application.properties:
mybatis.mapper-locations=classpath:mapper/*.xml #mybatis.type-aliases-package=com.julion.onlinestudy.domain
好吧,配置已经结束了。
接下来是一个小测试。
0、Bean文件:
1 package com.julion.onlinestudy.domain; 2 3 import com.fasterxml.jackson.annotation.JsonFormat; 4 5 import java.util.Date; 6 7 public class Student { 8 private Integer id; 9 10 private String username; 11 12 private String password; 13 14 private String name; 15 16 private String gender; 17 18 private String studentNo; 19 20 private String grade; 21 22 private String major; 23 24 private Date createTime; 25 26 private Date lastEditTime; 27 28 public Integer getId() { 29 return id; 30 } 31 32 public void setId(Integer id) { 33 this.id = id; 34 } 35 36 public String getUsername() { 37 return username; 38 } 39 40 public void setUsername(String username) { 41 this.username = username == null ? null : username.trim(); 42 } 43 44 public String getPassword() { 45 return password; 46 } 47 48 public void setPassword(String password) { 49 this.password = password == null ? null : password.trim(); 50 } 51 52 public String getName() { 53 return name; 54 } 55 56 public void setName(String name) { 57 this.name = name == null ? null : name.trim(); 58 } 59 60 public String getGender() { 61 return gender; 62 } 63 64 public void setGender(String gender) { 65 this.gender = gender == null ? null : gender.trim(); 66 } 67 68 public String getStudentNo() { 69 return studentNo; 70 } 71 72 public void setStudentNo(String studentNo) { 73 this.studentNo = studentNo == null ? null : studentNo.trim(); 74 } 75 76 public String getGrade() { 77 return grade; 78 } 79 80 public void setGrade(String grade) { 81 this.grade = grade == null ? null : grade.trim(); 82 } 83 84 public String getMajor() { 85 return major; 86 } 87 88 public void setMajor(String major) { 89 this.major = major == null ? null : major.trim(); 90 } 91 92 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") 93 public Date getCreateTime() { 94 return createTime; 95 } 96 97 public void setCreateTime(Date createTime) { 98 this.createTime = createTime; 99 } 100 101 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss") 102 public Date getLastEditTime() { 103 return lastEditTime; 104 } 105 106 public void setLastEditTime(Date lastEditTime) { 107 this.lastEditTime = lastEditTime; 108 } 109 }
1、Mapper XML文件:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.julion.onlinestudy.mapper.StudentMapper" > 4 <resultMap id="BaseResultMap" type="com.julion.onlinestudy.domain.Student" > 5 <id column="id" property="id" jdbcType="INTEGER" /> 6 <result column="username" property="username" jdbcType="VARCHAR" /> 7 <result column="password" property="password" jdbcType="VARCHAR" /> 8 <result column="name" property="name" jdbcType="VARCHAR" /> 9 <result column="gender" property="gender" jdbcType="CHAR" /> 10 <result column="student_no" property="studentNo" jdbcType="VARCHAR" /> 11 <result column="grade" property="grade" jdbcType="VARCHAR" /> 12 <result column="major" property="major" jdbcType="VARCHAR" /> 13 <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> 14 <result column="last_edit_time" property="lastEditTime" jdbcType="TIMESTAMP" /> 15 </resultMap> 16 17 <sql id="Base_Column_List" > 18 id, username, password, name, gender, student_no, grade, major, create_time, last_edit_time 19 </sql> 20 21 <select id="getOneById" resultMap="BaseResultMap" parameterType="java.lang.Integer" > 22 select 23 <include refid="Base_Column_List" /> 24 from student 25 where id = #{id,jdbcType=INTEGER} 26 </select> 27 28 </mapper>
2、Mapper文件:
1 package com.julion.onlinestudy.mapper; 2 3 import com.julion.onlinestudy.domain.Student; 4 import org.apache.ibatis.annotations.Mapper; 5 6 import java.util.List; 7 import java.util.Map; 8 9 @Mapper 10 public interface StudentMapper { 11 12 Student getOneById(Integer id); 13 14 }
3、Service文件:
package com.julion.onlinestudy.service; import com.julion.onlinestudy.domain.Student; import java.util.List; import java.util.Map; public interface StudentService { Student getOneById(Integer id); }
4、Controller文件:
1 package com.julion.onlinestudy.controller; 2 3 import com.julion.onlinestudy.common.BaseResponse; 4 import com.julion.onlinestudy.domain.Student; 5 import com.julion.onlinestudy.enums.StatusCode; 6 import com.julion.onlinestudy.service.StudentService; 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.web.bind.annotation.*; 11 12 import java.util.HashMap; 13 import java.util.List; 14 15 @RestController 16 @RequestMapping("/student") 17 public class StudentController { 18 19 private Logger logger = LoggerFactory.getLogger(this.getClass()); 20 21 @Autowired 22 private StudentService service; 23 24 @GetMapping(value = "/one/{id}", produces = "application/json;charset=utf-8") 25 @ResponseBody 26 public Student getOneStudent(@PathVariable Integer id) { 27 logger.info("Controller--获取单个学生信息---Start"); 28 Student student = service.getOneById(id); 29 logger.info("Controller--获取单个学生信息---End"); 30 return student; 31 } 32 33 }
5、测试结果:
以上是关于spring boot配置mybatis出现Invalid bound statement (not found)报错的解决办法的主要内容,如果未能解决你的问题,请参考以下文章