欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis?resultMap?id標(biāo)簽的錯(cuò)誤使用方式

 更新時(shí)間:2022年01月20日 11:10:54   作者:BEESTACK  
這篇文章主要介紹了MyBatis?resultMap?id標(biāo)簽的錯(cuò)誤使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

MyBatis resultMap id標(biāo)簽的錯(cuò)誤使用

我們?cè)诰帉慥O對(duì)象,如果業(yè)務(wù)場(chǎng)景稍微復(fù)雜一點(diǎn),就會(huì)用到集合屬性。例如用戶查看個(gè)人訂單列表,每個(gè)訂單又包含多種或者多個(gè)規(guī)格的商品。

本節(jié)的問題主要是我對(duì)mybatis id標(biāo)簽的錯(cuò)誤使用

id是resultMap以及Collection的子標(biāo)簽,標(biāo)記出作為 ID 的結(jié)果可以幫助提高整體性能。特別注意的是,id是當(dāng)前命名空間中的一個(gè)唯一標(biāo)識(shí),用于標(biāo)識(shí)一個(gè)結(jié)果映射。

如下圖,itemId(商品id)字段值在數(shù)據(jù)庫(kù)中不唯一,錯(cuò)誤使用會(huì)導(dǎo)致只返回該訂單某商品的一條記錄。因?yàn)閷?duì)于某個(gè)商品,麻辣味和五香味只是商品規(guī)格,其商品id是相同的。

在這里插入圖片描述

在這里插入圖片描述

改用普通result標(biāo)簽后,返回正確結(jié)果。

在這里插入圖片描述

在這里插入圖片描述

EOF

resultMap標(biāo)簽的使用規(guī)則

自定義結(jié)果映射規(guī)則

<!-- resultMap自定義某個(gè)javabean的封裝規(guī)則
? ? ? ?type:自定義規(guī)則的java類型
? ? ? ?id:唯一id方便引用
? ? ?-->
? ? <resultMap type="entity.Employee" id="getEmpByIdMap">
? ? ? ?<!-- id指定主鍵列的封裝規(guī)則
? ? ? ? ? ?column:指定哪一列
? ? ? ? ? ?property:指定對(duì)應(yīng)的javabean屬性
? ? ? ? -->
? ? ? ?<id column="id" property="id"/>
? ? ? ?<!-- result定義普通列封裝規(guī)則,若屬性名與數(shù)據(jù)庫(kù)對(duì)應(yīng)表的列名相同可不寫,
? ? ? ? ? ? mybatis會(huì)自動(dòng)封裝,但建議將所有的映射規(guī)則都寫上
? ? ? ?-->
? ? ? ?<result column="name" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? </resultMap>
? ? <!-- public Employee getEmpById(Integer id) -->
? ? <select id="getEmpById" resultMap="getEmpByIdMap">
? ? ? ?select * from employee where id=#{id}
? ? </select>

association聯(lián)合查詢

  • association可以指定聯(lián)合的javabean對(duì)象
  • property="dept":指定哪個(gè)屬性是聯(lián)合對(duì)象
  • javaType:指定這個(gè)屬性的類型
<resultMap type="entity.Employee" id="getEmpAndDeptMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="empName" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? ? ?<!-- association可以指定聯(lián)合的javabean對(duì)象
? ? ? ? ? ? property="dept":指定哪個(gè)屬性是聯(lián)合對(duì)象
? ? ? ? ? ? javaType:指定這個(gè)屬性的類型-->
? ? ? ?<association property="dept" javaType="entity.Department">
? ? ? ? ? ?<id column="did" property="id"/>
? ? ? ? ? ?<result column="deptName" property="departmentName"/>
? ? ? ?</association>
? ? </resultMap>
? ? <!-- public Employee getEmpAndDept(Integer id) -->
? ? <select id="getEmpAndDept" resultMap="getEmpAndDeptMap">
? ? ? ?select e.id id,e.name empName,e.email email,e.sex sex,e.d_id d_id,
? ? ? ? ? ?d.id did,d.name deptName from employee e,dept d
? ? ? ? ? ?where e.d_id=d.id and e.id=#{id}
? ? </select>

