java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Mybatis从3.4.0版本到3.5.7

Mybatis从3.4.0版本到3.5.7版本的迭代方法实现

作者:乐只乐之

本文主要介绍了Mybatis从3.4.0版本到3.5.7版本的迭代方法实现,包括主要的功能增强、不兼容的更改和修复的错误,具有一定的参考价值,感兴趣的可以了解一下

一、3.4.0

1、主要的功能增强

<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、不兼容的更改

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、主要的功能增强

使用Java 8 -parameters选项编译时,允许通过其声明的名称引用参数,处理代码详见ParamNameResolver。
~ 可以在mapper类上省略@Param注解指定参数名(前提xml中的名称要与参数名一致)
~ 默认开启

根据JSR310 对日期和时间新的API定义,增加对month和year的类型处理器,详见MonthTypeHandler和YearTypeHandler。

@Select 返回的数据类型支持对象数组,详见MapperAnnotationBuilder#getReturnType

 @Select("select * from users")
 User[] getUsers();

logImpl配置参数支持自定义xml配置,XMLConfigBuilder。

2、修复的错误

<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);
Cursor<User> usersCursor = sqlSession.selectCursor(
				"getAllUsers", null, new RowBounds(1, 3));

三、3.4.2

1、主要的功能增强

Property[] properties() default {};

2、修复的错误

@Options(useGeneratedKeys = true, keyProperty = "id")

四、3.4.3

1、主要的功能增强

new SQL()
.UPDATE("table_1 a")
.INNER_JOIN("table_2 b USING (ID)")
.SET("a.value = b.value");

2、修复的错误

public interface a extends super_a
public interface super_a extends super_super_a
@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();
	}
}

修复resultMap和as别名之间不应该出现的自动映射。
https://github.com/mybatis/mybatis-3/pull/1100~ 对于 ≤ 3.4.2,即使在结果映射中明确定义了字段映射,就算SQL语句中字段与映射中定义的字段不匹配,也会对该字段进行自动映射。
~ 对于 ≥ 3.4.3,自动映射不适用于在结果映射中显式映射的属性。

以下代码中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、主要的功能增强

六、3.4.5

1、主要的功能增强

<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、修复的错误

<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>
@Insert("INSERT INTO test(number, size) VALUES(#{number}, #{size})")
public void insert(@Param("number") long number, @Param("size") long size);
<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、主要的功能增强

@SelectProvider(type=SqlGenerator.class, method="selectIds")
Integer[] selectIds();

class SqlGenerator{
	public static String selectIds(){
			return "select id from login_user";
	}
}
@SelectProvider(type=SqlGenerator.class, method="selectIds")
Integer[] selectIds();

class SqlGenerator{
	public static CharSequence selectIds(){
			return "select id from login_user";
	}
}
<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、修复的错误

八、3.5.0

1、主要的功能增强

public interface UserMapper{

	@Select("select * from users where id = #{id}")
	Optional<User> findOne(Integer id);
}
<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>
-- 版本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>
@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>
<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>

2、修复的错误

3、不向后兼容的更改

九、3.5.1

1、主要的功能增强

@SelectProvider(type=SqlGenerator.class)
Integer[] selectIds();

class SqlGenerator{
	public static CharSequence selectIds(){
			return "select id from login_user";
	}
}
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、修复的错误

@Options(useGeneratedKeys = true, keyProperty="n.id" keyColumn="id")
int insert(@Param("n") User user);

3、不向后兼容的更改

LocalDateTypeHandler、LocalTimeTypeHandler和LocalDateTimeTypeHandler只有在支持JDBC 4.2 API的JDBC驱动程序才有效。

十、3.5.2

1、主要的功能增强

public void insert(){
	final String sql = new SQL(){{
		.INSERT_INTO("user")
		.INTO_VALUES("id", "name")
		.ADD_ROW()
		.INTO_VALUES("#{id}", "#{name}")
	}}.toString();
}

2、修复的错误

修复DefaultResultSetHandler可能存在的npe。

十一、3.5.3

1、主要的功能增强

<sql id="colsSuffix">
	<![CDATA[
		col_${suffix}
	]]>
</sql>

2、修复的错误

修复在迭代Cursor时,如果下一个元素为空出错的情况。

十二、3.5.4

1、主要的功能增强

支持在同一个方法上多次使用@Arg和@Result注解。

@Result(property="id", colum="id")
@Result(property="name", colum="name")
@Select("select * from user where id = #{id}")
User selectOne(int id);

2、修复的错误

十三、3.5.5

1、主要的功能增强

支持在@One和@Many注解中获取resultMap和columnPrefix所需要的数据。

2、修复的错误

十四、3.5.6

1、主要的功能增强

2、修复的错误

十五、3.5.7

1、主要的功能增强

改进jdk 8环境下的性能。

到此这篇关于Mybatis从3.4.0版本到3.5.7版本的迭代方法实现的文章就介绍到这了,更多相关Mybatis从3.4.0版本到3.5.7内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文