SpringBoot+Mybatis web项目常见bug

平时用eclipse比较多,自学springboot的时候转用idea,所以有些操作不是很熟悉,在这里记录一下遇到的一些bug及解决方案。

IDEA的一些便捷操作:
(1)Import某个类可用:alt+enter
(2)自动生成测试类,用junit测试,idea对着要进行测试的类/方法光标覆盖 ctrl+shift+t 选择create test class,勾选要测试的方法

一、【Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.】

由于版本的问题,mysql5用的驱动url是com.mysql.jdbc.Driver,mysql6以后用的是com.mysql.cj.jdbc.Driver。版本不匹配便会报驱动类已过时的错误。

说明application.yml配置中应改为:
【driver-class-name: com.mysql.cj.jdbc.Driver】

二、【org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):】

首先,一般情况下要检查mapper文件的地址是否对应。

另外!!!IDEA对xml文件处理的方式不同.** mapper.xml文件需要放置在resource这个文件夹下.**
而eclipse只要mapper接口文件与mapper.xml放置在同一平级目录就行。

三、JDBC连接Mysql6 com.mysql.cj.jdbc.Driver, 需要指定时区serverTimezone,否则会报如下错误,

【 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:

Cannot create PoolableConnectionFactory (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.)WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.】

一定要加上?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false

==>【url: jdbc:mysql://localhost:3306/smtest?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false

四、Mybais的XXXmapper文件头写错,导致无法读取mapper文件标签内容
标准的mapper文件写法:

<?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.example.demo.mapper.UserMapper">
<resultMap id="userMap" type="com.example.demo.entity.User">
    <id property="id" column="id"/>
    <result property="username" column="username" />
    <result property="password" column="password" />
</resultMap>

<select id="findById" resultMap="userMap">
select * from user where id=#{id}
</select>
</mapper>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我第一次在这里写文章,就遇老谢给的一个好问题,该与不该-喜欢与不喜欢。 每每想到这个问题,我就会想起我的母亲,她每...
    Lance_40b1阅读 228评论 0 4
  • A1: 我在看到这个模型之前也制定过很多目标也做过很多计划,我花了很长时间来制定计划,但是最后都以无法善...
    睿恩Rain阅读 217评论 0 0
  • 哄完孩子们睡觉了, 消停了, 我独自坐到桌子前, 打开台灯,静静的坐一会。 两天停更了,心里有些放不下, 不知道说...
    爱渔爱语阅读 897评论 10 5
  • 人这一辈子,难免踩到臭狗屎。没办法,谁让你走路不看道儿呢。只能自嘲一番,踩吧踩吧的,让它自己脱落。再不济,鞋子扔了...
    jolie_宋阅读 173评论 0 0