使用association進(jìn)行分布查詢

 1、先按照員工id查詢員工信息將會(huì)調(diào)用查詢員工的sql

2、根據(jù)查詢員工信息中的d_id值去部門表中查出部門信息

3、部門設(shè)置到員工中

        

<resultMap type="entity.Employee" id="getEmpAndDeptStepMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="name" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? ? ?<!-- association定義關(guān)聯(lián)對(duì)象的封裝規(guī)則
? ? ? ? ? ? select:表明當(dāng)前屬性是調(diào)用select指定的方法查出的結(jié)果
? ? ? ? ? ? column:指定將那一列的值作為參數(shù)傳給這個(gè)方法
? ? ? ? ? ? ?流程:使用select指定的方法(傳入column指定的這列參數(shù)的值)查出對(duì)象,
? ? ? ? ? ? ?并封裝給property指定的屬性
? ? ? ? ? ? -->
? ? ? ? ? ? <!-- discriminator鑒別器
? ? ? ? ? ? ? ? ?column:指定判定的列名
? ? ? ? ? ? ? ? ?javaType:列值對(duì)應(yīng)的java類型
? ? ? ? ? ? ?-->
? ? ? ?<discriminator javaType="string" column="sex">
? ? ? ? ? ?<!-- resultType不能缺少 -->
? ? ? ? ? ?<case value="男" resultType="entity.Employee">
? ? ? ? ? ? ? <association property="dept" select="dao.DepartmentMapper.getDeptById"
? ? ? ? ? ? ? ? ? column="d_id">
? ? ? ? ? ? ? </association>
? ? ? ? ? ?</case>
? ? ? ?</discriminator>
? ? </resultMap>
? ? <!-- public Employee getEmpByIdStep(Integer id) -->
? ? <select id="getEmpByIdStep" resultMap="getEmpAndDeptStepMap">
? ? ? ?select * from employee where id=#{id}
? ? </select>

嵌套結(jié)果集的方式,使用collection標(biāo)簽定義關(guān)聯(lián)的集合類型的屬性封裝規(guī)則

<resultMap type="entity.Department" id="getDeptByIdPlusMap">
? ? ? ?<id column="did" property="id"/>
? ? ? ?<result column="deptName" property="departmentName"/>
? ? ? ?<!-- collection定義關(guān)聯(lián)集合類型的屬性的封裝規(guī)則
? ? ? ? ? ? ofType:指定集合里面元素的類型 ? ? ? ? ? ??
? ? ? ? -->
? ? ? ?<collection property="emps" ofType="entity.Employee">
? ? ? ? ? ?<!-- 定義這個(gè)集合中元素的封裝規(guī)則 -->
? ? ? ? ? ?<id column="eid" property="id"/>
? ? ? ? ? ?<result column="empName" property="name"/>
? ? ? ? ? ?<result column="sex" property="sex"/>
? ? ? ? ? ?<result column="email" property="email"/>
? ? ? ?</collection>
? ? </resultMap>
? ? <!-- public Department getDeptByIdPlus(Integer id) -->
? ? <select id="getDeptByIdPlus" resultMap="getDeptByIdPlusMap">
? ? ? ?select d.id did,d.name deptName,e.id eid,
? ? ? ? ? ?e.name empName,e.sex,e.email
? ? ? ? ? ?from dept d left join employee e
? ? ? ? ? ?on d.id=e.d_id
? ? ? ? ? ?where d.id=#{id}
? ? </select>

collection分步查詢

