MyBatisは半自動性ORMです。このフレームワークを使用する際には、マッピングマッピングファイルを書き込むことです。マニュアルライティングは簡単に間違いを犯すことができるため、MyBatis-Generatorを使用してファイルを自動的に生成するのに役立ちます。
リバースエンジニアリング
1。リバースエンジニアリングとは何ですか
MyBaitsは、プログラマーがSQLステートメントを自分で作成することを要求しています。 MyBatisの役人は、MyBatis実行に必要なコードを自動的に生成するためのリバースエンジニアリングを提供します(mapper.java、mapper.xml、po ..)
実際のエンタープライズ開発で使用される一般的なリバースエンジニアリング方法:
データベースのテーブルにより、Javaコードが生成されます。
2。リバースエンジニアリングをダウンロードします
MyBatis-Generator-Core-1.3.2-Bundle.Zip
3。使用方法(使用できます)
3.1リバースエンジニアリングを実行します
公式文書で提供されるリバースエンジニアリングを実行するいくつかの方法
MyBatisジェネレーターを実行します
MyBatisジェネレーター(MBG)は、次の方法で実行できます。
(1)XML構成を備えたコマンドプロンプトから
(2)XML構成のANTタスクとして
(3)Mavenプラグインとして
(4)XML構成を備えた別のJavaプログラムから
(5)Javaベースの構成を備えた別のJavaプログラムから
(6)Eclipseプラグインを介してコードを生成することもできます
Javaプログラム方法(XML構成を備えた別のJavaプログラムから)を使用して、開発ツールに依存しないことをお勧めします。
以下は、リバースファイルを生成し、自動的に生成されたファイルを元のプロジェクトにコピーするプロジェクトです(これは、ソースファイルで直接生成され、同じ名前でファイルを上書きするように行われます)。インポートされたJARパッケージとプロジェクト構造のスクリーンショットは次のとおりです。
示されているように
3.2コード構成ファイルを生成します
GeneratorConfig.xml:
<?xml version = "1.0" encoding = "utf-8"?> <!doctype generatorconfigurationpublic " - // mybatis.org//dtd mybatisジェネレーター構成1.0 // en" "http://mybatis.org/dtd/mybatis-generator-config_1_0 id = "testables" targetRuntime = "mybatis3"> <commentgenerator> <! - 自動生成コメントを削除するかどうか真:はい:false:no-> <プロパティ名= "suppressallcomments" value = "true" /> < /commentgenerator> < driverClass = "com.mysql.jdbc.driver" connectionurl = "jdbc:mysql:// localhost:3306/mybatis" userid = "root" password = "1234"> </jdbcconnection> <! - <jdbcconnection driverclass = "oracle.jdbc.oracledriver" connectionurl = "jdbc:oracle:thin:@127.0.0.1:1521:yycg" userid = "yycg" password = "yycg"> </jdbcconnection> - > <! - デフォルトのdecimal decimalのdecimal decimalのdecimal decimal decimal decimal decimal decimal decimal decimal decimal decimal decimal decimal decimal decimalを解析するJava.math.bigdecimalへの数値タイプ - > <javatyperesolver> <プロパティ名= "forcebigdecimals" value = "false" /> < /javatyperesolver> <! - ターゲットプロジェクト:POクラスが生成される場所 - > <javamodelgenatoratorature TargetPackage = "cn.edu.hpu.ssm.po" targetproject = "./ src"> <! - enableSubpackages:schemaをパッケージの接尾辞にするかどうか - > <プロパティ名= "enableSubpackages" value = "false" /> < /> </javamodelgenerator> <! - ターゲットプロジェクト:マッパーマップファイルが生成される場所 - > <sqlmapgenerator targetpackage = "cn.edu.hpu.sssmapper"ターゲットプロジェクト= "./ src"> <! - eanbassubpackages:eanbackpackages = < value = "false"/> </sqlmapgenerator> <! - ターゲットパッケージ:マッパーインターフェイス生成の位置 - > <javaclientgeneratorタイプ= "xmlmapper" targetpackage = "cn.edu.hpu.sss.mapper" stargentproject = "./ src"> < - > <プロパティname = "enablesubpackages" value = "false"/> </javaclientgenerator> <! - データベーステーブルを指定 - > <table tablename = "items"> </table tablename = "and"> </table> <table tablename = "orderdetail"> </<table = <table < TableName = "Sys_user"> </table> <table schema = "" tablename = "sys_role"> </table schema = "" tablename = "sys_permission"> </table schema = "" tablename = "sys_user_role"> - > <! - 一部のテーブルフィールドには、Java Type <Table Schema = "" tableName = "" "> <columnOverride column =" "javatype =" "/> </table> - > </context> </generatorConfiguration>
3.3ジェネレータープログラムを実行します
generatorsqlmap.java:
import java.io.File;import java.util.ArrayList;import java.util.List;import org.mybatis.generator.api.MyBatisGenerator;import org.mybatis.generator.config.Configuration;import org.mybatis.generator.config.xml.ConfigurationParser;import org.mybatis.generator.internal.defaultshellcallback; public class generatorsqlmap {public void generator()throws {list <string>警告= new arraylist <string>(); boolean overwrite = true; // load configuration file configfile = new file( "generatureconfig.xml"); configurationparser cp = new ConfigurationParser(警告); configuration Config = cp.parseconfiguration(configfile); defaultshellcallback callback = new defaultshellcallback(overwrite); mybatisgenerator = mybatisgenerator = new mybatisgenerator(new mybatisgenerator(config、config、config、config、config、config、config、config、warning); static void main(string [] args)throws exception {try {generatorsqlmap generatorsqlmap = new generatorsqlmap(); generatorsqlmap.generator();} catch(exceps e){e.printstacktrace();}}}}}生成コード:
3.4生成されたコードの使用
生成されたプロジェクトの生成コードを独自のプロジェクトにコピーする必要があります。 itemsmapper.javaとitemmapper.xml、items、itemsexampleクラスを元のプロジェクトにコピーしましょう。
itemsmapperのテスト方法
パッケージcn.edu.hpu.ssm.test; import static org.junit.assert.fail; Import java.util.date; Import java.util.list; Import org.junit.before; import org.junit.test; import org.springframework.context.context.context; Import.context.context.context.context.context.context.context org.springframework.context.support.classpathxmlapplicationcontext; Import cn.edu.hpu.ssm.mapper.itemsmapper; import cn.edu.hpu.sssm.po.items; import cn.edu.hpu.ssssm.po.po.itemsexamplet; ApplicationContext; Private ItemsMapper ItemsMapper; // Annotationこのメソッドを呼び出す前に、このクラスのすべてのテストメソッドを実行する前に@beforepublic void setup()throws exception {applicationContext = new classPathxmlapplicationContext( "classpath:spring/applicationcontext.xml"); itemsmapper =(itemsmapper)applicationContext.getBean( "itemsmapper");} // delete @testpublic testdeletebyprimarykey(){emunted "); item(); items.setName( "iPhone-5s"); items.setPrice(3999f); items.setDetail( "Authentic"); items.setpic( "sdasd.jpg"); items.setcreatetime(new date()); itemmapper.insert(item);} {itemsexample itemsexample = new itemsexample(); // criteria itemsexample.criteria criteria = itemsexample.createcriteria(); criteria.andnameequalto( "TV"); //複数のレコードリスト<アイテムリスト>リスト= itemecbyexample(itemsexample(項目); for(int i = 0; i <list.size(); i ++){items it = list.get(i); System.out.println(it.getid()+":"+it.getName());}} //プライマリキー@testpublic void void void void selectbyprimarykey(){items items = itemsmapper.selectbyprimarykey(1); system.out.out.println(items.getname();}///////////// testUpdateByPrimaryKey(){//すべてのフィールドを更新するには、最初にクエリをしてから項目を更新する必要があります。 items.setName( "iPhone"); itemsmapper.updatebyprimarykey(items); //着信フィールドが空でない場合は、この方法をバッチアップデートで使用します。最初にクエリをしてから、//itemsmapper.updatebyprimarykeyselectutive(record);}}を更新する必要はありません。上記は、編集者によって紹介されたMyBatisフレームワークによって生成されたMyBatisリバースエンジニアリング自動コードです。私はそれが誰にでも役立つことを願っています!
wulin.comからの推奨読み取り:
mybatisはじめに学習チュートリアル(i) - mybatisクイックスタート
mybatis oracle blobタイプのフィールドの詳細な分析保存と読み取り
mybatisの練習ダオとマッパー
MyBatis実践における動的SQLと関連するクエリ