MyBatis

維基百科,自由的百科全書
跳至導覽 跳至搜尋
MyBatis
開發者MyBatis團隊
目前版本3.5.11(2022年9月18日,​3年前​(2022-09-18
原始碼庫
  • {{URL|example.com|可选的显示文本}}
Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
程式語言Java
引擎
    Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
    作業系統跨平台
    類型持久化框架
    許可協定Apache許可證 2.0
    網站https://mybatis.org/mybatis-3/zh_CN/

    MyBatis是一個Java持久化框架,它通過XML描述符或註解把對象儲存程序SQL陳述式關聯起來,對映成資料庫內對應的紀錄。[1]:226

    MyBatis是在Apache許可證 2.0下分發的自由軟件,是iBATIS 3.0的分支版本,其維護團隊也包含iBATIS的初創成員。[2]

    功能概況[編輯]

    與其他對象關係對映框架不同,MyBatis沒有將Java對象資料庫表關聯起來,而是將Java方法與SQL陳述式關聯。MyBatis允許用戶充分利用資料庫的各種功能,例如儲存程序、視圖、各種複雜的查詢以及某資料庫的專有特性。如果要對遺留資料庫、不規範的資料庫進行操作,或者要完全控制SQL的執行,MyBatis是一個不錯的選擇。

    JDBC相比,MyBatis簡化了相關程式碼:SQL陳述式在一行程式碼中就能執行。MyBatis提供了一個對映引擎,聲明式的把SQL陳述式執行結果與對象樹對映起來。通過使用一種內建的類XML表達式語言,或者使用Apache Velocity整合的外掛程式,SQL陳述式可以被動態的生成。

    MyBatis與Spring FrameworkGoogle Guice英語Google Guice整合,這使開發者免於依賴性問題。

    MyBatis支援聲明式數據快取(declarative data caching)。當一條SQL陳述式被標記為「可快取」後,首次執行它時從資料庫取得的所有數據會被儲存在一段高速緩衝記憶體中,今後執行這條陳述式時就會從高速緩衝記憶體中讀取結果,而不是再次命中資料庫。MyBatis提供了基於 Java HashMap 的預設快取實現,以及用於與OSCache英語OSCacheEhcache英語EhcacheHazelcast英語HazelcastMemcached連接的預設連接器。MyBatis還提供API供其他快取實現使用。

    用法[編輯]

    SQL陳述式儲存在XML檔案或Java 註解中。一個MyBatis對映的範例(其中用到了Java介面和MyBatis註解):

    package org.mybatis.example;
    
    public interface BlogMapper {
        @Select("select * from Blog where id = #{id}")
        Blog selectBlog(int id);
    }
    

    執行的範例:

    BlogMapper mapper = session.getMapper(BlogMapper.class);
    Blog blog = mapper.selectBlog(101);
    

    SQL陳述式和對映也可以外化到一個XML檔案中:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="org.mybatis.example.BlogMapper">
        <select id="selectBlog" parameterType="int" resultType="Blog">
            select * from Blog where id = #{id}
        </select>
    </mapper>
    

    也可以使用MyBatis API執行陳述式:

    Blog blog = session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
    

    詳細資訊可以參考MyBatis網站所提供的用戶手冊。參見外部連結。

    與Spring整合[編輯]

    MyBatis與Spring Framework整合。Spring Framework允許MyBatis參與Spring事務,建立了MyBatis對映器和對談,並把他們注入到其他bean中。

    如下所示是一個基本的XML組態範例:建立了對映器,並注入到「BlogService」bean中。

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        <property name="mapperInterface" value="org.mybatis.example.BlogMapper" />
    </bean>
    
    <bean id="blogService" class="org.mybatis.example.BlogServiceImpl">
        <property name="blogMapper" ref="blogMapper" />
    </bean>
    

    現在呼叫MyBatis只需要呼叫一個bean:

    public class BlogServiceImpl implements BlogService {
    
        private BlogMapper blogMapper;
    
        public void setBlogMapper(BlogMapper blogMapper) {
            this.blogMapper = blogMapper;
        }
    
        public void doSomethingWithABlog(int blogId) {
            Blog blog = blogMapper.selectBlog(blogId);
            ...
        }
    }
    

    Velocity語言[編輯]

    Velocity語言驅動程式允許用戶使用Apache Velocity來快速生成動態SQL查詢。

    <select id="findPerson" lang="velocity">
      #set( $pattern = $_parameter.name + '%' )
      SELECT *
      FROM person
      WHERE name LIKE @{pattern, jdbcType=VARCHAR}
    </select>
    

    MyBatis生成器[編輯]

    MyBatis提供了碼產生器。MyBatis生成器(MyBatis Generator)能對資料庫表內省,生成執行的增刪改查(CRUD)時所需的MyBatis程式碼。有相關的Eclipse外掛程式可供使用。

    MyBatis Migrations[編輯]

    MyBatis Migrations[註 1]是一個Java控制台應用程式,它通過管理數據定義語言(DDL)檔案來跟蹤資料庫模式英語Database schema的變更。[註 2]

    Migrations可以查詢當前資料庫的狀態,應用或恢復對資料庫模式英語Database schema的變更。它也有助於發現和解決由多個開發人員並列修改資料庫模式的情況。

    歷史[編輯]

    MyBatis專案繼承自iBATIS 3.0,其維護團隊也包含iBATIS的初創成員。

    2010年5月19日專案建立。當時Apache iBATIS 3.0發佈,其開發團隊宣佈會在新的名字、新的站點中繼續開發[3]

    2013年11月10日,專案遷移到了GitHub[4]

    參見[編輯]

    註腳[編輯]

    1. ^ 可譯作「MyBatis資料庫遷移管理工具」。
    2. ^ 模式遷移英語Schema migration

    參考文獻[編輯]

    1. ^ 周冠亞、黃文毅. Spring 5企業級開發實戰. 清華大學出版社. 2019. ISBN 9787302531029. 
    2. ^ iBATIS Home. ibatis.apache.org. [2020-11-11]. (原始內容存檔於2020-07-11). 
    3. ^ iBATIS Project Team Moving to Google Code. [2014-04-08]. (原始內容存檔於2016-03-04). 
    4. ^ Bye Google Code welcome Github. [2014-04-08]. (原始內容存檔於2013-11-10). 

    外部連結[編輯]