MySQL8.0用mybatis自動生成文件及報錯解決方案

先分享文件,裏面有generator.xml文件和jar包。

參考這篇博客用mybatis自動生成文件時,報了以下錯誤:

反覆檢查數據庫連接語句沒有找到錯誤後,百度錯誤造成的原因及解決辦法,參考這篇博客

第一步:使用最新的MySQL驅動jar包。
第二步:把驅動的類名改爲:
static String driver="com.mysql.cj.jdbc.Driver";
第三步:在訪問mysql的url後加入時區設置:
static String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC";(UTC表示標準時區)

修改後再次報錯:

解決方案參考這篇博客,原因是在 xml 中 &符號是作爲實體字符形式存在的,將mysql的URL改爲:

jdbc:mysql://localhost:3306/test1?characterEncoding=utf8&serverTimezone=UTC

最終修改後的generator.xml:

<?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="F:\generator\mysql-connector-java-8.0.19.jar" /> 
	<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 數據庫鏈接URL、用戶名、密碼 -->
		 <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test1?characterEncoding=utf8&amp;serverTimezone=UTC" userId="root" password="123456"> 
		<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="com.test.model" targetProject="F:\generator\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成的映射文件包名和位置 -->
		<sqlMapGenerator targetPackage="com.test.mapping" targetProject="F:\generator\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="F:\generator\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->
		<table tableName="tb_test" domainObjectName="tb_test" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> -->
	</context>
</generatorConfiguration>

終於執行成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章