網站首頁 編程語言 正文
一、Results的用法
- 用法一:
當數據庫字段名與實體類對應的屬性名不一致時,可以使用@Results映射來將其對應起來
。column為數據庫字段名,porperty為實體類屬性名,jdbcType為數據庫字段數據類型,id為是否為主鍵
@Select("select id, name, class_id from student”)
@Results({
//column為數據庫字段名,porperty為實體類屬性名,jdbcType為數據庫字段數據類型,id為是否為主鍵
@Result(column=“id”, property=“id”, jdbcType=JdbcType.INTEGER, id=true),
@Result(column=“name”, property=“name”, jdbcType=JdbcType.VARCHAR),
@Result(column=“class_id”, property=“classId”, jdbcType=JdbcType.INTEGER)
})
List selectAll();
- 用法二:當@results這段代碼需要在多個方法中用到時,
為了提高代碼復用性,可以為@results指定id,然后使用@resultMap注解來復用這段代碼(通過id匹配)
@Select("select id, name, class_id from student")
@Results(id="studentMap",value={
@Result(column=“id”, property=“id”, jdbcType=JdbcType.INTEGER, id=true),
@Result(column=“name”, property=“name”, jdbcType=JdbcType.VARCHAR),
@Result(column=“class_id”, property=“classId”, jdbcType=JdbcType.INTEGER)
})
List selectAll();
@Select("select id, name, class_id from student”
" where id = #{id}")
@resultMap("studentMap")
student getStudent (@param("id") long id)
- 用法三:
需要通過查詢到的字段去查詢另外一個方法,將查詢結果作為本次查詢的一個屬性
- @one表示一對一:如下面的屬性,studentMate類型為Student,查詢studentMate的方法查詢出一個
- @many表示一對多:students類型為List,查詢students的方法查詢出多個
----student的屬性
public class Student {
private Integer id;
private String name;
private String classId;
//查詢自己班的同學(一對一)
private List<Student> students;
//查詢自己的同桌(通過自己的id可以查詢出同桌的信息)(一對多)
private Student studentMate;
}
?
@Select("SELECT * " +
" FROM student " +
" WHERE id = #{id} ")
@Results(id=“studentMap”,
value={
@Result(column=“id”, property=“id”,id=true),
@Result(column=“{id = ID}”, property=“studentMate”, //將查詢到結果映射到java屬性studentMate
//這里要寫的是子查詢的路徑,而不是sql語句,同時,子查詢可以復用父查詢的colum參數
one=@One(select=“com.example.DemoDao.selectMetaById”))
})
//查詢出自己信息的同時查詢出同桌的信息
Student selectInfoAndMeta(@param("id") long id);
?
?-------------------------------------------------------------------------------------------------------------------
@Select("SELECT * " +
" FROM student " +
" WHERE id = #{id} ")
@Results(id=“studentMap”,
value={
@Result(column=“id”, property=“id”,id=true),
@Result(column=“{id = ID}”, property=“students”,//將查詢到結果映射到java屬性students
//這里要寫的是子查詢的路徑,而不是sql語句。@Many中常用的屬性只有selet,用于指定關聯查詢的方法
many=@many(select=“com.example.DemoDao.selectAllById”))
})
//查詢出自己信息的同時查詢出全班的信息
Student selectInfoAndAll(@param("id") long id);
傳遞多個參數時:@Result(column=“{id = ID,name = NAME}”, property=“***”)
----這個是重點----
等號右側的ID和NAME是查出來的數據,分別賦給selectAllById方法中的id和name參數,去查詢對應的信息(右邊賦值給左邊)
重點注意:@Results需要和@Select結合,不然單獨的@Results不能被識別
二、@ResultMap的用法
@ResultMap注解的使用,其實就是引用已定義好的@Results
@ResultMap:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ResultMap {
String[] value();
}
參考文章
參考文章
原文鏈接:https://blog.csdn.net/qq_43985303/article/details/130003886
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-05-23 手把手教你如何一眼分辨是C還是C++_C 語言
- 2023-07-25 使用EasyExcel實現Excel的導入導出
- 2022-09-18 centos+nginx+uwsgi部署django項目上線_python
- 2022-01-20 Syntax Error: TypeError: this.getOptions is not a
- 2022-03-26 使用C語言如何輸出逆序數_C 語言
- 2022-06-16 c語言單詞搜索的實現_C 語言
- 2022-04-23 elementui el-pagination 分頁組件封裝
- 2022-07-14 python利用線程生成不同尺寸的縮略圖實例詳解_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支