spring集成mybatis简单教程

首先说下实现了什么效果,就是不用每次查询前手动创建

复制代码
sessionFactory和添加datasource文件了。

整个工程结构是这样的

这次我也把代码放在了gitee上,方便大家更全貌的看到所有的实现细节。代码链接如下:

Java: 一些Java代码 (gitee.com)

最后就是可以成功查询到数据库的数据啦

我把遇到的一些问题简单记录下, 避免后人踩坑。

1.Failed to obtain JDBC Connection; nested exception is java.sql.SQLException:

这个和驱动版本有关系,我用的是com.mysql.cj.jdbc.Driver,所以当时用5.x版本就报错了

所以我选了

  • com.mysql.jdbc.Driver和mysql-connector-java 5.x一起用。
  • com.mysql.cj.jdbc.Driver和mysql-connector-java 6.x 及以上一起用。
  1. 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.

这个要在数据源配置文件里,加上时区serverTimezone=Asia/Shanghai

复制代码
druid.url=jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Shanghai

参考这个文档的解释The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone-CSDN博客

相关推荐
短剑重铸之日1 小时前
《ShardingSphere解读》07 读写分离:如何集成分库分表+数据库主从架构?
java·数据库·后端·架构·shardingsphere·分库分表
知我Deja_Vu1 小时前
【避坑指南】ConcurrentHashMap 并发计数优化实战
java·开发语言·python
daidaidaiyu2 小时前
Spring IOC 源码学习 事务相关的 BeanDefinition 解析过程 (XML)
java·spring
鬼蛟3 小时前
Spring————事务
android·java·spring
西门吹-禅4 小时前
【sap fiori cds up error】
java·服务器·sap cap cds
敲代码的嘎仔4 小时前
Java后端面试——SSM框架面试题
java·面试·职场和发展·mybatis·ssm·springboot·八股
NGC_66114 小时前
Spring与SpringBoot
spring
大傻^4 小时前
Spring AI Alibaba RAG实战:基于向量存储的检索增强生成
java·人工智能·spring
大傻^4 小时前
Spring AI Alibaba 快速入门:基于通义千问的AI应用开发环境搭建
java·人工智能·后端·spring·springai·springaialibaba
伯恩bourne4 小时前
Google Guava:Java 核心工具库的卓越之选
java·开发语言·guava