springboot統合mybatis plus(未完)


一、mybatis plusを紹介します。
mybatis plusはmybatisに基づいて開発された補助プラグインであり、mybatis機能を強化することが目的です。短所:ほとんどは単に表で調べます。多くの表は自分で書きます。利点:
  • は、一般的なCRUDのために、内部パッケージの良い方法を直接起動する。
  • は、条件クエリーに対して、mybatis plusの条件コンストラクタを使用して構成することができる。
  • 二、使用開始
  • 導入依存度
  • 
        org.springframework.boot
        spring-boot-starter-parent
        2.2.4.RELEASE
        
    
    
  • は、ymlまたはpropertiesにデータベース情報
  • を配置する。
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: 
        username: 
        password: 
        type: com.alibaba.druid.pool.DruidDataSource
    
  • プロジェクト起動クラスにmapperスキャン
  • を追加します。
    //           ,        mapper  。
    @MapperScan("com.mybatisplus.mapper")
    
  • エンティティクラス
  • を追加します。
    //               ,         
    //           @TableId
    //              (            ),    @TableField(exist = false)  ,              
    //   : 
    @TableField(exist = false)
     private User user;
    
  • 簡単に
  • を使います。
    //  serviceImpl    ,selectById()  mybatis plus     
    public Topic getTopicById(int tid) {
        return topicMapper.selectById(tid);
    }
    
    三、その他の使用
  • 普通単表CRUD略
  • 多表クエリ環境:ユーザーテーブルuser、招待状topic需要:すべての招待状を取得し、一緒に投稿者の名前を検索して実現する:
  • //mapper     mapper  
    
    /**
     * 1.        
     * 2. Results:       ,  xml     ,            
     * 3. id:           ,      
     * 4. value:      
     * 5.property:    topic       (private User user)
     * 6. column:    topic                        (user)
     * 7. @one  :  column        user       topic  user  
     * 
     * 
     */
    @Select("select * from topic")
    @Results(id = "topicMap",value = {
                @Result(property = "user",column = "t_uid",one = @One(select = "com.henu.mapper.UserMapper.selectById"))
        })
    List getTopic();
    
    この時点で照会した結果、userのtopicを持って、連表クエリを完了しました。