1。MyBatis Reverse Engineeringとは何ですか
MyBatisを使用する場合、プログラマーはSQLステートメントを自分で作成する必要があります。単一テーブルのSQLステートメントの数は非常に大きいです。 MyBatisの役人は、データベーステーブルに基づいてMyBatis実行コードを生成するツールを提供します。このツールはリバースエンジニアリングです。
リバースエンジニアリング:単一のデータベーステーブルのコード(mapper.xml、mappper.java、pojo)を生成 - >
MyBatis-Generator-Core-1.3.2.Jar-リバースエンジニアリング操作に必要なJARコアパッケージ
2。リバースエンジニアリング用の構成ファイルを構成します
構成ファイルGeneratorConfig.xml
<?xml version = "1.0" encoding = "utf-8"?> <!doctype generatorconfiguration public " - // mybatis.org//dtd mybatisジェネレーター構成1.0 // en" "http://mybatis.org/dtd/mybatis-generator-config_1_0 id = "testables" targetruntime = "mybatis3"> <commentgenerator> <! - 自動生成コメントを削除するかどうか真:はい:false:いいえ - > <プロパティ名= "suppressallcomments" value = "true" /> < /commentgenerator> < driverclass = "com.mysql.jdbc.driver" connectionurl = "jdbc:mysql:// localhost:3306/mybatis" userid "userid =" root "password =" 123 "> </jdbcconnection> <! - <jdbcconnectionclass =" oracle.jdbc.oracledriver " Connectionurl = "JDBC:Oracle:Thin:@127.0.0.1:1521:YYCG" userId = "yycg" password = "yycg"> </jdbcconection> - > <! - デフォルトのfalse、jdbc decimal and numeric型を整数に解析し、JDBCデシマルとムーダブのタイプを解析するとき、 - > <javatyperesolver> <プロパティ名= "forcebigdecimals" value = "false"/> </javatyperesolver> <! - ターゲットプロジェクト:POクラスが生成される場所 - > <javamodelgeneratorターゲットパッケージ= "スキーマは、パッケージの接尾辞として使用されます - > <プロパティ名= "enablesubpackages" value = "false" /> <! - データベースから返された値がクリーニングされた前と後のスペース - > <プロパティ名= "トリムストリング=" value = "true" /> < /javamodelgenerator> < /javamodelgenerator> <! TargetPackage = "cn.zm.mybatis.mapper" targetproject = "./ src"> <! - enablesubpackages:schemaをパッケージの接尾辞として使用するかどうか - > <プロパティ名= "enableSubpackages" value = "fals"/> </sqlmapgenerator> < type = "xmlmapper" targetpackage = "cn.zm.mybatis.mapper" stargeproject = "./ src"> <! - enablesubpackages:schemaをパッケージのサフィックスとして使用するかどうか - > <プロパティ名= "enableSubPackages" value = "false"/> </> javacliententerator> <> < TableName = "items"> </table> <! - <table tablename = "orders"> </table> <table tablename = "orderdetail"> </table> <table tablename = "user"> </table> - > <! - <table schema = "" tablename = "sys_user"> </<table schema = "sys_per schema = "" tablename = "sys_user_role"> </table> <table schema = "" tablename = "sys_role_permission"> </table> - > <! - いくつかのテーブルフィールドはJavaタイプ<テーブルスキーマ= "" tablename = "" "> <columnOverride column =" "javatepe =" </context> </generatorConfiguration>
3.リバースエンジニアリングを実行してコードを生成します
Javaクラスメソッドを実行します:
生成されたコードは次のとおりです。
4.テストのために生成されたコードをビジネスシステムプロジェクトにコピーする
パブリッククラスitemsmappertest {private applicationContext ApplicationContext;プライベートアイテムマッパーアイテムマッパー。 @before public void setup()throws Exception {applicationContext = new ClassPathxMLApplicationContext( "classPath:ApplicationContext.xml"); itemsmapper =(itemsmapper)applicationContext.getBean( "itemsmapper"); } //ルートプライマリキー@test public void deletebyprimarykey(){itemsmapper.deletebyprimarykey(4); } @test public void insert(){} @test public void selectbyexample(){itemsexample itemsexample = new itemsexample(); itemsexample.criteria criteria = itemsexample.createCriteria(); //基準を使用して、クエリ条件基準をカスタマイズします。 Criteria.andidequalto(1);リスト<アイテム> list = itemsmapper.selectbyexample(itemsexample); System.out.println(list); } @test public void selectbyprimarykey(){itemsアイテム= itemsmapper.selectbyprimarykey(1); System.out.println(items); } @test public void updatebyprimarykey(){}}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。