头图

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

<?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="com.mybatislearn.mockingjay.mapper.UserMapper">
    <resultMap id="UserMap" type="com.mybatislearn.mockingjay.entity.User" >
        <id column="id" property="id" />
        <result column="avatar" property="avatar" />
        <result column="createTime" property="createTime" />
        <result column="email" property="email"  />
        <result column="nickname" property="nickname"  />
        <result column="password" property="password"  />
        <result column="type" property="type"  />
        <result column="updateTime" property="updateTime"  />
        <result column="username" property="username"  />
    </resultMap>

    <!--  根据用户名查询用户-->
    <select id="loadUserByUsername" resultMap="UserMap">
        select * from t_user where username=#{username}
    </select>
</mapper>

配置一个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依赖

<dependencies>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.8</version>
    </dependency>
</dependencies>

在配置完成后出现了springboot启动失败的问题:

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.<init>(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

<version>2.5.4-SNAPSHOT</version>

进入官网后,看到springboot最新的稳定版本为2.5.3

springboot官网

springboot官网

替换springboot版本为2.5.3

<version>2.5.3</version>

成功启动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.<init>(MapperMethod.java:235) ~[mybatis-3.5.6.jar:3.5.6]
    at org.apache.ibatis.binding.MapperMethod.<init>(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没有写对!!!!!

<?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="com.mybatislearn.mockingjay.mapper.UserMapper">

    <resultMap id="UserMap" type="com.mybatislearn.mockingjay.entity.User" >
        <id column="id" property="id" />
        <result column="avatar" property="avatar" />
        <result column="createTime" property="createTime" />
        <result column="email" property="email"  />
        <result column="nickname" property="nickname"  />
        <result column="password" property="password"  />
        <result column="type" property="type"  />
        <result column="updateTime" property="updateTime"  />
        <result column="username" property="username"  />
    </resultMap>

    <!--  根据用户名查询用户-->
    <select id="loadUserByUsername" resultMap="UserMap">
        select * from t_user where username=#{username}
    </select>
</mapper>

3、总结

这次的错误排查用了我将近一天的时间,对于新手来说,太折磨了

于是,写一个文档进行记录,防止下次再遇到同样的问题。

springboot启动失败的时候,若日志显示找不到什么文件,估计是配置的问题。

配置项目的时候最好再官网上查查稳定的版本。

在配置mybatis的时候,容易犯的错误:

1、要注意属性文件中,实体类地址和mapper地址是否正确

type-aliases-package: com.mybatislearn.mockingjay.entity
mapper-locations: classpath:mapper/*.xml

2、配置Mapper.xml的时候,注意<mapper>标签下的namespace是否正确

<mapper namespace="com.mybatislearn.mockingjay.mapper.UserMapper">

注意<resultMap>标签中的 type地址是否正确

<resultMap id="UserMap" type="com.mybatislearn.mockingjay.entity.User" >

映射语句中的 id 是否正确对应映射接口的方法名

<select id="loadUserByUsername" resultMap="UserMap">
    select * from t_user where username=#{username}
</select>
package com.mybatislearn.mockingjay.mapper;
import com.mybatislearn.mockingjay.entity.User;

public interface UserMapper {
    User loadUserByUsername(String username);

}

caecarxu
1 声望0 粉丝