mybatis 多表關(guān)聯(lián)mapper文件寫法操作
兩張表SystemParam(系統(tǒng)參數(shù)表) Suit (主題)
SystemParam 與 Suit 是多對一
Suit 的higerSuit字段是Suit 的父及主題id 是多對一,需要自連接查詢,因為重名所以父表sql字段加別名
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;
//父級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 繼承上級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ǔ)充知識:Mybatis中resultMap標(biāo)簽實現(xiàn)多表查詢(多個對象)
1 n+1
1 在teacher中添加List student,
public class Teacher {
private int id;
private String name;
private List<Student> list;
2 在studentMapper.xml中添加通過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)屬性為集合類型時使用的標(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文件寫法操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一分鐘入門Java Spring Boot徹底解決SSM配置問題
Spring Boot是由Pivotal團(tuán)隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進(jìn)行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發(fā)展的快速應(yīng)用開發(fā)領(lǐng)域成為領(lǐng)導(dǎo)者2021-10-10
java實現(xiàn)圖片轉(zhuǎn)ascii字符畫的方法示例
這篇文章主要介紹了java實現(xiàn)圖片轉(zhuǎn)ascii字符畫的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
Java中注解@Async實現(xiàn)異步及導(dǎo)致失效原因分析
Async注解用于聲明一個方法是異步的,當(dāng)在方法上加上這個注解時將會在一個新的線程中執(zhí)行該方法,而不會阻塞原始線程,這篇文章主要給大家介紹了關(guān)于Java中注解@Async實現(xiàn)異步及導(dǎo)致失效原因分析的相關(guān)資料,需要的朋友可以參考下2024-07-07
基于線程的wait和notify使用,生產(chǎn)消費案例
這篇文章主要介紹了基于線程的wait和notify使用,生產(chǎn)消費案例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

