日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

mybatis中@Results,@ResultMap注解使用

作者:Jothan Zhong 更新時間: 2024-01-15 編程語言

一、Results的用法

  1. 用法一: 當數據庫字段名與實體類對應的屬性名不一致時,可以使用@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();
  1. 用法二:當@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)
  1. 用法三:需要通過查詢到的字段去查詢另外一個方法,將查詢結果作為本次查詢的一個屬性
  • @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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新