@Results用法和resultMap的用法
原文鏈接:https://blog.csdn.net/cherlshall/article/details/80950150
原文鏈接:https://blog.csdn.net/qq_42780864/article/details/81429114
MyBatis中使用@Results注解來映射查詢結果集到實體類屬性。
(1)@Results的基本用法。當數據庫字段名與實體類對應的屬性名不一致時,可以使用@Results映射來將其對應起來。column為數據庫字段名,porperty為實體類屬性名,jdbcType為數據庫字段數據類型,id為是否為主鍵。
@Select({"select id, name, class_id from my_student"})
@Results({
@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<Student> selectAll();
如上所示的數據庫字段名class_id與實體類屬性名classId,就通過這種方式建立了映射關系。名字相同的可以省略。
(2)@ResultMap的用法。當這段@Results代碼需要在多個方法用到時,為了提高代碼復用性,我們可以為這個@Results注解設置id,然后使用@ResultMap注解來復用這段代碼。
@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER)
})
List<Student> selectAll();
@Select({"select id, name, class_id from my_student where id = #{id}"})
@ResultMap(value="studentMap")
Student selectById(integer id);
(3)@One的用法。當我們需要通過查詢到的一個字段值作為參數,去執行另外一個方法來查詢關聯的內容,而且兩者是一對一關系時,可以使用@One注解來便捷的實現。比如當我們需要查詢學生信息以及其所屬班級信息時,需要以查詢到的class_id為參數,來執行ClassesMapper中的selectById方法,從而獲得學生所屬的班級信息。可以使用如下代碼。
@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="class_id", property="myClass", javaType=MyClass.class,
one=@One(select="com.example.demo.mapper.MyClassMapper.selectById"))
})
List<Student> selectAllAndClassMsg();
(4)@Many的用法。與@One類似,只不過如果使用@One查詢到的結果是多行,會拋出TooManyResultException異常,這種時候應該使用的是@Many注解,實現一對多的查詢。比如在需要查詢學生信息和每次考試的成績信息時。
@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
@Result(column="id", property="gradeList", javaType=List.class,
many=@Many(select="com.example.demo.mapper.GradeMapper.selectByStudentId"))
})
List<Student> selectAllAndGrade();
(5)傳遞多個參數。首先我們給這張表增加age(年齡)和gender(性別)兩個參數。當我們需要根據age和gender查詢學生的午餐,這時需要改寫column屬性的格式。等號左側的age和gender對應java接口的參數,右側的對應數據庫字段名。即將查到的my_student表中age和gender字段的值,分別賦給getLunchByAgeAndGender方法中的age和gender參數,去查詢對應的name(午餐名)。
@Select("select id, name, age, gender from my_student")
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
@Result(column="{age=age,gender=gender}", property="lunch",
one=@One(select="com.example.demo.mapper.StudentMapper.getLunchByAgeAndGender")),
})
List<Student> selectAllAndLunch();
@Select("select name from lunch where student_age = #{age} and student_gender = #{gender}")
String getLunchByAgeAndGender(@Param("age") int age, @Param("gender") int gender);
resultType
resultType可以把查詢結果封裝到pojo類型中,但必須pojo類的屬性名和查詢到的數據庫表的字段名一致。
如果sql查詢到的字段與pojo的屬性名不一致,則需要使用resultMap將字段名和屬性名對應起來,進行手動配置封裝,將結果映射到pojo中
resultMap
resultMap可以實現將查詢結果映射為復雜類型的pojo,比如在查詢結果映射對象中包括pojo和list實現一對一查詢和一對多查詢。
先在Mapper文件中,配置基本的sql語句
<!-- 查詢所有的訂單數據 -->
<!-- resultMap:填入配置的resultMap標簽的id值 -->
<select id="queryOrderAll" resultMap="orderResultMap">
SELECT id, user_id,
number,
createtime, note FROM `order` </select>配置resultMap標簽,映射不同的字段和屬性名
<!-- resultMap最終還是要將結果映射到pojo上,type就是指定映射到哪一個pojo -->
<!-- id:設置ResultMap的id -->
<resultMap type="order" id="orderResultMap">
<!-- 定義主鍵 ,非常重要。如果是多個字段,則定義多個id -->
<!-- property:主鍵在pojo中的屬性名 -->
<!-- column:主鍵在數據庫中的列名 -->
<id property="id" column="id" />
<!-- 定義普通屬性 -->
<result property="userId" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</resultMap>
結果就可以封裝到pojo類型中
使用resultMap進行關聯查詢
一對一查詢
一對一數據模型:訂單用戶
一個訂單信息只會是一個人下的訂單,所以從查詢訂單信息出發關聯查詢用戶信息為一對一查詢。如果從用戶信息出發查詢用戶下的訂單信息則為一對多查詢,因為一個用戶可以下多個訂單。
改造pojo類
在訂單類中添加User屬性,User屬性是一個引用類型,用于存儲關聯查詢的用戶信息,因為關聯關系是一對一,所以只需要添加單個屬性即可
配置Mapper.xml配置文件
OrderMapper.xml
先使用id和result屬性,映射order類的結果集,然后在使用association映射關聯對象User的結果集
<resultMap type="order" id="orderUserResultMap">
<id property="id" column="id" />
<result property="userId" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
<!-- association :配置一對一屬性 -->
<!-- property:order里面的User屬性名 -->
<!-- javaType:屬性類型 -->
<association property="user" javaType="user">
<!-- id:聲明主鍵,表示user_id是關聯查詢對象的唯一標識-->
<id property="id" column="user_id" />
<result property="username" column="username" />
<result property="address" column="address" />
</association>
</resultMap>
<!-- 一對一關聯,查詢訂單,訂單內部包含用戶屬性 -->
<select id="queryOrderUserResultMap" resultMap="orderUserResultMap">
SELECT
o.id,
o.user_id,
o.number,
o.createtime,
o.note,
u.username,
u.address
FROM
`order` o
LEFT JOIN `user` u ON o.user_id = u.id
</select>
測試
@Test
public void testQueryOrderUserResultMap() {
// mybatis和spring整合,整合之后,交給spring管理
SqlSession sqlSession = this.sqlSessionFactory.openSession();
// 創建Mapper接口的動態代理對象,整合之后,交給spring管理
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 使用userMapper執行根據條件查詢用戶,結果封裝到Order類中
List<Order> list = userMapper.queryOrderUserResultMap();
for (Order o : list) {
System.out.println(o);
}
// mybatis和spring整合,整合之后,交給spring管理
sqlSession.close();
}
結果
一對多查詢
查詢所有用戶信息及相關訂單。
修改pojo類,在pojo類添加訂單集合屬性
修改UserMapper.xml配置文件
先使用id和result配置映射User類的結果,然后使用一對多關系的collection標簽配置Order結果
<resultMap type="user" id="userOrderResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="birthday" column="birthday" />
<result property="sex" column="sex" />
<result property="address" column="address" />
<!-- 配置一對多的關系
property:填寫pojo類中集合類類屬性的名稱
javaType:填寫集合類型的名稱
-->
<collection property="orders" javaType="list" ofType="order">
<!-- 配置主鍵,是關聯Order的唯一標識 -->
<id property="id" column="oid" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</collection>
</resultMap>
<!-- 一對多關聯,查詢訂單同時查詢該用戶下的訂單 -->
<select id="queryUserOrder" resultMap="userOrderResultMap">
SELECT
u.id,
u.username,
u.birthday,
u.sex,
u.address,
o.id oid,
o.number,
o.createtime,
o.note
FROM
`user` u
LEFT JOIN `order` o ON u.id = o.user_id
</select>
測試結果
總結
以上是生活随笔為你收集整理的@Results用法和resultMap的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利民 AX90 SE ARGB 散热器上
- 下一篇: IOS苹果登录sign in with