文章目錄
- 注解實現結果集映射
- 注解實現關系映射
- 常用功能注解匯總
注解實現結果集映射
注意
配置結果集映射,只用看 SQL 執行結果,不看 SQL 語句!
注意
由于注解在映射結果集上沒有實現 <resultMap> 的 100% 功能,因此,十全十美的方案是在 mapper.xml 配置文件中通過 <resultMap> 映射結果集,然后再在接口中引用它。因此,一下內容僅作了解。
我們在使用 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();
注解實現關系映射
注意
配置結果集映射,只用看 SQL 執行結果,不看 SQL 語句!
一對一、一對多和多對多的關系映射就是在結果集映射的基礎上再使用 @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);
常用功能注解匯總
注解 | 目標 | 相對應的 XML | 描述 |
---|---|---|---|
@Param | 參數 | N/A | 如果你的映射器的方法需要多個參數,這個注解可以被應用于映射器的方法參數來給每個參數一個名字。 |
否則,多參數將會以它們的順序位置來被命名(不包括任何 RowBounds 參數)比如。#{param1} , #{param2} 等,這是默認的。 | |||
使用 @Param(“person”),參數應該被命名為 #{person} 。 | |||
@Insert | 方法 | <insert> | 這些注解中的每一個代表了執行的真實 SQL。它們每一個都使用字符串數組(或單獨的字符串)。 |
@Update | <update> | 如果傳遞的是字符串數組,它們由每個分隔它們的單獨空間串聯起來。 | |
@Delete | <delete> | ||
@Select | <select> | ||
@Results | 方法 | <resultMap> | 結果映射的列表,包含了一個特別結果列如何被映射到屬性或字段的詳情。屬性有 value,id 。 value 屬性是 Result 注解的數組。 id 的屬性是結果映射的名稱。 |
@Result | N/A | <result> | 在列和屬性或字段之間的單獨結果映射。屬性有 id,column,property,javaType,jdbcType,typeHandler,one,many。 |
<id> | 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 | 方法 | <insert> | 這些可選的 SQL 注解允許你指定一個類名和一個方法在執行時來返回運行允許創建動態的 SQL。基于執行的映射語句,MyBatis 會實例化這個類,然后執行由 provider 指定的方法。 |
@UpdateProvider | <update> | 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) | |
@DeleteProvider | <delete> | 屬性有 type ,method 。type 屬性是類。method 屬性是方法名。 | |
@SelectProvider | <select> |