MyBatis与JDBC连接数据库所使用的url之间的差异

在Windows7 系统上安装了MySQL 8.0,然后创建Maven工程,配置pom.xml文件,添加了如下依赖:

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>

(1)在mybatis-config.xml中,配置数据库连接环境:

<environments default="development">
<environment id="development">
<transactionManager type="JDBC" >
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="password@password" />
</dataSource>
</environment>
</environments>

运行测试函数进行连接测试,出现如下错误提示:

org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 102; 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾。

上网搜索解决方案,才知道 mybatis 的配置文件中,这里 url 中的 '&' 符号应当写成 '&amp;' ,修改后的 url 如下:

<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC" />

这样就可以正常连接了。

(2)鉴于1中的错误,尝试使用JDBC连接MySQL时,如下进行连接:

@Test
    public void testJDBC()
    {
        Connection conn = null;
        try
        {
            String userName = "root";
            String passWord = "password@password";
            String jdbcUrl = "jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC";
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(jdbcUrl, userName, passWord);
            String sql = "select id,countryname,countrycode from country";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next())
            {
                int id = rs.getInt("id");
                String countryname = rs.getString("countryname");
                String countrycode = rs.getString("countrycode");
                System.out.println(id + "\t" + countryname + "\t" + countrycode);
            }
            pstmt.close();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            System.out.println("Game Over!");
        }
    }

运行测试函数,结果出现如下错误:

java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC'.

尝试修改 url ,将 '&amp;' 改成 '&' , 结果就运行正常了,说明 JDBC 方式连接 MySQL 不需要对 '&' 进行转义。

特此记之。

posted on 2018-07-25 16:54  HorseShoe2016  阅读(11836)  评论(0编辑  收藏  举报