问题说明

学习过程中遇到很多问题,记录一下。

Maven静态资源过滤问题

<!--在build中配置resource,来防止我们资源导出失败的问题-->
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

Junit测试时,可能会提示

org.apache.ibatis.binding.BindingException: Type interface com.melodyhub.dao.UserDao is not known to the MapperRegistry.

MapperRegistry是什么?

每一个Mapper.xml都需要在MyBatis核心配置文件中注册!!!

<!--每一个Mapper.xml都需要在MyBatis核心配置文件中注册!!!-->
<mappers>
	<!--<mapper class="com.melodyhub.dao.UserMapper"/>-->
	<mapper resource="com/melodyhub/dao/UserMapper.xml"/>
</mappers>

MySQL时区

由于MySQL底层是使用的时区是CST,跟中国时间相差挣好8个小时。

解决办法是在mybatis-config.xml中``jdbcurl`末尾加上,二选一即可:

&amp;serverTimezone=GMT%2B8
&amp;useTimezone=true&amp;serverTimezone=UTC

参考:https://blog.csdn.net/ziningyihao/article/details/90644295

MySQL驱动/版本问题

所有的代码都没有问题,但是就是连接不上数据库,经过排查,发现我的MySQL版本是8.0,驱动包是5.1.47,差距太大,不兼容。

将MySQL降级到5.7.29,问题解决。

解决Mybatis报错Could not find resource mybatis-config.xml

下午移植远程仓库项目的时候,控制台报错:

java.io.IOException: Could not find resource mybatis-config.xml

经过排查,发现是我没有一直夫工程的pom.xml文件。

Maven clean清除缓存,然后build,即可。

-解决Mybatis报错Could not find resource mybatis-config.xml

MyBatis问题解决Error building SqlSession.

解决🐤

改为全类名com.melodyhub.pojo.User

MyBatis问题解决Error building SqlSession.

连接JDBC报错:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

问题:
Spring整合Mybatis时候,出现数据库连接异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

分析:

找了好半天的原因,最后发现是相关jdbc驱动包版本太低

解决方法:

下载最新版的mysql-connector-java即可。
虽然可以连接成功,但是控制台还是提示:

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.

修改一下mybatis-config.xml

<!--<property name="driver" value="com.mysql.jdbc.Driver"/>-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>

mysql 连接池连接超时的问题(The last packet successfully received from the server was 2,431 milliseconds ago. The last packet sent successfully to the server was 2,417 milliseconds ago.)

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 2,431 milliseconds ago.  The last packet sent successfully to the server was 2,417 milliseconds ago.
### The error may exist in com/melodyhub/mapper/UserMapper.xml
### The error may involve com.melodyhub.mapper.UserMapper.selectUser
### The error occurred while executing a query
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 2,431 milliseconds ago.  The last packet sent successfully to the server was 2,417 milliseconds ago.

	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
	

解决:
mysql超时设置的问题
如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接。

connection url中加参数: autoReconnect=true

jdbc.url=jdbc:mysql://localhost:3306/database?autoReconnect=true&amp;autoReconnectForPools=true