@Results用法和resultMap的用法
原文鏈接:https://blog.csdn.net/cherlshall/article/details/80950150
原文鏈接:https://blog.csdn.net/qq_42780864/article/details/81429114
MyBatis中使用@Results注解來映射查詢結(jié)果集到實(shí)體類屬性。
(1)@Results的基本用法。當(dāng)數(shù)據(jù)庫字段名與實(shí)體類對應(yīng)的屬性名不一致時,可以使用@Results映射來將其對應(yīng)起來。column為數(shù)據(jù)庫字段名,porperty為實(shí)體類屬性名,jdbcType為數(shù)據(jù)庫字段數(shù)據(jù)類型,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();
如上所示的數(shù)據(jù)庫字段名class_id與實(shí)體類屬性名classId,就通過這種方式建立了映射關(guān)系。名字相同的可以省略。
(2)@ResultMap的用法。當(dāng)這段@Results代碼需要在多個方法用到時,為了提高代碼復(fù)用性,我們可以為這個@Results注解設(shè)置id,然后使用@ResultMap注解來復(fù)用這段代碼。
@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的用法。當(dāng)我們需要通過查詢到的一個字段值作為參數(shù),去執(zhí)行另外一個方法來查詢關(guān)聯(lián)的內(nèi)容,而且兩者是一對一關(guān)系時,可以使用@One注解來便捷的實(shí)現(xiàn)。比如當(dāng)我們需要查詢學(xué)生信息以及其所屬班級信息時,需要以查詢到的class_id為參數(shù),來執(zhí)行ClassesMapper中的selectById方法,從而獲得學(xué)生所屬的班級信息。可以使用如下代碼。
@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查詢到的結(jié)果是多行,會拋出TooManyResultException異常,這種時候應(yīng)該使用的是@Many注解,實(shí)現(xiàn)一對多的查詢。比如在需要查詢學(xué)生信息和每次考試的成績信息時。
@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)傳遞多個參數(shù)。首先我們給這張表增加age(年齡)和gender(性別)兩個參數(shù)。當(dāng)我們需要根據(jù)age和gender查詢學(xué)生的午餐,這時需要改寫column屬性的格式。等號左側(cè)的age和gender對應(yīng)java接口的參數(shù),右側(cè)的對應(yīng)數(shù)據(jù)庫字段名。即將查到的my_student表中age和gender字段的值,分別賦給getLunchByAgeAndGender方法中的age和gender參數(shù),去查詢對應(yīng)的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可以把查詢結(jié)果封裝到pojo類型中,但必須pojo類的屬性名和查詢到的數(shù)據(jù)庫表的字段名一致。
如果sql查詢到的字段與pojo的屬性名不一致,則需要使用resultMap將字段名和屬性名對應(yīng)起來,進(jìn)行手動配置封裝,將結(jié)果映射到pojo中
resultMap
resultMap可以實(shí)現(xiàn)將查詢結(jié)果映射為復(fù)雜類型的pojo,比如在查詢結(jié)果映射對象中包括pojo和list實(shí)現(xiàn)一對一查詢和一對多查詢。
先在Mapper文件中,配置基本的sql語句
<!-- 查詢所有的訂單數(shù)據(jù) -->
<!-- resultMap:填入配置的resultMap標(biāo)簽的id值 -->
<select id="queryOrderAll" resultMap="orderResultMap">
SELECT id, user_id,
number,
createtime, note FROM `order` </select>配置resultMap標(biāo)簽,映射不同的字段和屬性名
<!-- resultMap最終還是要將結(jié)果映射到pojo上,type就是指定映射到哪一個pojo -->
<!-- id:設(shè)置ResultMap的id -->
<resultMap type="order" id="orderResultMap">
<!-- 定義主鍵 ,非常重要。如果是多個字段,則定義多個id -->
<!-- property:主鍵在pojo中的屬性名 -->
<!-- column:主鍵在數(shù)據(jù)庫中的列名 -->
<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>
結(jié)果就可以封裝到pojo類型中
使用resultMap進(jìn)行關(guān)聯(lián)查詢
一對一查詢
一對一數(shù)據(jù)模型:訂單用戶
一個訂單信息只會是一個人下的訂單,所以從查詢訂單信息出發(fā)關(guān)聯(lián)查詢用戶信息為一對一查詢。如果從用戶信息出發(fā)查詢用戶下的訂單信息則為一對多查詢,因?yàn)橐粋€用戶可以下多個訂單。
改造pojo類
在訂單類中添加User屬性,User屬性是一個引用類型,用于存儲關(guān)聯(lián)查詢的用戶信息,因?yàn)殛P(guān)聯(lián)關(guān)系是一對一,所以只需要添加單個屬性即可
配置Mapper.xml配置文件
OrderMapper.xml
先使用id和result屬性,映射order類的結(jié)果集,然后在使用association映射關(guān)聯(lián)對象User的結(jié)果集
<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是關(guān)聯(lián)查詢對象的唯一標(biāo)識-->
<id property="id" column="user_id" />
<result property="username" column="username" />
<result property="address" column="address" />
</association>
</resultMap>
<!-- 一對一關(guān)聯(lián),查詢訂單,訂單內(nèi)部包含用戶屬性 -->
<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();
// 創(chuàng)建Mapper接口的動態(tài)代理對象,整合之后,交給spring管理
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 使用userMapper執(zhí)行根據(jù)條件查詢用戶,結(jié)果封裝到Order類中
List<Order> list = userMapper.queryOrderUserResultMap();
for (Order o : list) {
System.out.println(o);
}
// mybatis和spring整合,整合之后,交給spring管理
sqlSession.close();
}
結(jié)果
一對多查詢
查詢所有用戶信息及相關(guān)訂單。
修改pojo類,在pojo類添加訂單集合屬性
修改UserMapper.xml配置文件
先使用id和result配置映射User類的結(jié)果,然后使用一對多關(guān)系的collection標(biāo)簽配置Order結(jié)果
<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" />
<!-- 配置一對多的關(guān)系
property:填寫pojo類中集合類類屬性的名稱
javaType:填寫集合類型的名稱
-->
<collection property="orders" javaType="list" ofType="order">
<!-- 配置主鍵,是關(guān)聯(lián)Order的唯一標(biāo)識 -->
<id property="id" column="oid" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</collection>
</resultMap>
<!-- 一對多關(guān)聯(lián),查詢訂單同時查詢該用戶下的訂單 -->
<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>
測試結(jié)果
總結(jié)
以上是生活随笔為你收集整理的@Results用法和resultMap的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利民 AX90 SE ARGB 散热器上
- 下一篇: IOS苹果登录sign in with