# 注解中的结果集映射

注意

由于注解在映射结果集上没有实现 <resultMap> 的 100% 功能,因此,十全十美的方案是在 mapper.xml 配置文件中通过 <resultMap> 映射结果集,然后再在接口中引用它。

因此,一下内容仅作了解。

# 1. 结果集映射

我们在使用 MyBatis 不可能都是遇到最简单的情况:表的列名与类的属性名一致。当表的列明与类的属性名不一致时,需要去配置结果集映射。

通过注解进行结果集的映射是通过使用 @Results@Result@ResultMap 注解完成的。其中,

  • @Results@Result 结合使用进行结果集映射;

  • @ResultMap 则是在别处『调用』映射规则。

  • @Results@Result 只需要配置一次,而 @ResultMap 会在多出使用。

例如:

@Select("select * from dept where deptno=#{id}")
@Results(id = "department", value = {
        @Result(property = "id", column = "deptno"),
        @Result(property = "name", column = "name"),
        @Result(property = "location", column = "loc")
})
public Department selectByPK(int id);

@Select("select * from dept")
@ResultMap("department")
public List<Department> select();

# 2. 关系映射

一对一、一对多和多对多的关系映射就是在结果集映射的基础上再使用 @One@Many 注解。

@Select("select * from emp where empno=#{id}")
@Results(id = "employee", value = {
        @Result(property = "empno", column = "empno"),
        @Result(property = "ename", column = "ename"),
        @Result(property = "job", column = "job"),
        @Result(property = "mgr", column = "mgr"),
        @Result(property = "hiredate", column = "hiredate"),
        @Result(property = "sal", column = "sal"),
        @Result(property = "comm", column = "comm"),
        @Result(property = "dept", column = "deptno", one = @One(select = "dao.DepartmentMapper.selectByPK"))
})
public Employee selectByPK(int id);

@Select("select * from emp where deptno = #{id}")
@ResultMap("employee")
public List<Employee> selectByEmployeeID(int deptno);
@Select("select * from dept where deptno=#{id}")
@Results(id = "department", value = {
        @Result(property = "id", column = "deptno"),
        @Result(property = "name", column = "deptno"),
        @Result(property = "location", column = "loc"),
        @Result(property = "employeeList", column = "deptno", many = @Many(select = "dao.EmployeeMapper.selectByDepartmentID"))
})
public Department selectByPK(int id);

# 3. 常用功能注解汇总

注解 目标 相对应的 XML 描述
@Param 参数 N/A 如果你的映射器的方法需要多个参数,这个注解可以被应用于映射器的方法参数来给每个参数一个名字。
否则,多参数将会以它们的顺序位置来被命名(不包括任何 RowBounds 参数)比如。#{param1} , #{param2} 等,这是默认的。
使用 @Param("person"),参数应该被命名为 #{person}
@Insert

@Update

@Delete

@Select
方法 <insert>

<update>

<delete>

<select>
这些注解中的每一个代表了执行的真实 SQL。它们每一个都使用字符串数组(或单独的字符串)

如果传递的是字符串数组,它们由每个分隔它们的单独空间串联起来。
@Results 方法 <resultMap> 结果映射的列表,包含了一个特别结果列如何被映射到属性或字段的详情。属性有 valueid
value 属性是 Result 注解的数组。
id 的属性是结果映射的名称。
@Result N/A <result>
<id>
在列和属性或字段之间的单独结果映射。属性有 id,column,property,javaType,jdbcType,typeHandler,one,many。
id 属性是一个布尔值,表示了应该被用于比较(和在 XML 映射中的 <id> 相似)的属性。
one 属性是单独的联系,和 <association> 相似 , 而 many 属性是对集合而言的 , 和 <collection> 相似。
@ResultMap 方法 N/A 这个注解给 @Select 或者**@SelectProvider** 提供在 XML 映射中的 <resultMap> 的id。
这使得注解的 select 可以复用那些定义在 XML 中的 ResultMap。
如果同一 select 注解中还存在 @Results 或者 @ConstructorArgs ,那么这两个注解将被此注解覆盖。
@One N/A <association> 复杂类型的单独属性值映射。属性有 select,已映射语句(也就是映射器方法)的完全限定名,它可以加载合适类型的实例。
注意:联合映射在注解 API 中是不支持的。这是因为 Java 注解的限制,不允许循环引用。
fetchType 会覆盖全局的配置参数 lazyLoadingEnabled
@Many N/A <collection> 映射到复杂类型的集合属性。属性有 select,已映射语句(也就是映射器方法)的全限定名,它可以加载合适类型的实例的集合,fetchType 会覆盖全局的配置参数 lazyLoadingEnabled 。 注意联合映射在注解 API 中是不支持的。这是因为 Java 注解的限制,不允许循环引用。
@InsertProvider

@UpdateProvider

@DeleteProvider

@SelectProvider
方法 <insert>

<update>

<delete>

<select>
这些可选的 SQL 注解允许你指定一个类名和一个方法在执行时来返回运行允许创建动态的 SQL。基于执行的映射语句,MyBatis 会实例化这个类,然后执行由 provider 指定的方法。
You can pass objects that passed to arguments of a mapper method, "Mapper interface type" and "Mapper method" via theProviderContext(available since MyBatis 3.4.5 or later) as method argument. (In MyBatis 3.4 or later, it's allow multiple parameters)
属性有 typemethodtype 属性是类。method 属性是方法名。