<resultMap type="entity.Department" id="getDeptByIdStepMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="name" property="departmentName"/>
? ? ? ?<collection property="emps" select="dao.EmployeeMapperPlus.getEmpsByDeptId"
? ? ? ? ? ?column="{id}">
? ? ? <!-- 或則 column="{deptId=id}"-->
? ? ? ?</collection>
? ? </resultMap>
? ?<!-- public List<Employee> getEmpsByDeptId(Integer deptId -->
? ?<select id="getEmpsByDeptId" resultType="entity.Employee">
? ? ? ?select * from employee where d_id=#{deptId}
? ? </select>
? ? <!-- public Department getDeptByIdStep(Integer id) -->
? ? <select id="getDeptByIdStep" resultMap="getDeptByIdStepMap">
? ? ? ?select * from dept where id=#{id}
? ? </select>

當(dāng)分布查詢需要傳遞多個(gè)多個(gè)值時(shí),將多個(gè)值封裝map傳遞

colum=“{key1=column1,key2=colum2...}”

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot項(xiàng)目攔截器重定向循環(huán)問題的解決

    springboot項(xiàng)目攔截器重定向循環(huán)問題的解決

    這篇文章主要介紹了springboot項(xiàng)目攔截器重定向循環(huán)問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • mybatisplus邏輯刪除基本實(shí)現(xiàn)和坑點(diǎn)解決

    mybatisplus邏輯刪除基本實(shí)現(xiàn)和坑點(diǎn)解決

    這篇文章主要介紹了mybatisplus邏輯刪除基本實(shí)現(xiàn)和坑點(diǎn)解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Springcloud Nacos基本操作代碼實(shí)例

    Springcloud Nacos基本操作代碼實(shí)例

    這篇文章主要介紹了Springcloud Nacos基本操作代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 淺談Mybatis+mysql 存儲(chǔ)Date類型的坑

    淺談Mybatis+mysql 存儲(chǔ)Date類型的坑

    這篇文章主要介紹了淺談Mybatis+mysql 存儲(chǔ)Date類型的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-11-11
  • SpringBoot中jar打包并部署到Tomcat

    SpringBoot中jar打包并部署到Tomcat

    最近做了一個(gè)SpringBoot項(xiàng)目,今天介紹一下SpringBoot中jar打包并部署到Tomcat,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 詳解Java中CountDownLatch的用法

    詳解Java中CountDownLatch的用法

    這篇文章主要為大家詳細(xì)介紹了Java中CountDownLatch類的用法,本文通過一些簡(jiǎn)單的示例進(jìn)行了簡(jiǎn)單介紹,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-05-05
  • Java經(jīng)典排序算法之二分插入排序詳解

    Java經(jīng)典排序算法之二分插入排序詳解

    這篇文章主要為大家詳細(xì)介紹了Java經(jīng)典排序算法之二分插入排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • ibatis學(xué)習(xí)之搭建Java項(xiàng)目

    ibatis學(xué)習(xí)之搭建Java項(xiàng)目

    本文的主要內(nèi)容是簡(jiǎn)單介紹了ibatis和如何通過iBatis搭建JAVA項(xiàng)目,包含了一個(gè)相關(guān)實(shí)例,需要的朋友可以參考下。
    2017-09-09
  • SpringBoot?中使用?Validation?校驗(yàn)參數(shù)的方法詳解

    SpringBoot?中使用?Validation?校驗(yàn)參數(shù)的方法詳解

    Validation?是用于檢查程序代碼中參數(shù)的有效性的框架,作為?Spring?框架中的一個(gè)參數(shù)校驗(yàn)工具,集成在?spring-context?包中,這篇文章主要介紹了SpringBoot?中使用?Validation?校驗(yàn)參數(shù),需要的朋友可以參考下
    2022-05-05
  • Spring?@Cacheable指定失效時(shí)間實(shí)例

    Spring?@Cacheable指定失效時(shí)間實(shí)例

    這篇文章主要介紹了Spring?@Cacheable指定失效時(shí)間實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論