SpringBoot框架中使用Mybatis连接数据库是遇到问题以及解决

问题1:运用注解自动加载Dao层对象UserDao总是出错,显示找不到反射对象,

@Autowired
UserDao userDAO;

解决方案:通过在Dao层UserDao文件里面添加@Repository注解可以解决上述问题。

import org.springframework.stereotype.Repository;

@Repository

问题2:安装MySQL8.0之后,并没有设置密码,用navicat连接MySQL发现连接不上,原来8.0之后会初始化密码,存放在MySQL安装目录下.err文件,找到初始化密码,成功链接数据库。

问题3:写好testDateBase文件,准备测试连接时,出现连接错误,连接MySQL8.0遇到问题

authentication plugin 'caching_sha2_password

解决方案:在cmd命令行模式下,进入MySQL,并运行

alter user root@localhost identified with mysql_native_password by 'password';

问题4:解决问题3后,又出现新的问题,经查阅,发现是mysql-connector-java版本问题,版本过低。到pow.xml文件中修改版本,将其改到5.1.44,发现可以解决问题。

transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: Unknown system variable 'query_cache_size'

至此,完成与数据库的连接,并成功更新数据库内容。结果如下:

猜你喜欢

转载自blog.csdn.net/ailuyi/article/details/82288835