mybatis 多表關(guān)聯(lián)mapper文件寫法操作
兩張表SystemParam(系統(tǒng)參數(shù)表) Suit (主題)
SystemParam 與 Suit 是多對(duì)一
Suit 的higerSuit字段是Suit 的父及主題id 是多對(duì)一,需要自連接查詢,因?yàn)橹孛愿副韘ql字段加別名
mapper方法
Systemparam selectJoinSuit(String strparamcode);
Po類
public class Systemparam {
//ManyToOne "主題"
private Suit suitobj;
private String strparamcode;
private String strenable;
private String strparamname;
//suit表主鍵
private String suit;
private String strparamvalue;
}
public class Suit {
//ManyToOne
private Suit suit;
//主鍵
private String strsuitcode;
private String strsuitname;
//父級(jí)id
private String higersuit;
}
resultMap的寫法
<resultMap id="BaseResultMap" type="net.transino.model.Systemparam" > <id column="strParamCode" property="strparamcode" jdbcType="VARCHAR" /> <result column="strEnable" property="strenable" jdbcType="VARCHAR" /> <result column="strParamName" property="strparamname" jdbcType="VARCHAR" /> <result column="suit" property="suit" jdbcType="VARCHAR" /> </resultMap>
resultMap 使用extends 繼承上級(jí)map
<resultMap id="ResultMapWithBLOBs" type="net.transino.model.Systemparam" extends="BaseResultMap" > <result column="strParamValue" property="strparamvalue" jdbcType="LONGVARCHAR" /> </resultMap> <resultMap id="JoinsuitMap" type="net.transino.model.Systemparam" extends="ResultMapWithBLOBs" > <association property="suitobj" javaType="Suit"> <id column="strSuitCode" property="strsuitcode" jdbcType="VARCHAR" /> <result column="strSuitName" property="strsuitname" jdbcType="VARCHAR" /> <result column="higerSuit" property="higersuit" jdbcType="VARCHAR" /> <association property="suit" javaType="Suit"> <id column="pstrSuitCode" property="strsuitcode" jdbcType="VARCHAR" /> <result column="pstrSuitName" property="strsuitname" jdbcType="VARCHAR" /> <result column="phigerSuit" property="higersuit" jdbcType="VARCHAR" /> </association> </association> </resultMap>
select寫法
<select id="selectJoinSuit" resultMap="JoinsuitMap" parameterType="java.lang.String">
select
systempara0_.*,
suit1_.*,
suit2_.strSuitCode pstrSuitCode,
suit2_.strSuitName pstrSuitName,
suit2_.higerSuit phigerSuit
from SystemParam systempara0_
LEFT OUTER JOIN
Suit suit1_
ON systempara0_.suit=suit1_.strSuitCode
LEFT OUTER JOIN
Suit suit2_
ON suit1_.higerSuit=suit2_.strSuitCode
WHERE
systempara0_.strParamCode=#{strparamcode,jdbcType=VARCHAR}
</select>
補(bǔ)充知識(shí):Mybatis中resultMap標(biāo)簽實(shí)現(xiàn)多表查詢(多個(gè)對(duì)象)
1 n+1
1 在teacher中添加List student,
public class Teacher {
private int id;
private String name;
private List<Student> list;
2 在studentMapper.xml中添加通過(guò)tid查詢
<select id="selByTid" resultType="Student" parameterType="int">
select * from student where tid=#{0}
</select>
3 在TeacherMapper.xml中添加查詢?nèi)?/p>
<resultMap type="Teacher" id="mymap1"> <id column="id" property="id"/> <result column="name" property="name"/> <collection property="list" ofType="Student" select="com.bjsxt.mapper.StudentMapper.selByTid" column="id"></collection> </resultMap> <select id="selAll" resultMap="mymap1"> select * from teacher </select>
其中collection是當(dāng)屬性為集合類型時(shí)使用的標(biāo)簽
2 多表聯(lián)合
<resultMap type="Teacher" id="stumap1"> <id column="tid" property="id"/> <result column="tname" property="name"/> <collection property="list" ofType="Student"> <id column="sid" property="id"/> <result column="sname" property="name"/> <result column="age" property="age"/> <result column="tid" property="tid"/> <association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association> </collection> </resultMap> <select id="selAll1" resultMap="stumap1"> select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t left join student s on t.id=s.tid </select>
以上這篇mybatis 多表關(guān)聯(lián)mapper文件寫法操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java Split 實(shí)現(xiàn)去除一個(gè)空格和多個(gè)空格
這篇文章主要介紹了java Split 實(shí)現(xiàn)去除一個(gè)空格和多個(gè)空格,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Java Spring Security認(rèn)證與授權(quán)及注銷和權(quán)限控制篇綜合解析
Spring Security 是 Spring 家族中的一個(gè)安全管理框架,實(shí)際上,在 Spring Boot 出現(xiàn)之前,Spring Security 就已經(jīng)發(fā)展了多年了,但是使用的并不多,安全管理這個(gè)領(lǐng)域,一直是 Shiro 的天下2021-10-10
Java實(shí)現(xiàn)經(jīng)典游戲打磚塊游戲的示例代碼
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)經(jīng)典的游戲—打磚塊。玩家操作一根螢?zāi)簧纤降摹鞍糇印保屢活w不斷彈來(lái)彈去的“球”在撞擊作為過(guò)關(guān)目標(biāo)消去的“磚塊”的途中不會(huì)落到螢?zāi)坏紫?。感興趣的小伙伴可以了解一下2022-02-02
Spring Cloud引入Eureka組件,完善服務(wù)治理
這篇文章主要介紹了Spring Cloud引入Eureka組件,完善服務(wù)治理的過(guò)程詳解,幫助大家更好的理解和使用spring cloud,感興趣的朋友可以了解下2021-02-02
100-200之間所有素?cái)?shù)求和程序代碼(二個(gè)版本)
寫一個(gè)求100-200之間素?cái)?shù),并求和的程序,大家參考使用吧2013-11-11
SpringMVC使用MultipartFile實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了SpringMVC使用MultipartFile實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
SpringAOP實(shí)現(xiàn)自定義接口權(quán)限控制
本文主要介紹了SpringAOP實(shí)現(xiàn)自定義接口權(quán)限控制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

