情景如下,這兩天在做一個(gè)分布式的項(xiàng)目,使用了Alibaba的dubbo作為通信工具,zookeeper作為register,由于dubbo是基于socket協(xié)議的,所以在進(jìn)行pojo傳輸?shù)臅r(shí)候報(bào)了異常,因?yàn)閜ojo沒有實(shí)現(xiàn)序列化接口,就無(wú)法進(jìn)行基于二進(jìn)制的序列化傳輸。報(bào)錯(cuò)如下。
?
但是很麻煩的一件事是如果逆向工程生成的pojo全部自己實(shí)現(xiàn)序列化會(huì)很麻煩,所以看了一下mybatis的插件,發(fā)現(xiàn)有一個(gè)可以自動(dòng)給所有pojo實(shí)現(xiàn)序列化接口(example除外)。
具體代碼如下:
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 <!-- 是否去除自動(dòng)生成的注釋
true:是 :
false:否 -->
14 <property name="suppressAllComments" value="true" />
15 </commentGenerator>
16 <!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類、連接地址、用戶名、密碼 -->
17 <jdbcConnection driverClass="com.mysql.jdbc.Driver"
18 connectionURL="jdbc:mysql://localhost:3306/xxxxxxx" userId=" "
19 password=" ">
20 </jdbcConnection>
21 <!--
默認(rèn)false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時(shí)把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 <!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 -->
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 <!-- 指定數(shù)據(jù)庫(kù)表 -->
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代碼如下:
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如下:點(diǎn)擊下載
轉(zhuǎn)載于:https://www.cnblogs.com/TimeIsChoice/p/8807548.html
總結(jié)
以上是生活随笔為你收集整理的Mybatis逆向工程的pojo实现序列化接口代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。