目錄
Mybatis中常用的註解
註解 | 目標 | 對應的XML標籤 |
@CacheNamespace | 類 | <cache> |
@CacheNamespaceRef | 類 | <cacheRef> |
@Results | 方法 | <resultMap> |
@Result | 方法 |
<result> <id> |
@One | 方法 | <association> |
@Many | 方法 | <collection> |
@Insert @Update @Delete |
方法 |
<insert> <update> <delete> |
@InsertProvider @UpdateProvider @DeleteProvider @SelectProvider |
方法 |
<insert> <update> <delete> <select> 允許創建動態SQL |
@Param | 參數 | N/A |
@Options | 方法 | 映射語句的屬性 |
@select | 方法 | <select> |
使用案例
(1)@Select 簡單查詢
@Select(" Select * from user ")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "name", property = "name"),
@Result(column = "tel", property = "tel"),
@Result(column = "birth", property = "birth"),
@Result(column = "address", property = "address")
})
List<User> queryAllUser();
(2)@Insert 簡單插入
@Insert(" insert into user(name,sex,age) values(#{name},#{sex},#{age} " )
int saveUser(User user);
(3)@Update 簡單更新
@Update("update user set name= #{name} ,sex = #{sex},age =#{age} where id = #{id}")
void updateUserById(User user);
(4)@Delete 簡單刪除
@Delete("delete from user where id =#{id} ")
void deleteById(Integer id);
(5)@One
@Select(" select * from user where id = #{id} ")
@Results(
value = {
@Result(column = "name",property = "name"),
@Result(column = "type",property = "type",
//one指示我們,查詢出來的結果只有一個。
one = @One(select="com.xxxx.UserMapper.findTypeById",
//及時加載
fetchType = FetchType.EAGER))
}
)
User findUserById(Integer id);
(6)@Many
@Select(" select * from dept")
@Results({
@Result(id = true, column = "did", property = "did"),
@Result(column = "name", property = "name"),
@Result(column = "address", property = "address"),
@Result(column = "id",property = "emps",
//many指示我們,查詢出來的結果有很多個
many = @Many(
//select = sql語句
select = "com.xxxx.EmpMapper.findAllEmpByDid",
//懶加載
fetchType = FetchType.LAZY))
})
List<Dept> findAllDept();
轉自:https://www.cnblogs.com/uiee/p/10778694.html
表示感謝!!!
@Results、@Result、@ResultMap註解的使用是爲了解決實體類名稱和數據庫名稱不一致的問題,使用方法參考如下博客: