日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

@Results用法和resultMap的用法

發布時間:2024/6/21 综合教程 31 生活家
生活随笔 收集整理的這篇文章主要介紹了 @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的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 网站在线播放 | 火影忍者羞羞漫画 | 青青草亚洲 | 亚洲成人麻豆 | 国产精品18久久久久久vr下载 | 操操操爽爽爽 | 美国伊人网 | 国产精品嫩草久久久久 | 日本人妖网站 | 在线观看中文字幕亚洲 | 久久久午夜视频 | 日本美女动态图 | 狂野少女电影在线观看国语版免费 | 99在线精品视频免费观看软件 | 日本加勒比中文字幕 | 日韩欧美在线中文字幕 | 9l视频自拍蝌蚪9l视频成人 | 韩国av电影网站 | 亚洲精品在线观看av | 日韩免费av一区 | 成年人性生活视频 | 欧美区一区二区三 | 亚洲天堂男人天堂 | 美女调教视频 | 国产精品久久久久久久久毛片 | 少妇人妻好深好紧精品无码 | 日韩一级高清 | 中文字字幕一区二区三区四区五区 | 欧美日韩中文字幕视频 | 国产福利一区二区三区 | 亚洲网在线观看 | 重口味av| 在线免费观看黄色 | 欧美精品片 | 极品少妇av| 九色视频在线观看 | 亚洲欧美国产日韩精品 | 日韩手机在线观看 | 91精品国产综合久久国产大片 | 五月婷婷综合色 | 免费网站观看www在线观看 | 国产一区二区小视频 | 91在线免费网站 | 天天想你免费观看完整版高清电影 | 国产一级全黄 | 在线视频免费观看 | 五月婷婷综合色 | 性感美女黄色片 | 国产精品久线在线观看 | 99嫩草 | 欧美精品久久久久久久 | 麻豆出品 | 成人五区| 亚a在线| 河北彩花av在线播放 | 午夜精品免费观看 | 日韩欧美一区二区区 | 亚洲一级片在线观看 | 日本女优在线看 | 欧美成人极品 | 99国产精品视频免费观看一公开 | 插少妇视频| 国产精品成人一区二区三区 | 中文字幕一区二区三区人妻 | 国产一区二区视频在线免费观看 | 日韩一级视频在线观看 | 亚洲www在线观看 | 国产精品久久av无码一区二区 | 亚洲天天av | 日本精品免费一区二区三区 | 国产一区二区视频免费观看 | a毛片在线观看 | 91免费版视频 | 一级片日韩| 神马午夜91 | 久久精品国产亚洲av成人 | 国产亚洲欧美视频 | 欧美aaaaa | 国产91在线看 | 国产做爰xxxⅹ久久久精华液 | 五月婷婷综 | 天天操天天干天天干 | 午夜一级片 | 久久久久久久久久综合 | 日韩在线导航 | 日韩在线视 | 亚洲色图20p | 中文字幕在线有码 | 亚洲国产伊人 | 日本一区二区三区在线免费观看 | 在线a网 | 亚洲天堂社区 | 成人一区二区三区在线 | 性生活视频播放 | 亚洲国产精品二区 | 欧美日韩亚洲免费 | japan高清日本乱xxxxx | 青青操视频在线 | 男人天堂网址 |