idea中springboot项目启动报错has no explicit mapping for /error
作为一个爱学习青年,最近在学习java web的时候跟着快速入门教程学习写spring boot web接口项目的时候遇到一个报错,如果访问普通的路径没有任何问题,但是如果访问带数据库查询的@RequestMapping路径则会报如下错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.Thu Aug 26 21:19:19 CST 2021There was an unexpected error (type=Internal Server Error, status=500).
然后查看idea的控制台输出,发现错误内容如下:
2021-08-26 21:19:19.260 ERROR 52391 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ranjuan.mapper.UserMapper.queryUserList] with root cause
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'amin'@'10.0.2.2' (using password: YES)
解决办法:
1、查看application.properties或application.yml配置文件否正确
mybatis.mapper-locations这个配置项目注意有个“s”,我当时就是少打了一个s折腾了半天。 数据库配置里面spring.datasource.username与spring.datasource.name是不一样的东西,应该用前面的。

为了方便我把yml和properties的配置写一起了,可以直接复制我这边,不熟悉的话手打容易出错还不好发现问题
#datasource: #driver-class-name: com.mysql.cj.jdbc.Driver #url: jdbc:mysql://192.168.10.195:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC #username: root1 #password: root1 #mybatis: #下面的话用来指定xml文件的存放地。 #mapper-locations: classpath:mapper/*.xml #将mybatis 与springboot关联起来 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.10.195:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC #spring.datasource.name=root1 #错误示范,如果这里配置启用,连接数据库的时候会使用你电脑的用户名作为数据库连接的用户名去验证而不是root1,会提示Access denied for user spring.datasource.username=root1 spring.datasource.password=root1 #pojo 别名扫描包 mybatis.type-aliases-package=com.ranjuan.domain #加载mybatis映射文件 #mybatis.mapper-location=classpath:mapper/*.xml #错误示范 mybatis.mapper-locations=classpath:mapper/*.xml
2、controller包下的controller类里面注解用@RestController 这样就可以不用写@ResponseBody

3、注意各个包、类文件的层级关系。

4、注意网上有些教程里面使用的框架的版本问题可能与你现在使用的不一样。
比如我这次遇到的application.properties配置的数据库项,有区别


另外需要注意你在写pom.xml文件是数据库的依赖不要使用idea自动填充的版本号。

另外也有网友反馈把一些xml文件内看起来是空行的(看起来像空行实际里面可能有空格之类的)全部删除后就好了,后面我测试了在xml中留带空格的空行但是也是可以正常运行的,不知道是不是我版本比较高的原因自动纠错了。
我的测试demo中的关键代码
controller下的MybatiesController
package com.ranjuan.controller;
import com.ranjuan.domain.User;
//import com.ranjuan.service.UserService;
import com.ranjuan.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
//新建的用来测试的
@RestController//加个注解
public class MybatisController {
@Autowired
private UserMapper userMapper;
//private UserService userService;
@RequestMapping("/ab")
public String hello() {
return "sssasd";
}
@RequestMapping("/abc")
public List<User> queryUserList77() {
System.out.println("-->zhunbeijinru");
List<User> users= userMapper.queryUserList();
System.out.println("-->--->--->"+ users.toString());
return users;
//return userMapper.queryUserList();
//System.out.println("-->"+ userService.findAll().toString());
//System.out.println("-->"+ userService.queryUserList().toString());
// return userService.queryUserList();
}
}
domain下的User
package com.ranjuan.domain;
public class User {
private Integer id;
private String username;
private String password;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
", name='" + name + ''' +
'}';
}
public User(Integer id, String username, String password, String name) {
this.id = id;
this.username = username;
this.password = password;
this.name = name;
}
}
mapper下的UserMapper
package com.ranjuan.mapper;
import com.ranjuan.domain.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
public List<User> queryUserList();
}
service下的UserService,后来没有按规范写,所以这个代码就没有使用到。
项目的启动类SpringbootMybatisApplication
package com.ranjuan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
resource--mapper下的UserMapper
<?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.ranjuan.mapper.UserMapper">
<!--
sql的id:mapper接口的对应方法名
resultType和parameterType都和方法的参数和返回类型对应
parameterType : 传入的参数类型(原本应该用的是全限定名,这里用的是别名) long:大Long _long:小long (具体的对应请参见文档)
resultType : 返回的结果类型(每一条数据返回的对象类型) 自己的对象一定是全限定类名(这里写的是别名)
注意:xml配置方式和接口+注解方式两种方式不能同时对同一sql操作使用
-->
<!--查询一个-->
<!--<select id="findOne" parameterType="long" resultType="cn.wang._02mapper.domain.Product">
select * from product where id=#{id}
</select>-->
<!--查询所有 resultType返回类型要对应起来 因为application.propertis文件里mybatis.type-aliases-package=com.ranjuan.domain,
这里直接写User,相当于com.ranjuan.domain.User-->
<select id="queryUserList" resultType="User">
select id,username,password,name from user
</select>
</mapper>
pom.xml文件(application.properties文件上面已经贴了这里不再贴)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ranjuan</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
数据库表内容(user表,id、username、password、name字段即可)


基于互联网精神,在注明出处的前提下本站文章可自由转载!
本文链接:https://ranjuan.cn/springboot-web-mapping-error/
微信赞赏
支付宝赞赏
发表评论