前の記事であるMyBatis Reverse Engineeringの使用は、主にMyBatis-Generator-Core-1.3.2.Jarの使用について説明しています。この記事で紹介したいのは、JARパッケージコードを変更して、カスタムテンプレートの生成を実現することです。
1。ここからMyBatis-Generator-Core-1.3.2.jarプロジェクトのソースコードをダウンロードできます
http://maven.outofmemory.cn/org.mybatis.generator/mybatis-generator-core/1.3.2/
2. Eclipse、file-> Importの下で既存のMavenプロジェクトをインポートします
プロジェクトのソースコードの場所を選択し、[完了]をクリックしてインポートを完了します。
プロジェクトディレクトリ構造はほぼこのようなものです。
3.以下では、マッピングとXML形式を逆転させて生成します。
4.変更を開始し、最初に各ディレクトリを説明します
下部にあるTSEパッケージは、メインクラスである私のカスタマイズされたパッケージであり、生成されたコードが予想される標準を満たしているかどうかをテストします。
このパッケージは外国人によって書かれているため、生成されたコードスタイルは私たちのものとほとんど同じではありません。コード形式を変更する場合は、パイナップルエレファントに関する記事を読むことをお勧めします。ここではコード形式については説明しません。
http://www.blogjava.net/bolo/archive/2015/03/20/423683.html
まず、最初に、逆プロジェクトによって生成されるインターフェイスファイルのマッピングコードを変更します。デフォルトでは、追加、削除、変更、検索があります。更新する変更方法の1つについて話しましょう。
たとえば、生成されたマッピングvoidアップデート(Map <String、Object> DataMap)にこのようなメソッドが必要です。
org.mybatis.generator.codegen.mybatis3.javamapper.elementsパッケージの下のupprimarykeywithwithblobsmethodgeneratorクラスを変更します。
/** Copyright 2009 The Apache Software Foundation ** Apacheライセンスバージョン2.0(「ライセンス」); 「As "BASITのように、*明示または黙示のいずれかのいずれかの種類の保証または条件なし*。*ライセンスに基づく特定の言語ガバナンスの許可と*制限のライセンスを参照してください。 org.mybatis.generator.api.dom.java.fulyqualifiedJavatype; Import org.mybatis.generator.api.dom.java.interface; import org.mybatis.generator.api.dom.java.javavisibility; Import org.mybatis.generator.api.dom.javad.api.dom.javad.javadibility org.mybatis.generator.api.dom.java.parameter;/**** @author jeff butler**/public class upprocebyPrimaryKeyWithOutBlobsMethodGenerator extendSabstractJavamApperTodGenerator {public bydadbyprimarywithoutblobsblobsmetheNerator() void addInterfaceElements(interface interface){set <fully qualifiidedjavatype> importedTypes = new Treeset <fully資格Javatype>(); fully qualifiedjavatype = new fully qualifiidedjavatype(intruspectedtable.getBaseRecordtype(); method = new method(); // method modifier publicMethod.setVisability(javavisability.public); // return valueを設定するカスタムボイドを使用します。ここではカスタム値を使用します。完全に資格のあるJavatype( "void")、コンストラクター方法に戻る型を書き留めるだけです。SetturnType(完全に資格のあるJavatype.getVoidInstance()); "記録")); // $ non-nls-1 $ $ fully qualifiedjavatype maptype = fully qualifiedjavatype.getMymapInstance(); Method.AddParameter(パラメーター); context.getCommentGenerator()。addGeneralMethodComment(Method、Introspectectable); AddMapperAnnotations(InterFaze、Method); if(context.getPlugins()。clientUpdateByPrimaryKeyWithOutBlobsMethodGenerated(Method、Interfaze、IntrospectedTable)){interfaze.addimportedTypes(importedTypes); interfaze.addmethod(method);}} public void returnation(method;コメントに従って変更できます。
次に、マッピングの対応するXMLのコードを変更します。同様に、ここでは更新方法のみを紹介します。読んだ後、自分で他の方法を変更できると思います。
次のように、org.mybatis.generator.codegen.mybatis.mybatis3.xmlmapper.elementsパッケージのorg.mybatis.generator.codegen.mybatis3.xmlmapper.elementsのunderbyprimarykeywithwithblobsementgeneratorクラスを変更します。
/** Copyright 2009 The Apache Software Foundation ** Apacheライセンスバージョン2.0(「ライセンス」); 「As "Basionのように、*明示的または暗示されたいずれかの種類の保証または条件なし**/パッケージorg.mybatis.generator.codegen.mybatis3.xmlmapper.elements; import java.util.iterator; import java.util.util.list; org.mybatis.generator.api.intruspectedColumn; Import org.mybatis.generator.api.dom.outpututitivities; Import org.mybatis.generator.api.dom.xml.Attribute; Import org.mybatis.generator.api.dom.xml.textelement; org.mybatis.generator.api.dom.xml.xmlelement; Import org.mybatis.generator.codegen.mybatis3.mybatis3formattintitivities;/**** @author jeff butler**/public class upublis upproagbyprobblobblobsotgenerator boolean issimple; public upputidbybyPrimaryKeyWithBlobSelementGenerator(boolean issimple){super(); // this.issimple = issimple;}@overridepublic void addelements(xmlelement parentelement){// updateタグ(方法のエクステリアレイヤー) // $ non-nls-1 $ // update of the update tag tag assens.addattribute(new属性( "id"、intruspectedtable.getupdate.getupdate)primarykeystatementid()); //$NON-NLS-1$ ANSWER.ADDATTRIBUTE(New Attribute( "parameterType"、// $ non-nls-1 $ "map")); //タグをContext.getCommentGenerator()。AddComment(Answer); StringBuilder sb = new StringBuilder(); sb.Append( "update"); //$non-nls-1$sb.append(introspectedtable.gettullyqualifiedTableNAmeatruntime();//Tag Content、つまり、テキスト要素Answer.Addelement(new TextElement(sb.tostring())); sb.setLength(0); //タグxmlelement setelement = new XMLelement( "set"); // $ non-nls-1 $ //データベーステーブルですべてのフィールドを取得<introspectedColumn> cols = introspectedtable.getallColumns(); // iteration java.util.iterator <introspectedcolumn> iter = cols.iterator(); iter.next(); // if tag xmlelement ifelement = new xmlement( "if"); // $ non-nls-1 $ //フィールド名String str = mybatis3formattingutitivities.getescapedColumnname(IntrospectedColumn); //タグ追加属性テストの場合、フィールド!= null and field!= '' ifelement.addattribute(new Attribute( "test"、str+'!フィールドsb.append(mybatis3formattingutitivities.getescapedColumnname(introspectedColumn)); sb.Append( "="); //$non-nls-1$ if(iter.hasnext()){sb.append( '、');} //上記のテキスト要素ifelement.addelement(new TextElement(sb.toString())); if(iter.hasnext()){sb.setLength(0); outotutitivities.xmlindent(sb、(sb.setlength(0); 1);} setElement.Addelement(ifelement);} // where element(modified field precondition)xmlelement whereelement = new xmlement( "where"); sb.setlength(0); sb.append(mybatis3formattingutitivities.getescapedColumnname(intruspectedColumn)); sb.Append( "="); //$NON-NLS-1$SB.APPEND(MyBatis3FormattingUtilities.getParameterCluase(intruspectedColumn); whereElement.Addelement(New TextElement(sb.ToString()) Answer.Addelement(WhereElement); if(context.getPlugins()。sqlmapupdatebyprimarykeywithblobselementgenerated(answer、introspectedtable)){parentelement.addelement(nesss);}}}}}この更新方法に従って他の方法を変更できます。
新しい方法を追加する場合は、次の投稿を参照してください
http://m.blog.csdn.net/article/details?id=35985705
次に、変更の結果を確認します
GeneratorConfig.xml // XMLを最初に構成し、SRC/Main/Resources/Directory <?xml version = "1.0" Encoding = "utf-8"?> <!<!<!/ mybatis.org//dtd mybatisジェネレーターの構成1.0 // en " "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <GeneratorConfiguration> <! - 構成ファイルの紹介 - > <! - データ接続ドライバーJARアドレスを指定 - > <classPathErtry場所= "e:/eclipse_workspace/testmybatis/mysql-connector-java-5.1.13-bin.jar"/> <! - データベースの1つのコンテキスト - > <コンテキストID = "infoguardian" infoguardian "infoguardian" infoguardian " uncomment - > <Property name = "Suppressdate" value = "true" /> <! - コメント生成タイムスタンプを生成するかどうか - > < /commentgenerator> <! - jdbc connection-> <jdbcconnection driverclass = "com.mysql.jdbc.driver" Connectionurl = "jdbc:mysql:// localhost:3306/login?charatereCoding = utf-8" userid = "root" password = "root"/> <! - タイプ変換 - > <javatypereSolver> <! - bigdecimalを使用するかどうか、falseは次のタイプを自動的に変換する可能性があります( value = "false"/> </javatyperesolver> <! - エンティティクラスアドレスを生成 - > <javamodelgenerator targetpackage = "pojo"ターゲットパッケージ= "mybatis3"> <! - 現在のパスの下にスキーマの新しいレイヤーを追加するかどうか、EG:ファーズパス - > <プロパティname = "enablesubpackages" value = "true"/> <! - 文字列タイプのフィールドに設定したときにトリムを呼び出すかどうか - > <プロパティ名= "trimstrings" value = "true"/> </javamodelgenerator> <! - mapxmlファイルを生成 - > <sqlmapgeneratorターゲット=現在のパスの下のスキーマの層、例:例:Fase Path cn.ffcs.test.domain "、true:cn.ffcs.test.domain" type = "xmlmapper" targetpackage = "mapper" stargentproject = "mybatis3"> <! - 現在のパスの下にスキーマの新しい層を追加するかどうか、例:Fase Path cn.ffcs.test.domain "、true:cn.ffcs.test.domain"。 </javaclientGenerator> <! - テーブル情報の構成、ここでテーブルは生成されません。対応するテーブル名は一度変更する必要があります - > <table tablename = "login" domainobjectname = "login" enablecountbyexample = "false" enableeletebyexample = "enableselectbyexample =" false </generatorConfiguration>
startup.java//検証のためのメインプログラム
パッケージtse; Import static org.junit.assert.assertequals; import java.io.file; import java.util.arraylist; import java.util.list; import java.util.list; import org.mybatis.generator.api.api.mybatisatorator; import org.mybatis.conerator; org.mybatis.generator.config.xml.configurationParser; Import org.mybatis.generator.internal.defaultshellcallback; public static void main(string [] args)throws exception {list <string> warnings = new Arraylist <string>; file(startup.class.getResource( "/generatorconfig.xml")。touri()); configurationParser cp = new configurationParser(警告); configuration Config = cp.parseConfiguration(configfile); defaultshellcallback shellcallback = new defallycallator(true); mybatisgenerorator mybatisgenerator(config、shellcallback、警告); mybatisgenerator.generate(null); system.out.println(警告);}}} OK、起動up.javaを実行します
対応するファイルは、GeneratorConfig.xmlの構成に従ってターゲットディレクトリで生成されます。
わかりました、私が思っていたのと同じ結果です。
5。上記の変更の後、パッケージングを開始します。
それはMavenプロジェクトなので、Maven3.3.9を使用しています。MavenBulidinEclipseを使用することもできますが、とにかく気に入らない。
以下は私のmavenプロジェクトのpom.xmlファイルコードです
<?xml version = "1.0" encoding = "utf-8"?> <! - 著作権2009-2011 Apacheライセンスに基づくMybatis Teamlicense、バージョン2.0(「ライセンス」);ライセンスの遵守を除いて、このファイルを使用することはできません。ライセンスatttp://www.apache.org/licenses/license-2.0のコピーを取得することができます。ライセンスの下。--> <! - バージョン:$ id:pom.xml 4114 2011-11-27 19:03:32z Simone.tripodi $ - > <Project XMLNS = "http://maven.apache.org/pom/4.0.0" " xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0<//modelversion> <parent> <groupid> org.mybatis.generator</groupid> <artifaptid>mybati s-generator </artifactid> <バージョン> 1.3.2 </version> </parent> <artifactid> mybatis-generator-core </artifactid> <packaging> jar </packaging> <name> mybatisジェネレーターコア</name> <build> <! - このビルドは、システムプロジェクトで使用する機器のJARファイルを作成およびインストールします。 <プラグイン> <groupId> org.apache.maven.plugins </groupid> <artifactid> maven-site-plugin </artifactid> <executions> <execution> <phase> prapare-package </fase> <goal> <goal> site> site </goal> </goal> </execution> </</plagin> <> < <プラグイン> <groupId> org.apache.maven.plugins </groupid> <artifactid> maven-source-plugin </artifactid> <executions> <execution> <phase> prepare-package </fase> <goal> <goal> <goal> jar-no-fork </goal> </execution> </execution> < <Plugin> <groupId> org.apache.maven.plugins </groupid> <artifactid> maven-source-plugin </artifactid> <バージョン> 2.2.1 </version> <executions> <execution> <id> at tach-sources </id> <goal> <goal> jar </goal> </goal> <configuration> <confignal> <configning> **/org/** </inctrual> </configuration> </execution> </plugin> - > <! - <プラグイン> <groupid> org.apache.maven.plugins </groupid> <artifactid> maven-javadoc-plugin </artifactid> <実行> <実行> <フェーズ - > <Plugin> <groupId> org.codehaus.mojo </groupid> <artifactid> cobertura-maven-plugin </artifactid> <executions> <execution> <id> cobertura-instrument </id> <phas> <phas> phas> phas> per-Integration-test </</execution> </plugin> <! - <Plugin> <groupId> org.apache.maven.plugins </groupid> <artifactid> maven-jar-plugin </artifactid> <configuration> <archive> <manifest> < Mainclass> org.mybatis.generator.api.shellrunner </mainclass> </manifest> </archive> </configuration> <executions> <execution> <id> cober tura-jar </id> <phase> Integration-test </phase> <goal> <goal> jar </goal> </goal> <configuration> <configuration> <configuration> cobertura </classifier> <classifier> <classesdirectory> $ {beadir}/ターゲット/genated-classes/cobertura </classdirectory> </conferation - > <Plugin> <groupId> org.apache.maven.plugins </groupid> <artifactid> maven-jar-plugin </artifactid> <バージョン> <バージョン> <バージョン> <executions> <id> attach-jar </id> <フェーズ>統合テスト</フェーズ> <ゴール> <goa l> jar </goal> </goal> </execution> </executions> <configuration> <manifest> <manifest> <manifest> <addclasspath> true </addclasspath> <classpathprefix> </classpathprefix> <mainclass> org.mybatis.generator.api.shellrunner </main class> </manifest> </archive> <include> <cluded> **/org/** </incrubs> </incruction> configuration> </plugin> <plugin> <groupid> org.apache.maven.plugins </groupid> <artifactid> maven-install-plugin </artifactid> <exe Cutions> <Execution> <id> cobertura-install </id> <phase> Integration-test </phase> <goal> intill </goal> </goal> <Configuration> <combertura> cobertura </classifier> </configuration> </execution> </executions> </plugin> <! - <plugin> <groupid> org.apache.maven.plugins </groupid> <artifactid> maven-assembly-plugin </artifactid> <configuration> <descriptor> $ {beadir}/src/main/assembly/src.xml <Executions> <Execution> <id> bundle </id> <goal> goal>シングル</goal> </goal> <phase>パッケージ</phase> </execution> </executions> </plugin> - > <Plugin> <groupId> org.apache.maven.plugins </groupid> <artifactid> maven-assembly-plugin </artifactid> <configuration> <appendasembly id> false </appendasemblyid> <descriptors> <descriptor> $ {beadir} /src/main/assembly/src.xml </descriptor> </descriptors> </configuration> <Executions> <Execution> <id> make-assembly </id> <phase>パッケージ</phase> <goal> single </goal> </goal> </execution> </execution> </plugin> <plugin> <groupid> com.googlecode.maven-gcu-plugin </groupid> maven-gcu-plugin tifactid> <Executions> <Execution> <phase> deploy </phase> <gogle> <goal> upload </goal> </goal> <configuration> <configuration> <uploads> <upload> <file> $ {project.build.directory}/$ {project.artifactid} - $ {$ {project.version} -bundle.zip </file> $ {project.version} </summary> <labels> <label> featured </label> <label> Type-archive </label> <label> Product-Generator </label> <abol>バージョン - $ {project.version} </labels> </upload> </uploads> </configuration >/plugin> <plugin> <groupid> org.apache.maven.plugins </groupid> <artifactid> maven-release-plugin </artifactid> <configuration> <arguments> -prelease、grproad </arguments> </configuration> </plugin> </bugins> bui ld> <報告> <プラグイン> <プラグイン> <groupId> org.codehaus.mojo </groupid> <artifactid> jdepend-maven-plugin </artifactid> <バージョン> 2.0-ベータ-2 </version a-maven-plugin </artifactid> </plugin> </plaginins> </reporting> <dependencies> <dependency> log4j </groupid> <artifactid> log4j </artifactid> <scope> upid> <artifactid> ant </artifactid> <scope> revided </scope> </dependency> <dependency> groupid> junit </groupid> <artifactid> <artifactid> <scope>テスト</scope> </dependency> <依存関係> <依存関係> <グループactid> hsqldb </artifactid> <scope> test </scope> </dependency> <dependency> groupid> org.apache.maven.plugins </groupid> <artifactid> maven-surefire-plugin </artifactid> <バージョン> 2.12 </バージョン<url> https://mybatis.googlecode.com/svn/sub-projects/generator/tags/mybatis-generator-1.3.2/mybatis-generat or-core </url> <connection> scm:svn:https://mybatis.googlecode.com/svn/sub-projects/generator/tags/mybatis-gen Ether-1.3.2/mybatis-generator-core </connection> <DevelopERConnection> SCM:svn:https://mybatis.googlecode.com/svn/sub-projects/generator/tags/mybatis-generator-1.3.2/mybatis-generator-core-core-core </scm </scm </scm次に、SRC/Main/Assembly/SRC.XMLコードを変更します
<アセンブリxmlns = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi = "http://www.w3.org/200 1/xmlschema-instance "xsi:schemalocation =" http://maven.apache.org/plugins/maven-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id> bundle</id> <formats> <format> zip< </format> </formats> <filesets > <fileset> <directory> src/main/resources </directory> <OutputDirectory>/</outputDirectory> <include> <including>属orconfig.xml </include> </include> <excludes> <explude> log4j.properties </explude> <exclude> src.xml </explude> </expludes> </fileset> <fileset> <directory> src/main/scrips ude> run.bat </include> </include> fileset> <fileset> <fileset> <directory> $ {project.build.directory} </directory> <outputdirectory>/</outputdirectory> <conting> <contaud> $ {project.artifactid} - $ {$ {project.vinction} LESET> <Fileset> <Directory> $ {project.build.directory} </directory> <OutputDirectory>/</outputDirectory> <include> <incture> $ {project.artifactid} - $ {project.version} -sours.jar </include次に、SRC/Main/の下に新しいスクリプトフォルダーを作成し、スクリプトフォルダーに新しいTXTテキストドキュメントを作成し、次のコードを入力します
Java -Jar MyBatis -Generator -1.3.2.Jar -Configfile GeneratorConfig.xml上書き
一時停止
run.batにファイル名を変更します
この時点で、パッケージと構成が完了します。
AS-> Maven Buildの[プロジェクト]の下で[右]を右クリックして、目標にパッケージを入力できます。または、コマンドラインにCMDにプロジェクトディレクトリを入力し、MVNパッケージを実行します。ここで初めて実行したときは、Mavenが依存JARパッケージをダウンロードするため、辛抱強く待ってください。
パッケージング後、プロジェクトのルートディレクトリのターゲットディレクトリに次の構造が生成されます。
上記の図から、MyBatis-Generator-Core-1.3.2.Jarパッケージが生成されたことがわかります。次に、それを使用してGeneratorConfig.xmlを追加して、必要なコードを生成できます。
コード変更プロセス中に何も理解していない場合は、ソースコードの詳細をご覧ください。
OK、結果を表示します
この記事はすべてです!
上記は、編集者が紹介したMyBatis Reverse Engineering JARパッケージの変更とパッケージングに関する関連する知識です。私はそれが誰にでも役立つことを願っています!