詳細介紹MyBatis 3.4.0版本的功能
新增功能
1. Cursor 新增返回值類型為游標(biāo)的方法
當(dāng)查詢大量(上百萬)數(shù)據(jù)的時候,使用游標(biāo)可以有效的減少內(nèi)存使用,不需要一次性將所有數(shù)據(jù)得到,可以通過游標(biāo)逐個或者分批(逐個獲取一批后)處理。
SqlSession 中新增的 3 個游標(biāo)方法:
/** * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator. * @param <T> the returned cursor element type. * @param statement Unique identifier matching the statement to use. * @return Cursor of mapped objects */ <T> Cursor<T> selectCursor(String statement); /** * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator. * @param <T> the returned cursor element type. * @param statement Unique identifier matching the statement to use. * @param parameter A parameter object to pass to the statement. * @return Cursor of mapped objects */ <T> Cursor<T> selectCursor(String statement, Object parameter); /** * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator. * @param <T> the returned cursor element type. * @param statement Unique identifier matching the statement to use. * @param parameter A parameter object to pass to the statement. * @param rowBounds Bounds to limit object retrieval * @return Cursor of mapped objects */ <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds);
注意: 3.4.0 版本的游標(biāo)方法目前有個 bug,因此不支持 @Select 注解方式,在將來的 3.4.1 版本中會解決這個問題。
使用示例:
<select id="selectAll" resultType="tk.mybatis.springboot.model.City"> select * from city </select>
xml 里面沒有任何改變,在獲取值的地方有變化,例如使用接口:
Cursor<City> selectAll();
或者使用命名接口方式:
Cursor<City> cityList = sqlSession.selectCursor("selectAll");
得到結(jié)果后,使用方法如下:
Iterator<City> iterator = cityList.iterator(); while(iterator.hasNext()){ City c2 = iterator.next(); Assert.assertNotNull(c2); Assert.assertNotNull(c2.getName()); Assert.assertNotNull(c2.getState()); }
嵌套查詢的情況
當(dāng)使用嵌套查詢時,還需要設(shè)置resultOrdered="true"
屬性,使用方法如下:
<select id="selectAll" resultMap="xx.CityMap" resultOrdered="true">
只有設(shè)置這個屬性才能得到當(dāng)前對象 id 所對應(yīng)的所有嵌套結(jié)果。
對某一個嵌套查詢,設(shè)置 resultOrdered="true"
的結(jié)果:
不設(shè)置的結(jié)果:
以上圖為例,判斷是否為同一個結(jié)果下的對象,使用 id 判斷的,這個 id 必須是 <resultMap> 中的 <id>,另外為了結(jié)果完整,你還需要按照 <id> 配置的列進行排序,如果結(jié)果不是 <id> 對應(yīng)列的順序,嵌套的結(jié)果數(shù)量會出錯。
2. 增加對 Java 8 日期(JSR-310)的支持
添加以下依賴:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.0</version> </dependency>
如果你使用的 3.4.0 版本,就不需要任何配置就可以直接用。
如果你使用的老版本,需要手動配置:
<typeHandlers> <typeHandler handler="org.apache.ibatis.type.InstantTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalDateTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.OffsetDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.OffsetTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.ZonedDateTimeTypeHandler" /> </typeHandlers>
有關(guān) mybatis-typehandlers-jsr310 項目的詳細信息看這里
3. 新增 autoMappingUnknownColumnBehavior 參數(shù)
新增了一個 settings 配置的參數(shù) autoMappingUnknownColumnBehavior ,當(dāng)檢測出未知列(或未知屬性)時,如何處理,默認情況下沒有任何提示,這在測試的時候很不方便,不容易找到錯誤。
可選值:
- NONE : 不做任何處理 (默認值)
- WARNING : 警告日志形式的詳細信息
- FAILING : 映射失敗,拋出異常和詳細信息
配置時,在 <settings> 里面添加:
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
4. Sql Provider 注解方式支持多個參數(shù)
例如:
@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName") List<User> getUsersByName( @Param("name") String name, @Param("orderByColumn") String orderByColumn); // Multiple arguments
在寫 UserSqlBuilder 的時候,同樣需要使用注解來指定參數(shù)(或者按順序):
public String buildGetUsersByName( @Param("name") final String name @Param("orderByColumn") final String orderByColumn) { // Allow multiple arguments return new SQL(){{ SELECT("*"); FROM("users"); if (name != null) { WHERE("name like #{name} || '%'"); } ORDER_BY(orderByColumn); }}.toString(); }
解決的 BUG
支持實體類中的泛型類型
例如 Entity 基類:
public abstract class Entity<K extends Serializable> { private static final long serialVersionUID = -1L; protected K id; public K getId() { return id; } public void setId(K id) { this.id = id; }
其中一個子類:
public class User extends Entity<String>
在先前的版本中,MyBatis 無法獲取 id 的實際類型,導(dǎo)致找不到 TypeHandler 出錯。
這里只列舉部分重要的內(nèi)容,詳細內(nèi)容看官方說明
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
spring boot 自定義規(guī)則訪問獲取內(nèi)部或者外部靜態(tài)資源圖片的方法
這篇文章主要介紹了spring boot 自定義規(guī)則訪問獲取內(nèi)部或者外部靜態(tài)資源圖片的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01jasypt 集成SpringBoot 數(shù)據(jù)庫密碼加密操作
這篇文章主要介紹了jasypt 集成SpringBoot 數(shù)據(jù)庫密碼加密操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11SpringBoot詳解如何進行整合Druid數(shù)據(jù)源
Druid是阿里開發(fā)的一款開源的數(shù)據(jù)源,被很多人認為是Java語言中最好的數(shù)據(jù)庫連接池,本文主要介紹了SpringBoot整合Druid數(shù)據(jù)源的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Matplotlib可視化之自定義顏色繪制精美統(tǒng)計圖
matplotlib提供的所有繪圖都帶有默認樣式.雖然這可以進行快速繪圖,但有時可能需要自定義繪圖的顏色和樣式,以對繪制更加精美、符合審美要求的圖像.matplotlib的設(shè)計考慮到了此需求靈活性,很容易調(diào)整matplotlib圖形的樣式,需要的朋友可以參考下2021-06-06關(guān)于servlet向mysql添加數(shù)據(jù)時中文亂碼問題的解決
最近在工作中遇到一個小問題,出現(xiàn)了中文亂碼的問題,無奈只能想辦法解決,下面這篇文章主要給大家介紹了關(guān)于servlet向mysql添加數(shù)據(jù)時中文亂碼問題的解決方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08