Mybatis從3.4.0版本到3.5.7版本的迭代方法實現(xiàn)
一、3.4.0
1、主要的功能增強(qiáng)
- 在SqlSession中新增Cursor List方法。
- BatchExecutor和ReuseExecutor操作支持事務(wù)超時處理,主要是修復(fù)通過spring注解@Transactional指定超時未使用到sql的情況
- 更好的支持泛型
- 支持Java8新的Date和Time API(JSR-310)
<T> Cursor<T> selectcursor(String statement); <T> Cursor<T> selectcursor(string statement, object parameter); <T> Cursor<T> selectcursor(string statement, Object parameter, RowBounds rowBounds);
2、selectCursor example
<select id="getEmployeeNestedCursor"resultMap="results" resultOrdered="true"> select * from employees order by id </select>
// Get a list which can have millions of rows Cursor<Employee> employees=sq1Session.selectCursor("getEmployeeNestedCursor"); // Get an iterator on employees Iterator<Employee> iter = employees.iterator(); List<Employee> smallChunk = new ArrayList<>(10); while(iter.hasNext()){ // Fetch next 10 employees for(int i = 0; i<10 && iter.hasNext(); i++){ smallChunk.add(iter.next()); } doSomethingWithAlreadyFetchedEmployees(smallChunk); smallChunk.clear(); }
3、不兼容的更改
- Transaction接口新增getTimeout()方法,如有實現(xiàn)該接口需特別注意。
- 注解@options()參數(shù)flushCache改用枚舉。
- 因為增加對事務(wù)超時的支持,所以StatementHandler#prepare(Connection)更改為StatementHander#prepare(Connection,Integer),后一個參數(shù)取事務(wù)超時時間。
- 添加了SqlSession#selectCursor、Executor#queryCursor、ResultSetHander#handleCursorResultsSets和StatementHandler#queryCursor接口,如有實現(xiàn)需特別注意。
public enum FlushCachePolicy { /** <code>false</code> for select statement; * <code>true</code> for insert/update/delete statement. * */ DEFAULT, /** Flushes cache regardless of the statement type. */ TRUE, /** Does not flush cache regardless of the statement type. */ FALSE }
二、3.4.1
1、主要的功能增強(qiáng)
使用Java 8 -parameters選項編譯時,允許通過其聲明的名稱引用參數(shù),處理代碼詳見ParamNameResolver。
~ 可以在mapper類上省略@Param注解指定參數(shù)名(前提xml中的名稱要與參數(shù)名一致)
~ 默認(rèn)開啟
根據(jù)JSR310 對日期和時間新的API定義,增加對month和year的類型處理器,詳見MonthTypeHandler和YearTypeHandler。
@Select 返回的數(shù)據(jù)類型支持對象數(shù)組,詳見MapperAnnotationBuilder#getReturnType
@Select("select * from users") User[] getUsers();
logImpl配置參數(shù)支持自定義xml配置,XMLConfigBuilder。
2、修復(fù)的錯誤
- 修復(fù)當(dāng)在xml映射文件中指定了columnPrefix時,循環(huán)引用的resultMap也會填充祖父對象對應(yīng)的值。
<resultMap id="user" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <association property="superior" resultMap="user" columnPrefix="superior_"/> </resultMap> <select id="find" parameterType="long" resultMap="user"> select 1 as id, 'a' as name, 2 as superior_id, 'b' as superior_name from dual </select>
User user = dao.find(1L);
- 修復(fù)在 IBM WebSphere Application Server 8.5.5.9 上啟動時拋出RuntimeException。
- 修復(fù)Cursor 無法作為@Select語句的返回類型
PS:3.4.0增加了selectCursor游標(biāo)處理查詢結(jié)果的方法,但是無法支持@Select - 修復(fù)與 Kylin JDBC 驅(qū)動程序一起使用時拋出 NullPointerException。
- 修復(fù)RowBounds無法作為返回類型為 Cursor 的 select 語句的參數(shù)。
Cursor<User> usersCursor = sqlSession.selectCursor( "getAllUsers", null, new RowBounds(1, 3));
- Select 語句@Param不能用作關(guān)聯(lián)的嵌套選擇語句。
三、3.4.2
1、主要的功能增強(qiáng)
- 增加returnInstanceForEmptyRow配置,以控制當(dāng)查詢結(jié)果中所有列都為null時返回空對象,而不是null。
- 支持mapper接口中的默認(rèn)方法。
- TypeHandler支持遞歸搜索。
- @CacheNamespace注解增加properties屬性。
Property[] properties() default {};
- 允許在占位符上指定默認(rèn)值。
PS:默認(rèn)情況下該功能禁用,可使用org.apache.ibatis.parsing.PropertyParser.enable-default-value=true配置來開啟。 - 新增JSR310 1.0.2定義的類型處理器。
2、修復(fù)的錯誤
- aggressiveLazyLoading 的默認(rèn)值更改為false。
- 使用keyProperty指定的字段找不到時拋出異常。
@Options(useGeneratedKeys = true, keyProperty = "id")
四、3.4.3
1、主要的功能增強(qiáng)
- 增加為枚舉的公共接口注冊類型處理程序。
https://github.com/mybatis/mybatis-3/pull/947 - 在MapperAnnotationBuilder中Jdbc3KeyGenerator 和 NoKeyGenerator 分別共享自己的同一個實例,詳見MapperAnnotationBuilder#parseStatement。
- 可以通過SQL Builder構(gòu)建update … join 語句。
~ 以下代碼3.4.3以前不生效
new SQL() .UPDATE("table_1 a") .INNER_JOIN("table_2 b USING (ID)") .SET("a.value = b.value");
2、修復(fù)的錯誤
- 修復(fù)找不到祖父級mapper接口上的方法。
~ 如下代碼中,super_super_a接口中的方法在3.4.3以前的版本中是無法被遍歷到的
public interface a extends super_a public interface super_a extends super_super_a
- 修復(fù)當(dāng)LanguageDriver為null時,將實例對象放在“LANGUAGE_DRIVER_MAP”中,而不是“驅(qū)動程序”對象中的錯誤,詳見LanguageDriverRegistry。
- 修復(fù)布爾值不能同時映射到get和is。
- 解決解析嵌套結(jié)果過多內(nèi)存分配的問題。
- 修復(fù)接口默認(rèn)方法只支持public的情況。
~ 以下代碼3.4.3以前是不能支持的,因為Mapper是包級別的訪問權(quán)限
@Mapper interface Mapper extends SupperMapper{ @Select("select * from login_user") Cursor<Map<String, Object>> select(); @Select("select id from login_user") Integer[] selectIds(); default List<Map<String, Object>> defaultGetUser(){ return selectList(); } }
修復(fù)resultMap和as別名之間不應(yīng)該出現(xiàn)的自動映射。
~ https://github.com/mybatis/mybatis-3/pull/1100~ 對于 ≤ 3.4.2,即使在結(jié)果映射中明確定義了字段映射,就算SQL語句中字段與映射中定義的字段不匹配,也會對該字段進(jìn)行自動映射。
~ 對于 ≥ 3.4.3,自動映射不適用于在結(jié)果映射中顯式映射的屬性。
以下代碼中phone在版本3.4.3以后中返回null
<resultMap id="user" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="phone" column="phone_number"/> </resultMap> <select id="getUser" resultMap="user"> select id, name, phone_number as phoneNumber from user where id = 1 </select>
五、3.4.4
1、主要的功能增強(qiáng)
- 無功能更新,僅僅修復(fù)了3.4.3版本中Meven Central 上的 JAR 中的一個錯誤。
六、3.4.5
1、主要的功能增強(qiáng)
- 允許為enum自定義默認(rèn)的TypeHandler,詳見EnumOrdinalTypeHandler。
- sql provider 方法上支持 ProviderContext用于構(gòu)建通用mapper。
- 將 JSR-310(Java 日期和時間 API)的類型處理程序合并到master。
- 允許在sql provider中替換ProviderSqlSource 上的屬性配置。
<properties> <property name="Constants.LOGICAL_DELETE_OFF" value="false"/> </properties>
public String buildSql(@Param("name") final String name){ return new SQL(){{ SELECT("*"); FROM("test"); if(name != null){ WHERE("name = #{name}"); } WHERE("logical_delete = #{Constants.LOGICAL_DELETE_OFF}"); }}.toString(); }
2、修復(fù)的錯誤
- 修復(fù)在兩個select之間執(zhí)行insert或者update時,并非所有的select結(jié)果都得到處理。
<select id="multi" resultMap="userResult,groupsResult"> select * from mbtest.order_detail; insert into mbtest.order_detail (order_id, line_number) values (1, 132); select * from mbtest.order_header; </select>
- 延遲加載不能覆蓋用戶設(shè)置的屬性值。
~ https://github.com/mybatis/mybatis-3/issues/988 - 修復(fù)當(dāng)使用size為屬性名時用integer映射的錯誤。
以下代碼在版本3.4.5之前無法正常運行
@Insert("INSERT INTO test(number, size) VALUES(#{number}, #{size})") public void insert(@Param("number") long number, @Param("size") long size);
- 修復(fù)PostgreSQL中useGeneratedKeys=true,當(dāng) keyProperty 指定的‘id’沒有setter則拋出 ExecutorException。
- 修復(fù)使用foreach標(biāo)簽污染全局上下文。
以下代碼其中item=status,會污染 and status =#{status}中的status
<select> select * from tb <where> <if test="status != null"> and status = #{status} </if> <if test="statusList != null"> and status in <foreach collection="statusList" item="status", open="(" separator="," close=")"> #{status} </foreach> </if> </where> </select>
七、3.4.6
1、主要的功能增強(qiáng)
- 將自定義 ResultHandler應(yīng)用于cursor類型的返回。
~ https://github.com/mybatis/mybatis-3/issues/493 - BatchExecutor在遍歷執(zhí)行sql語句之后立即關(guān)閉其語句。
- 當(dāng)解析xml文件失敗時,將資源路徑添加到異常消息中。
- @SelectProvider注解支持靜態(tài)方法,詳見ProviderSqlSource。
~ 如下代碼
@SelectProvider(type=SqlGenerator.class, method="selectIds") Integer[] selectIds(); class SqlGenerator{ public static String selectIds(){ return "select id from login_user"; } }
- ProviderSqlSource構(gòu)造sql語句時支持返回CharSequence。
@SelectProvider(type=SqlGenerator.class, method="selectIds") Integer[] selectIds(); class SqlGenerator{ public static CharSequence selectIds(){ return "select id from login_user"; } }
- 可以替換include中的占位符。
~ 以下代碼在版本3.4.6以前無效,不能填充include標(biāo)識的sql
<select id="selectList" resultType="java.util.Map" resultOrdered="true"> select <include refid="colsIfAttribute"> <property name="value" value="y"/> </include> from login_user </select> <sql id="colsIfAttribute"> <if test="'${value}' == 'x'"> * </if> <if test="'${value}' == 'y'"> id </if> </sql>
2、修復(fù)的錯誤
- 修復(fù)自定義HashMap類型處理器發(fā)生ClassCastException異常的情況。
~ https://github.com/mybatis/mybatis-3/commit/9233556404a3b6cc122d42c8ce0c97ba9e01b7ce - 修復(fù)序列化和反序列化緩存的數(shù)據(jù)導(dǎo)致NullPointerException的錯誤。
- 修復(fù)調(diào)用TypeHandlerRegistry.hasTypeHandler 后無法注冊 TypeHandler的問題,詳見TypeHandlerRegistry。
八、3.5.0
1、主要的功能增強(qiáng)
- 避免在 JDK 9+ 上出現(xiàn)“非法反射訪問”警告。
- 添加自動模塊名稱。
- 支持java.util.Optional作為執(zhí)行器返回值。
public interface UserMapper{ @Select("select * from users where id = #{id}") Optional<User> findOne(Integer id); }
- 將BaseTypeHandler中的wasNull判斷移到子類去做。
~ https://github.com/mybatis/mybatis-3/pull/1244 - xml中constructor標(biāo)簽支持columnPrefix。
<resultMap id="authorRM" type="Author"> <id property="id" column="id"/> <result property="name" column="name"/> </resultMap> <resultMap id="articleRM" type="Article"> <constructor> <idArg column="id" javaType="int"/> <arg column="name" javaType="String"/> <arg resultMap="authorRM" columnPrefix="author_" javaType="Author"/> <arg resultMap="authorRM" columnPrefix="coauthor_" javaType="Author"/> </constructor> </resultMap>
- 提高尋找TypeHandler自動映射結(jié)果的性能。
- 當(dāng)沒有找到keyProperty指定的key時拋出異常。
- 新增SqlxmlTypeHandler類型處理器,用于將sqlxml轉(zhuǎn)換為string,詳見SqlxmlTypeHandler。
- 標(biāo)簽可以直接將逗號跟在sql語句后面。
-- 版本3.5.0以前的寫法 -- <set> <if test="name != null"> name = #{name} </if> <if test="desc != null"> ,desc = #{desc} </if> </set> -- 版本3.5.0以后的寫法 -- <set> <if test="name != null"> name = #{name}, </if> <if test="desc != null"> desc = #{desc} </if> </set>
- 運行OGNL訪問表達(dá)式上私有、包私有和受保護(hù)的屬性。
@Getter public class Point{ private int lid; private int x; private int y; }
<insert id="save"> insert into points (lid, x, y) values <foreach item="point" separator=","> (#{point.lid}, #{point.x}, #{point.y}) </foreach> </insert>
- mapper xml中使用標(biāo)簽如果不指定resultType,自動從resultMap標(biāo)簽的type中獲取。
<resultMap type="Owner" id="OwnerMap"> <id property="id" column="id"/> <result property="name" column="name"/> <discriminator javaType="String" column="vehicle_id"> <case value="car"> <association property="vehicle" column="vehicle_id" select="CarMapper.get"/> </case> </discriminator> </resultMap>
- 支持 Log4J 2.6+。
- 將測試框架升級到 JUnit 5。
- 改進(jìn)了與僅支持 JDBC 3 API 的驅(qū)動程序的兼容性。
- 同時使用@CacheNamespace和不會再拋出異常。
2、修復(fù)的錯誤
- 修復(fù)OffsetDateTimeTypeHandler、OffsetTimeTypeHandler 和 ZonedDateTimeTypeHandler 丟失時區(qū)的問題。
- 修復(fù)
Cursor
與 Db2 一起使用時發(fā)生的 SQLException。 - 修復(fù)當(dāng)類層次結(jié)構(gòu)超過 3 個級別時,無法正確解析通用類型參數(shù)。
3、不向后兼容的更改
- 在3.5.0中使用Cursor需要 JDBC 4.1 API 的驅(qū)動程序。
- 如果使用
org.apache.ibatis.type.BaseTypeHandler
自定義了類型處理器,需要檢查wasNull()。 - 更正 JdbcTransaction.java 中的錯字(autoCommmit -> autoCommit)。
- keyProperty不再有默認(rèn)值。
九、3.5.1
1、主要的功能增強(qiáng)
- Add LanguageDriver to ProviderSqlSource。
~ https://github.com/mybatis/mybatis-3/pull/1391 - LONGVARCHAR的默認(rèn)類型處理器從ClobTypeHandler更改為StringTypeHandler。
- 如果在使用@SelectProvider時,方法名和對應(yīng)的provider方法名一樣可以省略@SelectProvider中method參數(shù)。
@SelectProvider(type=SqlGenerator.class) Integer[] selectIds(); class SqlGenerator{ public static CharSequence selectIds(){ return "select id from login_user"; } }
- ProviderContext構(gòu)造sql語句時支持mybatis-config.xml配置的databaseId屬性值。
interface DatabaseIdMapper{ @SelectProvider(type=SqlGenerator.class) String selectDatabaseId(); } class SqlProvider{ public static String providerSql(ProviderContext context){ if("hsql".equals(context.getDatabaseId())){ return "select " + context.getDatabaseId() + " from user"; } else{ return "select " + context.getDatabaseId() + " from user"; } } }
2、修復(fù)的錯誤
- 修復(fù)LocalTimeTypeHandler丟失小數(shù)秒部分。
- 修復(fù)LocalDateTypeHandler和LocalDateTimeTypeHandler處理日期的一些異常。
- 修復(fù)keyProperty用參數(shù)名稱指定在批量插入的時候報錯。
~ keyProperty = “n.id”在批量插入中會報錯。
@Options(useGeneratedKeys = true, keyProperty="n.id" keyColumn="id") int insert(@Param("n") User user);
- 修復(fù)當(dāng)枚舉值中有實現(xiàn)方法時找不到類型處理器的錯誤。
3、不向后兼容的更改
LocalDateTypeHandler、LocalTimeTypeHandler和LocalDateTimeTypeHandler只有在支持JDBC 4.2 API的JDBC驅(qū)動程序才有效。
十、3.5.2
1、主要的功能增強(qiáng)
- sql builder支持LIMIT、OFFSET和FETCH FIRST。
- 在Provider注解上增加value 屬性,如InsertProvider。
- 支持?jǐn)?shù)組對象作為參數(shù)入?yún)ⅰ?br />~ https://github.com/mybatis/mybatis-3/pull/1548
- sql builder支持多行插入語法。
public void insert(){ final String sql = new SQL(){{ .INSERT_INTO("user") .INTO_VALUES("id", "name") .ADD_ROW() .INTO_VALUES("#{id}", "#{name}") }}.toString(); }
- 當(dāng)只有單個參數(shù)條件的時候,允許任意數(shù)據(jù)類型。
2、修復(fù)的錯誤
修復(fù)DefaultResultSetHandler可能存在的npe。
十一、3.5.3
1、主要的功能增強(qiáng)
- 更新了默認(rèn)方法調(diào)用以支持 JDK 14+8。
- 在Provider注解上增加value 屬性,如InsertProvider。
- getter/setter僅在真正訪問到時才會拋出ReflectionException。
- 在標(biāo)簽中支持CDATA變量。
<sql id="colsSuffix"> <![CDATA[ col_${suffix} ]]> </sql>
2、修復(fù)的錯誤
修復(fù)在迭代Cursor時,如果下一個元素為空出錯的情況。
十二、3.5.4
1、主要的功能增強(qiáng)
支持在同一個方法上多次使用@Arg和@Result注解。
@Result(property="id", colum="id") @Result(property="name", colum="name") @Select("select * from user where id = #{id}") User selectOne(int id);
2、修復(fù)的錯誤
- 修復(fù)自動生成id時,如果重寫了hashCode方法,不再調(diào)用對應(yīng)hashCode方法,否則拋出npe。
- 修復(fù)TypeParameterResolver 中的 TypeVariable 解析可能不正確。
~ https://github.com/mybatis/mybatis-3/issues/1794 - 解決TypeHandlerRegistry 中的競爭條件。
~ https://github.com/mybatis/mybatis-3/issues/1819
十三、3.5.5
1、主要的功能增強(qiáng)
支持在@One和@Many注解中獲取resultMap和columnPrefix所需要的數(shù)據(jù)。
2、修復(fù)的錯誤
- 修復(fù)@CacheNamespaceRef可能出現(xiàn)的IllegalArgumentException。
十四、3.5.6
1、主要的功能增強(qiáng)
- 增加SQL_SERVER_SNAPSHOT事務(wù)隔離級別以支持MS SQL Server。
- 當(dāng)沒有定義JEP-290 序列化過濾器時,將在反序列化對象流上記錄一條 WARN 級別的消息。
~https://docs.oracle.com/pls/topic/lookup?ctx=javase15&id=GUID-8296D8E8-2B93-4B9A-856E-0A65AF9B8C66~ https://github.com/mybatis/mybatis-3/pull/2079 - 指定defaultSqlProviderType以支持全局配置在sql provider的一些注解上@SelectProvider、@UpdateProvider,@InsertProvider和@DeleteProvider。
2、修復(fù)的錯誤
- 修復(fù)使用BlockingCache產(chǎn)生OutOfMemoryError的風(fēng)險。
~ https://github.com/mybatis/mybatis-3/pull/2044 - 修復(fù)使用包作為子標(biāo)簽解析 typeAliases 元素時拋出 InvalidPathException。
~ https://github.com/mybatis/mybatis-3/issues/1974
十五、3.5.7
1、主要的功能增強(qiáng)
改進(jìn)jdk 8環(huán)境下的性能。
到此這篇關(guān)于Mybatis從3.4.0版本到3.5.7版本的迭代方法實現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis從3.4.0版本到3.5.7內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring遠(yuǎn)程加載配置的實現(xiàn)方法詳解
這篇文章主要介紹了Spring遠(yuǎn)程加載配置的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03SpringMVC + servlet3.0 文件上傳的配置和實現(xiàn)代碼
本篇文章主要介紹了SpringMVC + servlet3.0 文件上傳的配置和實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04Netty結(jié)合Protobuf進(jìn)行編解碼的方法
這篇文章主要介紹了Netty結(jié)合Protobuf進(jìn)行編解碼,通過文檔表述和代碼實例充分說明了如何進(jìn)行使用和操作,需要的朋友可以參考下2021-06-06Java并發(fā)編程包中atomic的實現(xiàn)原理示例詳解
這篇文章主要給大家介紹了關(guān)于Java并發(fā)編程包中atomic的實現(xiàn)原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09