Spring+mybaits配置遇到的问题
2021年,八月,在学习springboot+mybatis的时候遇到了一些问题:
1、spring启动错误
刚开始,在配置springboot+mybaits的时候,根据网络上的博客,配置application.yml,配置UserMapper等文件
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/t_blog?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: root
password: 123456
mybatis:
type-aliases-package: com.mybatislearn.mockingjay.entity
mapper-locations: classpath:mapper/*.xml
server:
port: 8080
UserMapper.xml
配置一个mapper接口
UserMapper.java
package com.mybatislearn.mockingjay.mapper;
import com.mybatislearn.mockingjay.entity.User;
public interface UserMapper {
User loadUserByUsername(String username);
}
同时项目内也是MVC结构的:
service
UserService.java
package com.mybatislearn.mockingjay.service;
import com.mybatislearn.mockingjay.entity.User;
public interface UserService {
User queryUserByname(String name);
}
UserServiceImpl.java
package com.mybatislearn.mockingjay.service;
import com.mybatislearn.mockingjay.entity.User;
import com.mybatislearn.mockingjay.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public User queryUserByname(String name) {
User user = userMapper.loadUserByUsername(name);
return user;
}
}
Controller
package com.mybatislearn.mockingjay.controller;
import com.mybatislearn.mockingjay.entity.User;
import com.mybatislearn.mockingjay.mapper.UserMapper;
import com.mybatislearn.mockingjay.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getuser")
public User GetUser(String name) {
System.out.println(name);
User user = userService.queryUserByname(name);
return user;
}
}
最后是一个User实体
User.java
package com.mybatislearn.mockingjay.entity;
import lombok.Data;
import lombok.ToString;
import java.time.LocalDateTime;
@Data
@ToString
public class User {
private Long id;
private String avatar;
private LocalDateTime createTime;
private String email;
private String nickname;
private String password;
private Integer type;
private LocalDateTime updateTime;
private String username;
}
最后是pom文件,这里只取maven依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework
spring-web
5.3.8
在配置完成后出现了springboot启动失败的问题:
打印的日志
APPLICATION FAILED TO START
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.createHandlerMethod(AbstractHandlerMethodMapping.java:341)
The following method did not exist:
'void org.springframework.web.method.HandlerMethod.(java.lang.String, org.springframework.beans.factory.BeanFactory, org.springframework.context.MessageSource, java.lang.reflect.Method)'
The method's class, org.springframework.web.method.HandlerMethod, is available from the following locations:
```
jar:file:/C:/Users/xzh11/.m2/repository/org/springframework/spring-web/5.3.8/spring-web-5.3.8.jar!/org/springframework/web/method/HandlerMethod.class
```
The class hierarchy was loaded from the following locations:
```
org.springframework.web.method.HandlerMethod: file:/C:/Users/xzh11/.m2/repository/org/springframework/spring-web/5.3.8/spring-web-5.3.8.jar
```
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.method.HandlerMethod
Process finished with exit code 0
在查阅资料后,我觉得是我使用的spring版本的问题,项目内我使用的是2.5.4snapshot
2.5.4-SNAPSHOT
进入官网后,看到springboot最新的稳定版本为2.5.3
替换springboot版本为2.5.3
2.5.3
成功启动springboot!
2、mybais报错 Invalid bound statement
在启动springboot后,对Controller进行测试,结果出现mybatis报错Invalid bound statement
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.mybatislearn.mockingjay.mapper.UserMapper.loadUserByUsername
at org.apache.ibatis.binding.MapperMethod$SqlCommand.(MapperMethod.java:235) ~[mybatis-3.5.6.jar:3.5.6]
at org.apache.ibatis.binding.MapperMethod.(MapperMethod.java:53) ~[mybatis-3.5.6.jar:3.5.6]
at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:115) ~[mybatis-3.5.6.jar:3.5.6]
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:102) ~[mybatis-3.5.6.jar:3.5.6]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) ~[mybatis-3.5.6.jar:3.5.6]
at com.sun.proxy.$Proxy57.loadUserByUsername(Unknown Source) ~[na:na]
at com.mybatislearn.mockingjay.service.UserServiceImpl.queryUserByname(UserServiceImpl.java:15) ~[classes/:na]
at com.mybatislearn.mockingjay.controller.UserController.GetUser(UserController.java:21) ~[classes/:na]
......
在折腾了一上午之后,发现还是配置的错误:
在UserMapper文件中的namespace没有写对!!!!!
3、总结
这次的错误排查用了我将近一天的时间,对于新手来说,太折磨了
于是,写一个文档进行记录,防止下次再遇到同样的问题。
springboot启动失败的时候,若日志显示找不到什么文件,估计是配置的问题。
配置项目的时候最好再官网上查查稳定的版本。
在配置mybatis的时候,容易犯的错误:
1、要注意属性文件中,实体类地址和mapper地址是否正确
type-aliases-package: com.mybatislearn.mockingjay.entity
mapper-locations: classpath:mapper/*.xml
2、配置Mapper.xml的时候,注意
注意
映射语句中的 id 是否正确对应映射接口的方法名
package com.mybatislearn.mockingjay.mapper;
import com.mybatislearn.mockingjay.entity.User;
public interface UserMapper {
User loadUserByUsername(String username);
}