1. 程式人生 > >mybatis-generator生成通用mapper中文亂碼解決

mybatis-generator生成通用mapper中文亂碼解決

<?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="C:\Users\Administrator\Desktop\tools\generator\mysql-connector-java-5.1.34.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
	 
		<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
			<property name="mappers" value="tk.mybatis.mapper.common.Mapper" />
			<!-- caseSensitive預設false,當資料庫表名區分大小寫時,可以將該屬性設定為true -->
			<!-- <property name="caseSensitive" value="true" /> -->
		</plugin>

        <!-- 阻止生成自動註釋 -->
        <commentGenerator>
        	
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        
<property name="javaFileEncoding" value="UTF-8"/>

 
         <!-- 資料庫連線資訊 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/webchat" userId="root" password="root">
        </jdbcConnection>
        
        <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
         
        <javaModelGenerator targetPackage="webchat.model" targetProject="D:\workspace\webchat\src\main\java">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
        </javaModelGenerator>
         
        <sqlMapGenerator targetPackage="webchat.mapping" targetProject="D:\workspace\webchat\src\main\java">
                <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
         
        <javaClientGenerator type="XMLMAPPER" targetPackage="webchat.dao" targetProject="D:\workspace\webchat\src\main\java">
                <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
	 
	    
	<table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
        <generatedKey column="id" sqlStatement="Mysql"/>
    </table>
    
	<table tableName="t_group" domainObjectName="Group" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
            <generatedKey column="id" sqlStatement="Mysql"/>
    </table>
    <table tableName="group_user" domainObjectName="groupUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
            <generatedKey column="id" sqlStatement="Mysql"/>
    </table>
    
    <table tableName="group_message" domainObjectName="groupMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
            <generatedKey column="id" sqlStatement="Mysql"/>
    </table>
    
    <table tableName="friend_type" domainObjectName="friendType" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
            <generatedKey column="id" sqlStatement="Mysql"/>
    </table>
    
    <table tableName="friend_message" domainObjectName="friendMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
            <generatedKey column="id" sqlStatement="Mysql"/>
    </table>
    
    <table tableName="friend" domainObjectName="Friend" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
            <generatedKey column="id" sqlStatement="Mysql"/>
    </table>
 

    
    
	</context>
</generatorConfiguration>

首先檢查你的 generator.xml  要有  <property name="javaFileEncoding" value="UTF-8"/>

然後檢查工程是否為utf-8編碼!

最後開啟你的exlipse.ini 檔案,最末尾加上 -Dfile.encoding=UTF-8

重啟eclipse,配置maven生成程式碼即可,發現沒有中文亂碼了。