MyBatis-Plus(略してMP)はMyBatis強化ツールです。 MyBatisに基づいて、開発を単純化し、効率を改善するために、強化され、変化しないだけで生まれています。
中国文書:http://baomidou.oschina.io/mybatis-plus-doc/#/
この記事で紹介します
1)それを構築する方法
2)コード生成(コントローラー、サービス、マッパー、XML)
3)単一のテーブルのCRUD、条件付きクエリ、およびページネーションベースクラスがあなたのために行われました
1。それを構築する方法
1.最初にスプリングブートプロジェクトを作成します - > https://start.spring.io/
2。メイブン依存関係
<Dependency> groupId> com.baomidou </groupid> <artifactid> mybatis-plus-boot-starter </artifactid> <version> 2.3 </version> </dependency> <! - コード生成の速度依存関係 - > <依存関係> <groupid> org.apache.velociti <バージョン> 2.0 </version> </dependency>
3.構成(私はそれがあまりにも言葉であると思うので、データソースの構成はここで省略されています)
Application.Properties
mybatis-plus.mapper-locations = classpath:/mapper/*mapper.xmlmybatis-plus.typealiasespackage = com.taven.web.springbootmp.entitymybatis-plus.global-config.id-type = 3mybatis-plus.global-configt.field-config.foid-config.foid-config. egy = 2mybatis-plus.global-config.db-column-dunderline = truemybatis-plus.global-config.key-generator = com.baomidou.mybatisplus.incrementer.OracleKeygeneratormybatis-plus.globalconfig.logic-delete-value = 1 mybatis-plus.global-config.logic-not-delete-value = 0mybatis-plus.global-config.sql-injector = com.baomidou.mybatisplus.mapper.logicsqlinjector#これは、クラスに変更する必要があります.taven.web.springbootmp.mymetaobjedlermybatis-plus.configuration.map-underscore-to-camel-case = truemybatis-plus.configuration.cache-enabled = falsemybatis-plus.configuration.jdbc-type-for-null
構成クラスmybatisplusconfig
org.mybatis.spring.annotation.mapperscan; Import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; Import com.baomidou.mybatisplus.incrementer.h2KeyGenerator; Import; com.baomidou.mybatisplus.incrementer.ikeygenerator; import com.baomidou.mybatisplus.mapper.isqlinjector; Import com.baomidou.mybatisplus.mapper.logicsqlinjector; import com.baomidou.mybatisplus.matebatisplus.matebatisplus.matebatisplus.matebatisplus.matebatisplus. com.baomidou.mybatisplus.plugins.paginationinterceptor; Import com.baomidou.mybatisplus.plugins.performanceInterceptor; Import com.taven.web.springbootmp.mymetaobjecthandler;@enabletransactionmanagement@mapperscan( "com.taven.web.springbootmp.mapper")Public Class MyBatisPlusConfig { / ** * MyBatis-Plus Publician [Performincution exefiency Plagpin] Pablian performanceInterceptor(){return new PerformanceInterceptor(); } / * *ページネーションプラグイン、データベースタイプのマルチテナントを自動的に識別するには、公式Webサイト[プラグイン拡張子]を参照してください。 } @bean public metaobjecthandler metaobjecthandler(){return new mymetaobjecthandler(); } / ***インジェクションプライマリキージェネレーター* / @bean public ikeygenerator keygenerator(){return new h2keygenerator(); } / *** indection sql injector* / @bean public isqlinjector sqlinjector(){return new logicsqlinjector(); }} com.baomidou.mybatisplus.mapper.metaobjecthandler; import org.apache.ibatis.reflection.metaobject; Import org.slf4j.logger; import org.slf4j.loggeractory;/** *公共フィールドを自動化する、オプションの注射方法 * ///@componedpublicmaymaymaymaymayma MetaObjectHandler {protected final static logger logger = loggerFactory.getLogger(Application.Class); @Override public void insertFill(MetaObject MetaObject){logger.info( "追加されたときに説明できないことをしない"); } @Override public void updateFill(MetaObject MetaObject){logger.info( "更新時に何かを説明できないことを行う"); }} 2。コード生成
Junitを実行して、コントローラー、サービスインターフェイス、実装、Mapper、XMLを生成します
import org.junit.test; Import com.baomidou.mybatisplus.generator.autogenerator; import com.baomidou.mybatisplus.generator.config.datasourceconfig;インポートcom.baomidou.mybatisplus.generator.config.globalconfig; Import; com.baomidou.mybatisplus.generator.config.packageconfig; Import com.baomidou.mybatisplus.generator.config.strategyconfig; Import com.baomidou.mybatisplus.generator.config.rules.dbtype; com.baomidou.mybatisplus.generator.config.rules.namingstrategy;/** * <p> *テスト生成コード * </p> * * @author k god * @date 2017/12/18 */public class geneeratorserviceentity {@test public void generatecode(){sinr 「com.taven.web.springbootmp」; boolean servicenamestartwithi = false; // user-> userservice、true:user-> iuserservice generatebytables(servicenamestartwithi、packagename、 "cable"、 "station"); = new GlobalConfig(); string dburl = "jdbc:mysql:// localhost:3306/communicate"; DataSourceConfig DataSourceConfig = new DataSourceConfig(); DataSourceConfig.setDBType(dbtype.mysql).seturl(dburl).setUsername( "root").setPassWord( "root").setDrivername( "com.mysql.jdbc.driver"); StrategyConfig StrategyConfig = new StraptyConfig(); StrategyConfig .setCapitalMode(true).setEntityLombokModel(false).setDBColumnunderline(true).setnaming(namingstrategy.underline_to_camel).setinclude(tablenames); .setauthor( "yin tianwen").setOutputdir( "e:// dev // stsdev // spring-boot-mp // src // main // java").setfileoverride(true); if(!servicenamestartwithi){config.setservicename( "%sservice"); } new autogenerator()。setglobalconfig(config).setDataSource(dataSourceConfig).setStrategy(Strategyconfig).setPackageInfo(new PackageConfig().setParent(packagename).setcontroller( "Controller").Setentity( ").setentity(").Setentity( ")。 } // private void generateBytables(string packagename、string ... tablenames){// generateBytables(true、packagename、tablename); //}}}建設は基本的にこの時点で完了しており、以下で使用を開始できます!
3. mybatis-plusを使用します
最初に、上記の生成コード()を実行し、テーブル構造に基づいて次のコードを生成します(XMLは次の手動で移動しました)。サービスとマッパーは基本クラスを継承し、私たちのために多くの方法をカプセル化しました。以下のいくつかの簡単な例を見てみましょう。
/** * <p> *フロントエンドコントローラー * </p> * * @author yin tianwen * @since 2018-05-31 */ @controller @requestmapping( "/cable")パブリッククラスcablecontroller {@autowired private cableservice cableservice; /***リストクエリテスト**/@RequestMapping( "/1")@responsebody public object test1(){//エンティティに対応するエンティティワッパーオブジェクトを構築し、entitywrapper <cable> ew = new entitywrapper <>(); ew.where( "type = {0}"、1).like( "name"、 "king").and( "Core_Number = {0}"、24)。リスト<Cable> list = cableservice.selectlist(ew); List <Map <String、Object >> Maps = cableservice.selectmaps(ew); System.out.println(list); System.out.println(マップ); 「OK」を返します。 }/***ページネーションクエリテスト*/@RequestMapping( "/2")@responsebody public object test2(){//エンティティに対応するエンティティワッパーオブジェクトを構築し、フィルタリングクエリエンティティワッパー<ケーブル> ew = new entitywrapper <>(); ew.where( "type = {0}"、1)// .like( "name"、 "king").and( "Core_Number = {0}"、24)。ページ<Cable>ページ=新しいページ<>(1,10); Page <Cable> pagerst = cableservice.selectPage(page、ew); PAGERSTを返す; }/***カスタムクエリフィールド*/@requestMapping( "/3")@responsebody public object test3(){object vl = null; //エンティティに対応するEntityWrapperオブジェクトを作成し、フィルタリングクエリEntityWrapper <Cable> ew = new EntityWrapper <>()を実行します。 ew.setsqlSelect( "id、` name`、 " +" case type/n " +" 1 then '220kv'/n " +" typename ").where(" type = {0} "、1)// .like(" name "、" king ")。ページ<>(1,10); Page <Cable> pagerst = cableservice.selectPage(page、ew); PAGERSTを返す; }/** * insert */@requestMapping( "/4")@responsebody public object test4(){cable c = new cable(); C.SetName( "光学ケーブルをテスト"); cableservice.insert(c); 「OK」を返します。 }/** * update */@requestMapping( "/5")@responsebody public object test5(){cable c = cableservice.selectbyid(22284l); C.SetName( "光学ケーブル2222"をテスト); C.SetType(1); cableservice.updatebyid(c); 「OK」を返します。 }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。