日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis逆向工程的pojo实现序列化接口代码

發(fā)布時(shí)間:2024/7/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis逆向工程的pojo实现序列化接口代码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

情景如下,這兩天在做一個(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ò),歡迎將生活随笔推薦給好友。