7.MyBatisどうやってシームレスにSpringをドッキングしますか?

8854 ワード

1.なぜMyBatis-pringが現れたのですか?
SpringフレームとMyBatisフレームはJavaインターネット技術の主流フレームです。しかし、MyBatisをSpringフレームにシームレスに統合するにはどうすればいいですか?この時、MyBatis-pringが誕生しました。このクラスのクラスを使うと、Springは必要なMyBatis工場類とsession類をロードします。
Spring 3.0もibatis 2.0だけをサポートしています。元々MyBatis 3のサポートをSpring 3.0に追加しました。残念ながら、Spring 3.0の開発はMyBatis 3.0の公式発表前に終了しました。Spring開発チームは非公開版MyBatisの統合支援を発表したくないからです。MyBatisに対するサポートを放棄しました。
Springに従ってjavaの事実基準の技術枠組みになりつつある。Spring 4.0はiBatisへの直接的なサポートを除いて移動します。MyBatisチームはSpringベースのMyBatis統合Jar-MyBatis-pringを開発した。
2.MyBatis-pringを使うメリット
  • .トラフィック層とモデル層のより良い分離が可能になる。更にSpringフレームの中でMyBatisももっと簡単で、多くのコードを節約します。
  • .Sql Session Factory、Sql Sessiondなどのオブジェクトを表示する必要さえない
  • 3.MyBatis-pringの構成部分
  • 1.構成データソース
  • .Sql Session Factoryを配置する
  • .Sql Session Templateを配置する
  • .Mapper
  • を配置する。
  • .事務処理
  • MyBatisではSql Session Factoryオブジェクトを構築し、Sql Sessionを生成し、MyBatis-pringプロジェクトではSql Sessionの使用はSql Session Templateによって実現され、Sql Session操作のパッケージを提供しています。だからSql Session Templateを通じてMapperを得ることができます。
    4.Spring MVCに配置する
    4.1 Sql Session FactoryBenの配置
    基本的なMyBatisでは、session工場はSql Session FactoryBuiderを使って作成できます。MyBatis-pringではSql Session FactoryBeanを使って代替します。
    
      
    
    
    注意点
  • Sql Session Factoryは、JDBCのDataSource
  • の必須属性を持っています。
    Sql Session FactoryBenのgetObjectに戻る時、検証があります。
      @Override
      public SqlSessionFactory getObject() throws Exception {
        if (this.sqlSessionFactory == null) {
          afterPropertiesSet();
        }
    
        return this.sqlSessionFactory;
      }
    
      @Override
      public void afterPropertiesSet() throws Exception {
        notNull(dataSource, "Property 'dataSource' is required");
        notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
        state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
                  "Property 'configuration' and 'configLocation' can not specified with together");
    
        this.sqlSessionFactory = buildSqlSessionFactory();
      }
    
    4.2 datasourceを配置する
    ここで使っているのはアリババのデータベース接続池です。https://github.com/alibaba/druid
    compile group: 'com.alibaba', name: 'druid', version: '1.1.2'
    
    
    
        
        
        
        
    
    
    4.3データベースリンクの属性を設定する
    resourceディレクトリの下でpropertiesフォルダを作成し、datasource.propertiesを作成します。
    jdbc_url=jdbc:mysql://localhost:3306/cnblogs?serverTimezone=Asia/Shanghai&characterEncoding=utf8
    jdbc_user=root
    jdbc_password=root
    
    アリババのデータベース接続池を使って、データベースドライバを配置する必要がありません。urlのプレフィクスから判断されます。
    4.4配置Springロードプロファイルと配置置換動的ラベル
    
    
        
    
    
    
    
        
    
    
    4.5 Springを配置して自動的にMapperインターフェースを作成するbean
    MyBatis-pringは、変換器MapperScanner Configrerを提供しており、マッピングインターフェースをSpring容器中のBeanに変換することができます。これでコードにマッピングしたbeanを注入することができます。
    
    
        
    
    
    
    basePackage属性は、マッパーインターフェースファイルのための基本的なパケットパスを設定します。セミコロンまたはコンマを使用して、複数のパケットパスをセパレータとして設定できます。各マッパーは、指定されたパケットパスの中で再帰的に検索される。
    4.6配置事務
    MyBatisとSpringを結合した後、Spring AOPを使って事務を管理していますが、Spring AOPを使うのはかなり簡単です。ほとんどの場面では声明式事務を使えばいいです。
  • Jarパケットを導入する
  • compile group: 'org.springframework', name: 'spring-tx', version: '4.3.10.RELEASE'
    compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.10.RELEASE'
    
    
    
        
    
    
    
    
    
    5.Spring xmlの完全な配置と参照のJarパッケージ
  • によって導入されたJarリスト
  • compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.10.RELEASE'
    compile group: 'org.springframework', name: 'spring-tx', version: '4.3.10.RELEASE'
    compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.10.RELEASE'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1'
    compile group: 'org.mybatis', name: 'mybatis', version: '3.4.5'
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
    compile group: 'com.alibaba', name: 'druid', version: '1.1.2'
    
  • Springの構成
  •