java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot找不到Mapper的Bean

使用SpringBoot找不到Mapper的Bean问题及解决

作者:人間_過客

这篇文章主要介绍了使用SpringBoot找不到Mapper的Bean问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

SpringBoot找不到Mapper的Bean

记一次令人抓狂的错误解决经历,先说结果:

数据库版本使用错误,没有配置pom文件中的版本号,默认使用了本地驱动中的最高6版本。

更改为5版本即可发生报错后找了很多的文档也没有符合我这个样子的,于是我就把自己的经历写下来,给和我一样犯了这个错的初学者看。

初期报错

@Service
public class UserService {

    @Autowired
    private DeptMapper deptMapper;//此处报错为找不到mapper类型的Bean
   

检查启动类中已经正确配置路径。

强行启动程序后

//这句话说使用数据库链接应该使用带cj那个,说明使用的驱动版本是6,但是编写的配置文件是5的配置。
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
//这句说的是找不到mapper的目录
Property 'mapperLocations' was not specified.​

如果强行调用service

就会出现如下错误:

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b3ac131]
2021-06-05 16:43:55.847 ERROR 32732 --- [p-nio-80-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
### The error may exist in top/loveeveryone/mapper/DeptMapper.java (best guess)
### The error may involve top.loveeveryone.mapper.DeptMapper.selectByPrimaryKey
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.] with root cause
//说数据库时区有问题

综合检查,发现数据库驱动有问题。

在pom文件中加载Mysql数据库驱动程序时没有添加驱动程序版本,默认使用最高版本导致驱动失败。

将驱动修改至5版本就好了

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文