一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳細介紹MyBatis 3.4.0版本的功能

詳細介紹MyBatis 3.4.0版本的功能

2020-11-17 11:58isea533 Java教程

這篇文章主要給大家介紹了關于MyBatis 3.4.0版本的功能,文中只列舉部分重要的內容,詳細內容看官方說明,需要的朋友可以參考借鑒,下面跟著小編一起來學習學習吧。

新增功能

1. Cursor 新增返回值類型為游標的方法

當查詢大量(上百萬)數據的時候,使用游標可以有效的減少內存使用,不需要一次性將所有數據得到,可以通過游標逐個或者分批(逐個獲取一批后)處理。

SqlSession 中新增的 3 個游標方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * 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 版本的游標方法目前有個 bug,因此不支持 @Select 注解方式,在將來的 3.4.1 版本中會解決這個問題。

使用示例:

?
1
2
3
<select id="selectAll" resultType="tk.mybatis.springboot.model.City">
 select * from city
</select>

xml 里面沒有任何改變,在獲取值的地方有變化,例如使用接口:

?
1
Cursor<City> selectAll();

或者使用命名接口方式:

?
1
Cursor<City> cityList = sqlSession.selectCursor("selectAll");

得到結果后,使用方法如下:

?
1
2
3
4
5
6
7
Iterator<City> iterator = cityList.iterator();
while(iterator.hasNext()){
 City c2 = iterator.next();
 Assert.assertNotNull(c2);
 Assert.assertNotNull(c2.getName());
 Assert.assertNotNull(c2.getState());
}

嵌套查詢的情況

當使用嵌套查詢時,還需要設置resultOrdered="true"屬性,使用方法如下:

?
1
<select id="selectAll" resultMap="xx.CityMap" resultOrdered="true">

只有設置這個屬性才能得到當前對象 id 所對應的所有嵌套結果。

對某一個嵌套查詢,設置 resultOrdered="true" 的結果:

詳細介紹MyBatis 3.4.0版本的功能

不設置的結果:

詳細介紹MyBatis 3.4.0版本的功能

以上圖為例,判斷是否為同一個結果下的對象,使用 id 判斷的,這個 id 必須是 <resultMap> 中的 <id>,另外為了結果完整,你還需要按照 <id> 配置的列進行排序,如果結果不是 <id> 對應列的順序,嵌套的結果數量會出錯。

2. 增加對 Java 8 日期(JSR-310)的支持

添加以下依賴:

?
1
2
3
4
5
<dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis-typehandlers-jsr310</artifactId>
 <version>1.0.0</version>
</dependency>

如果你使用的 3.4.0 版本,就不需要任何配置就可以直接用。

如果你使用的老版本,需要手動配置:

?
1
2
3
4
5
6
7
8
9
<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>

有關 mybatis-typehandlers-jsr310 項目的詳細信息看這里

3. 新增 autoMappingUnknownColumnBehavior 參數

新增了一個 settings 配置的參數 autoMappingUnknownColumnBehavior ,當檢測出未知列(或未知屬性)時,如何處理,默認情況下沒有任何提示,這在測試的時候很不方便,不容易找到錯誤。

可選值:

  • NONE : 不做任何處理 (默認值)
  • WARNING : 警告日志形式的詳細信息
  • FAILING : 映射失敗,拋出異常和詳細信息

配置時,在 <settings> 里面添加:

?
1
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>

4. Sql Provider 注解方式支持多個參數

例如:

?
1
2
3
4
@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
List<User> getUsersByName(
 @Param("name") String name,
 @Param("orderByColumn") String orderByColumn); // Multiple arguments

在寫 UserSqlBuilder 的時候,同樣需要使用注解來指定參數(或者按順序):

?
1
2
3
4
5
6
7
8
9
10
11
12
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 基類:

?
1
2
3
4
5
6
7
8
9
10
11
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;
 }

其中一個子類:

?
1
public class User extends Entity<String>

在先前的版本中,MyBatis 無法獲取 id 的實際類型,導致找不到 TypeHandler 出錯。

這里只列舉部分重要的內容,詳細內容看官方說明

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://blog.csdn.net/isea533/article/details/51533296

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 九九成人免费视频 | 高h禁伦奶水女 | 国产精品亚洲综合久久 | 欧美色在线| 男人和女人上床 | 免费av在线看 | 操女b | 肉文高h调教 | 国产精品污双胞胎在线观看 | ysl蜜桃色成人麻豆 youwu在线影院 | waswaswas免费| 天天色天天色天天色 | 久久精品热在线观看30 | x8x8在线永久免费观看 | 火影小南被爆羞羞网站 | 日本高清中文字幕视频在线 | 天堂精品高清1区2区3区 | 免费日本视频 | 好大好猛好深好爽视频 | 掰开逼操| 亚洲欧美一区二区三区在饯 | 日本无卡视频 | 亚洲第一色网 | 骚虎tv| 91天堂国产在线 在线播放 | 黄 色 成 年人在线 幻女free性俄罗斯第一次摘花 | 日本卡一卡2卡3卡4精品卡无人区 | 夫妻性生活影院 | 交换余生在线播放免费 | 精品久久香蕉国产线看观看亚洲 | 亚洲午夜久久久久久91 | 办公室里被迫高h | 小黄鸭YELLOWDUCK7596 | 欧美特欧美特级一片 | 日韩毛片在线影视 | 91热爆在线 | 污到湿的爽文免费阅读 | 国产精品理论片 | 99成人免费视频 | tube99大学生 | 2021国产精品露脸在线 |