Mybatis 常见错误

常见错误

数据库密码错误

错误信息:

Caused by: java.sql.SQLException: 
Access denied for user 'root'@'localhost' (using password: YES)

解决方案:

application.properties 修改password

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db01
spring.datasource.username=root
spring.datasource.password=root123

找不到数据库

错误信息:

Caused by: java.sql.SQLSyntaxErrorException: Unknown database 'db011'

解决方案:

application.properties 修改url

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db011
spring.datasource.username=root
spring.datasource.password=root

表不存在

错误信息:

Caused by: java.sql.SQLSyntaxErrorException: Table 'db01.tuser' doesn't exist

解决方案:

com/itheima/boot06/mapper/UserMapper.java 修改@Select的value值

@Mapper
public interface UserMapper {
    @Select("select * from tuser")
    List<User> findAll();
}

依赖注入失败

错误信息:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.itheima.boot06.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

方案一:

com/itheima/boot06/BootProject06MybatisApplication.java 修改@MapperScan设置

@SpringBootApplication
@MapperScan(basePackages = "com.itheima.boot06.mapper")
public class BootProject06MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootProject06MybatisApplication.class, args);
    }

}

方案二:

com/itheima/boot06/mapper/UserMapper.java 添加@Mapper注解

@Mapper
public interface UserMapper {
    @Select("select * from user")
    List<User> findAll();
}

yml 常见错误

异常类型 典型原因
BindingException 接口方法与 SQL 语句绑定失败
BuilderException namespace 写错了
ExecutorException 结果映射缺失
SQLSyntaxErrorException SQL 语法错误
posted @ 2025-07-08 19:41  Breezy_space  阅读(17)  评论(0)    收藏  举报