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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

【Mybatis】 mapper XML 文件中使用 association 实现一对一关联

發(fā)布時(shí)間:2024/9/19 asp.net 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Mybatis】 mapper XML 文件中使用 association 实现一对一关联 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

  • Mybatis 一對(duì)一,使用 association 標(biāo)簽
  • Mybatis 一對(duì)多,使用 collection 標(biāo)簽
  • 本文主要說明 association 標(biāo)簽。 collection 標(biāo)簽與之類似,參考這里。
  • association 標(biāo)簽有兩種用法:join查詢、嵌套查詢
  • 文中代碼是從項(xiàng)目中Copy后再修改得到的,如果有對(duì)不上的地方,需要自動(dòng)糾錯(cuò)。

假設(shè),有下面這個(gè)實(shí)體類:

public class Car {/** 車輛編號(hào) */private Long carid;/** 公司編號(hào) */private Long comid;/** 車牌號(hào) */private String carnum;/** 公司 */private Company company;... }

association 的 join查詢用法

略。

association 的嵌套查詢用法

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.office.leasing.mapper.CarMapper"><resultMap type="Car" id="CarResult"><result property="carid" column="carid" /><result property="comid" column="comid" /><result property="carnum" column="carnum" /><result property="carbrand" column="carbrand" /><result property="carcolor" column="carcolor" /><result property="cartype" column="cartype" /><result property="remark" column="remark" /><result property="status" column="status" /><association property="company" javaType="Company" column="comid" select="selectCompany" /></resultMap><resultMap type="Company" id="CompanyResult"><result property="leaid" column="leaid" /><result property="leaname" column="leaname" /><result property="address" column="address" /><result property="telnumber" column="telnumber" /><result property="contacts" column="contacts" /><result property="createTime" column="create_time" /><result property="status" column="status" /></resultMap><select id="selectCarListCustom" parameterType="Car" resultMap="CarResult">select * from car<where> <if test="carnum != null and carnum != ''"> and carnum = #{carnum}</if></where></select><select id="selectCompany" parameterType="Integer" resultMap="CompanyResult">select * from company where comid=#{comid}</select> </mapper>
  • <association ... column="comid" ... /> 中 comid 是數(shù)據(jù)庫中 car 表的 comid 列的列名。
  • <select id="selectCompany" ...>... comid=#{comid}... 中 #{comid} 的值對(duì)應(yīng)數(shù)據(jù)庫中car表的comid列。
  • <select id="selectCompany" ...>... comid=#{comid}... 中 #{comid} 的變量名comid可以隨便叫。比如可以用#{abc}替代#{comid}。因?yàn)閜arameterType="Integer",所以叫啥都無所謂了。
  • 也可以這樣寫 <association ... column="{id=comid}" ... /> 。id 是自定義的變量名,想叫啥叫啥。comid 是數(shù)據(jù)庫中 car 表的 comid 列的列名。

遇到復(fù)合主鍵是怎么辦?

<mapper namespace="..."><resultMap type="XX" id="XXMap"><id property="id" column="colid"/> <id property="name" column="colname"/> <collection property="list" javaType="ArrayList" column="{id = colid,name=colname}" select="getSubXXX"/> </resultMap><select id="getSubXXX" parameterType="map" resultMap="xxx">select * from xxx where xxx_id=#{id} and xxx_name=#{name}</select> </mapper>
  • column="{id = colid,name=colname}" id+name為符合主鍵
  • id、name為別名/變量名
  • colid、colname為數(shù)據(jù)庫中表的列名
  • 在getSubXXX中,直接使用別名/變量名即可,比如:#{id}。

錯(cuò)誤:元素類型為 “resultMap“ 的內(nèi)容必須匹配 “(constructor?,id*,result*,association*,collection*,discriminator?)”

參考這里。

參考

https://mybatis.org/mybatis-3/sqlmap-xml.html
https://mybatis.org/mybatis-3/zh/sqlmap-xml.html
https://www.cnblogs.com/duanxz/p/3830509.html
https://www.cnblogs.com/YingYue/p/3912648.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的【Mybatis】 mapper XML 文件中使用 association 实现一对一关联的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。