情景以下,這兩天在作一個分佈式的項目,使用了Alibaba的dubbo做爲通訊工具,zookeeper做爲register,因爲dubbo是基於socket協議的,因此在進行pojo傳輸的時候報了異常,由於pojo沒有實現序列化接口,就沒法進行基於二進制的序列化傳輸。報錯以下。java
可是很麻煩的一件事是若是逆向工程生成的pojo所有本身實現序列化會很麻煩,因此看了一下mybatis的插件,發現有一個能夠自動給全部pojo實現序列化接口(example除外)。
具體代碼以下:mysql
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <context id="testTables" targetRuntime="MyBatis3"> 8 9 <!-- 配置pojo的序列化 --> 10 <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> 11 12 <commentGenerator> 13 <!-- 是否去除自動生成的註釋 true:是 : false:否 --> 14 <property name="suppressAllComments" value="true" /> 15 </commentGenerator> 16 <!--數據庫鏈接的信息:驅動類、鏈接地址、用戶名、密碼 --> 17 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 18 connectionURL="jdbc:mysql://localhost:3306/xxxxxxx" userId=" " 19 password=" "> 20 </jdbcConnection> 21 <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer,爲 true時把JDBC DECIMAL 22 和 NUMERIC 類型解析爲java.math.BigDecimal --> 23 <javaTypeResolver> 24 <property name="forceBigDecimals" value="false" /> 25 </javaTypeResolver> 26 27 <!-- targetProject:生成PO類的位置 --> 28 <javaModelGenerator 29 targetPackage="cn.xxxxxxx.pojo" targetProject=".\src"> 30 <!-- enableSubPackages:是否讓schema做爲包的後綴 --> 31 <property name="enableSubPackages" value="false" /> 32 <!-- 從數據庫返回的值被清理先後的空格 --> 33 <property name="trimStrings" value="true" /> 34 </javaModelGenerator> 35 <!-- targetProject:mapper映射文件生成的位置 --> 36 <sqlMapGenerator targetPackage="cn.xxxxxxx.mapper" 37 targetProject=".\src"> 38 <!-- enableSubPackages:是否讓schema做爲包的後綴 --> 39 <property name="enableSubPackages" value="false" /> 40 </sqlMapGenerator> 41 <!-- targetPackage:mapper接口生成的位置 --> 42 <javaClientGenerator type="XMLMAPPER" 43 targetPackage="cn.xxxxxxx.mapper" targetProject=".\src"> 44 <!-- enableSubPackages:是否讓schema做爲包的後綴 --> 45 <property name="enableSubPackages" value="false" /> 46 </javaClientGenerator> 47 <!-- 指定數據庫表 --> 48 <table schema="" tableName=" "></table> 49 <table schema="" tableName=" "></table> 50 <table schema="" tableName=" "></table> 51 <table schema="" tableName=" "></table> 52 <table schema="" tableName=" "></table> 53 <table schema="" tableName=" "></table> 54 <table schema="" tableName=" "></table> 55 <table schema="" tableName=" "></table> 56 <table schema="" tableName=" "></table> 57 <table schema="" tableName=" "></table> 58 <table schema="" tableName=" "></table> 59 60 </context> 61 </generatorConfiguration>
java代碼以下:sql
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; public class GeneratorSqlmap { public void generator() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; // 指定 逆向工程配置文件 File configFile = new File("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
須要的jar以下:點擊下載
數據庫