Mybatis-Plus



概要
MyBatis-Plus(略称MP)はMyBatisのエンハンスメントツールであり、MyBatisをベースにエンハンスメントのみを行い変更せず、開発を簡素化し、効率を向上させるために生まれた.
 
とくせい
  • 侵入なし:増強だけして変更せず、導入は既存の工事に影響を及ぼさず、糸のように滑らかである
  • の損失が小さい:起動すると自動的に基本CURDに注入され、性能は基本的に損失がなく、直接対象操作
  • に向かう.
  • 強大なCRUD操作:汎用Mapper、汎用サービスを内蔵し、少量の配置だけで単表の大部分のCRUD操作を実現でき、更に強大な条件コンストラクタがあり、各種類の使用需要を満たす
  • はLambda形式呼び出しをサポートする:Lambda式を通じて、各種類のクエリー条件を簡単に作成することができ、フィールドの書き間違えを心配する必要はありません.
  • は多種のデータベースをサポートする:MySQL、MariaDB、Oracle、DB 2、H 2、HSQL、SQLite、Postgre、SQLServer 2005、SQLServerなど多種のデータベース
  • をサポートする.
  • はプライマリ・キーの自動生成をサポートする:最大4種類のプライマリ・キー・ポリシー(分散型一意IDジェネレータ-Sequenceを含む)をサポートし、自由に構成でき、プライマリ・キーの問題
  • を完璧に解決する.
  • はXMLホットロードをサポートする:Mapper対応のXMLはホットロードをサポートし、簡単なCRUD操作に対してはXMLなしで
  • を起動することもできる.
  • ActiveRecordモードをサポートする:ActiveRecord形式呼び出しをサポートし、エンティティークラスはModelクラスを継承するだけで強力なCRUD操作
  • を行うことができる.
  • カスタムグローバル汎用操作をサポート:グローバル汎用メソッド注入(Write once,use anywhere)
  • をサポート
  • サポートキーワード自動エスケープ:サポートデータベースキーワード(order、key......)自動エスケープ、キーワード
  • もカスタマイズ可能
  • 内蔵コードジェネレータ:コードまたはMavenプラグインを使用すると、Mapper、Model、Service、Controllerレイヤコードを迅速に生成できます.テンプレートエンジンをサポートします.さらに、カスタム構成など、
  • を使用します.
  • 内蔵ページングカード:MyBatis物理ページングに基づいて、開発者は具体的な操作に関心を持つ必要がなく、プラグインを構成した後、ページングを書くのは普通のListクエリー
  • と同じである.
  • 内蔵性能分析プラグイン:Sql文とその実行時間を出力でき、テストの開発時にこの機能を有効にすることを提案し、遅いクエリー
  • を迅速に取り出すことができる.
  • 内蔵グローバルブロックプラグイン:全表delete、update操作インテリジェント分析ブロックを提供し、ブロックルールをカスタマイズし、誤操作を予防する
  • 内蔵Sql注入剥離器:Sql注入剥離をサポートし、Sql注入攻撃
  • を有効に予防する.
    インストール
    springboot 
    
    
        com.baomidou
        mybatis-plus-boot-starter
        3.1.1
    
    
    
    SpringMVC
    
    
        com.baomidou
        mybatis-plus
        3.1.1
    

     
    コード生成
       entity service mapper    
    
    package com.lp.springbootmybatisplus;
    
    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    import com.baomidou.mybatisplus.core.toolkit.StringPool;
    import com.baomidou.mybatisplus.core.toolkit.StringUtils;
    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.InjectionConfig;
    import com.baomidou.mybatisplus.generator.config.*;
    import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    import com.baomidou.mybatisplus.generator.config.rules.DateType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class CodeGenerator
    {
        /**
         *         
         */
        private static final String PARENT_PATH = "com.lp";
    
        /**
         *   
         */
        private static final String AUTHOR = "lipeng";
        /**
         * 

    * *

    */ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append(" " + tip + ":"); System.out.println(help.toString()); if(scanner.hasNext()) { String ipt = scanner.next(); if(StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException(" " + tip + "!"); } public static void main(String[] args) { // AutoGenerator mpg = new AutoGenerator(); // GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor(AUTHOR); gc.setOpen(false); gc.setEntityName("%s"); gc.setDateType(DateType.ONLY_DATE); // gc.setSwagger2(true); Swagger2 mpg.setGlobalConfig(gc); // DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); // PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner(" ")); pc.setParent(PARENT_PATH); mpg.setPackageInfo(pc); // InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { } }; // freemarker //String templatePath = "/templates/mapper.xml.ftl"; // velocity String templatePath = "/templates/mapper.xml.vm"; // List focList = new ArrayList<>(); // focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // , Entity 、 xml !! return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); //strategy.setSuperEntityClass("com.lp.common.BaseEntity"); strategy.setEntityLombokModel(false); strategy.setRestControllerStyle(false); // strategy.setSuperControllerClass("com.lp.common.BaseController"); strategy.setInclude(scanner(" , ").split(",")); //strategy.setSuperEntityColumns("id"); strategy.setControllerMappingHyphenStyle(true); //strategy.setEntityTableFieldAnnotationEnable(true); strategy.setRestControllerStyle(false); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

    内蔵mapperメソッド
    /**
     * Mapper       ,     mapper.xml   ,    CRUD  
     * 

    Mapper id

    * * @author hubin * @since 2016-01-23 */ public interface BaseMapper extends Mapper { /** * * * @param entity */ int insert(T entity); /** * ID * * @param id ID */ int deleteById(Serializable id); /** * columnMap , * * @param columnMap map */ int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap); /** * entity , * * @param wrapper ( null) */ int delete(@Param(Constants.WRAPPER) Wrapper wrapper); /** * ( ID ) * * @param idList ID ( null empty) */ int deleteBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList); /** * ID * * @param entity */ int updateById(@Param(Constants.ENTITY) T entity); /** * whereEntity , * * @param entity (set , null) * @param updateWrapper ( null, entity where ) */ int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper); /** * ID * * @param id ID */ T selectById(Serializable id); /** * ( ID ) * * @param idList ID ( null empty) */ List selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList); /** * ( columnMap ) * * @param columnMap map */ List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap); /** * entity , * * @param queryWrapper ( null) */ T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** * Wrapper , * * @param queryWrapper ( null) */ Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** * entity , * * @param queryWrapper ( null) */ List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** * Wrapper , * * @param queryWrapper ( null) */ List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** * Wrapper , *

    * * @param queryWrapper ( null) */ List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** * entity , ( ) * * @param page ( RowBounds.DEFAULT) * @param queryWrapper ( null) */ IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); /** * Wrapper , ( ) * * @param page * @param queryWrapper */ IPage> selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); }