项目场景:
为了避免使用jdbc的繁杂,使用mybatis来进行数据库的连接操作,结果真是*了狗了,连接了很长时间才连接上
问题描述:
下面记录一下我的debug过程
1.首先遇到的问题是 communication link fail
搜了一下 网上说是因为mysql8.0版本太高,再导入依赖时应该用更高的版本
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
但在改了之后发现并没有什么用(后来证明确实如此)
反而 Driver的地址变为了 com.mysql.cj.jdbc.Driver
2.之后遇到了 “1字节的UTF-8序列的字节1无效”得问题
继续搜 网上说要把配置文件中的UTF-8 改为 UTF8
改完后果然没问题了
3.编码是没问题了 结果又显示数据库连接不上
我真是*了狗了 气得我一口老血喷出来
于是我又想到是不是时区的问题 因为mysql8.0在连接时需要选择时区为GMT
结果发现不是
我一直用的是下面的配置
jdbc:mysql://localhost:3306/数据库?useSSL=true&useUnicode=true&characterEncoding=utf8
后来我换为网上给的一个配置
jdbc:mysql://localhost:3306/数据库?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
我也不知道为什么 突然就行了 ???????????????
但是!会有warning
Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+,
这个warning在连接jdbc时我就遇到过
解决方案
1.在数据库连接的url中添加useSSL=false;
2.url中添加useSSL=true,并且提供服务器的验证证书。
后来想到 网上给的useSSL好像是true! 原来问题的根源在这里!!!吐了
完整配置:
这里给出我的完整配置 哎 真麻烦呀
1.导入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
2.编写MyBatis核心配置文件
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/sql_wangpan?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
3.编写MyBatis工具类
package com.run.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
//sqlSessionFactory----> sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//获取Mybatis获取sqlSession对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取SqlSession连接
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
4.创建实体类
package com.run.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class commonuser {
private int ID;
private String username;
private String password;
private String phonenumber;
private String email;
}
5.编写Mapper接口类
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.run.dao.commonuserDao">
<select id="getCommonUserList" resultType="com.run.pojo.commonuser">
select * from commonuser
</select>
</mapper>
6.编写Mapper.xml配置文件
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.run.dao.commonuserDao">
<select id="getCommonUserList" resultType="com.run.pojo.commonuser">
select * from commonuser
</select>
</mapper>
7.编写测试类
package com.run.dao;
import com.run.pojo.commonuser;
import com.run.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class commonuserDaoTest {
@Test
public void test(){
//第一步:获得sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//方式一:执行SQL
commonuserDao commonuserDao = sqlSession.getMapper(commonuserDao.class);
List<commonuser> commonUserList = commonuserDao.getCommonUserList();
for (commonuser commonuser : commonUserList) {
System.out.println(commonuser);
}
//关闭SqlSession
sqlSession.close();
}
}
可能出现问题说明:Maven静态资源过滤问题
<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>