1. 程式人生 > >mysql 錯誤解決

mysql 錯誤解決

1.錯誤提示

使用mybatis程式碼生成工具時,提示時區亂碼錯誤,如下所示

C:\springinit\demo\src\main\java>java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -over
write
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. Th
e driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one t
ime 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.
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
        at org.mybatis.generator.internal.JDBCConnectionFactory.getConnection(JDBCConnectionFactory.java:84)
        at org.mybatis.generator.config.Context.getConnection(Context.java:542)
        at org.mybatis.generator.config.Context.introspectTables(Context.java:446)
        at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:257)
        at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:189)
        at org.mybatis.generator.api.ShellRunner.main(ShellRunner.java:117)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '???ú±ê×??±
??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (v
ia the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time z
one support.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
        at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
        at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2234)
        at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2258)
        at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
        ... 9 more

2.解決辦法

配置jdbcConnection,新增servertimezone配置

如下所示

       <!--資料庫連結URL,使用者名稱、密碼 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mdblog" userId="root" password="root" >
            <property name="serverTimezone" value="UTC"/>
        </jdbcConnection>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 資料庫驅動-->
    <classPathEntry  location="mysql-connector-java-8.0.13.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--資料庫連結URL,使用者名稱、密碼 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mdblog" userId="root" password="root" >
            <property name="serverTimezone" value="UTC"/>
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="cn.goingtodo.domain" targetProject=".">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成對映檔案的包名和位置-->
        <sqlMapGenerator targetPackage="cn.goingtodo.mapping" targetProject=".">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.goingtodo.IDao" targetProject=".">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名-->
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

3.效果

C:\springinit\demo\src\main\java>java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
Table Configuration user matched more than one table (mdblog..user,mysql..user)
Cannot obtain primary key information from the database, generated objects may be incomplete
Cannot obtain primary key information from the database, generated objects may be incomplete
Existing file C:\springinit\demo\src\main\java\.\cn\goingtodo\domain\User.java was overwritten
Existing file C:\springinit\demo\src\main\java\.\cn\goingtodo\IDao\UserMapper.java was overwritten

MyBatis Generator finished successfully, there were warnings.

切記資料庫配置表user要指明,資料庫名.表明, test.user, mdblog.user是不一樣的,不然會混淆。

4.再來一遍

C:\springinit\demo\src\main\java>java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
Table Configuration mdblog.user matched more than one table (mdblog..user,mysql..user)
Cannot obtain primary key information from the database, generated objects may be incomplete
Cannot obtain primary key information from the database, generated objects may be incomplete
Existing file C:\springinit\demo\src\main\java\.\cn\goingtodo\domain\User.java was overwritten
Existing file C:\springinit\demo\src\main\java\.\cn\goingtodo\IDao\UserMapper.java was overwritten
Existing file C:\springinit\demo\src\main\java\.\cn\goingtodo\domain\User.java was overwritten
Existing file C:\springinit\demo\src\main\java\.\cn\goingtodo\domain\UserWithBLOBs.java was overwritten
Existing file C:\springinit\demo\src\main\java\.\cn\goingtodo\IDao\UserMapper.java was overwritten

MyBatis Generator finished successfully, there were warnings.

5.附錄

mysql-connector-java-8.0.13.jar
mybatis-generator-core-1.3.7.jar

自動生成不易啊,摸索了一下。。。。。。