mybatis 多表关联mapper文件写法操作

作者:林奇lc 时间:2021-12-02 23:21:25 

两张表SystemParam(系统参数表) 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>

补充知识:Mybatis中resultMap标签实现多表查询(多个对象)

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中添加查询全部


<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是当属性为集合类型时使用的标签

2 多表联合


<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>

来源:https://blog.csdn.net/zlc819240815/article/details/83008475

标签:mybatis,多表关联,mapper
0
投稿

猜你喜欢

  • Java中synchronized锁的深入理解

    2023-08-18 01:36:55
  • Java压缩解压zip技术_动力节点Java学院整理

    2022-08-29 22:50:42
  • Java中关于二叉树的概念以及搜索二叉树详解

    2023-01-12 00:09:25
  • 用c# 自动更新程序

    2023-04-03 09:03:46
  • C语言实现贪吃蛇游戏演示

    2023-07-03 14:16:43
  • java9中gc log参数迁移

    2022-06-28 03:21:01
  • 新浪微博第三方登录界面上下拉伸图片之第三方开源PullToZoomListViewEx(二)

    2021-07-25 04:52:35
  • Java GUI编程实现在线聊天室

    2022-04-07 22:50:09
  • C#利用GDI绘制常见图形和文字

    2023-04-09 20:44:40
  • Android Studio 3.0 新功能全面解析和旧项目适配问题

    2022-09-16 23:53:57
  • Java中StringTokenizer的用法简介汇总

    2023-05-29 00:52:24
  • Unity实现聊天室功能

    2023-12-20 19:53:34
  • C#如何动态创建lambda表达式

    2022-04-18 21:57:16
  • Java设计模式之状态模式

    2022-05-08 07:24:25
  • 详解C#通过反射获取对象的几种方式比较

    2021-07-26 17:45:55
  • SpringBoot 多Profile使用与切换方式

    2022-04-13 14:58:27
  • Java实现添加,读取和删除Excel图片的方法详解

    2023-11-27 06:29:33
  • 基于html5+java实现大文件上传实例代码

    2023-09-26 02:14:29
  • C#面向对象特征的具体实现及作用详解

    2023-11-14 06:43:39
  • springboot配置文件中属性变量引用方式@@解读

    2023-11-24 20:39:18
  • asp之家 软件编程 m.aspxhome.com