mybatis 多表关联mapper文件写法操作
作者:林奇lc 发布时间:2021-12-02 23:21:25
标签:mybatis,多表关联,mapper
两张表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的Dao层实现传统开发方式编写UserDao接口public interface UserDao {
- 参考文章图解Java中插入排序算法的原理与实现实现效果示例代码import java.awt.*;public class AlgoVisu
- 1.元组(Tuple)元组(Tuple)在4.0 的时候就有了,但元组也有些缺点,如: 1)Tuple 会影响代码的
- 一个简单的网格布局activity_main.xml<?xml version="1.0" encoding=&q
- 目录课设要求相关知识点1.服务端能够看到所有在线用户2.服务端能够强制用户下线3.客户端能够看到所有在线用户4.客户端要求能够向某个用户发送
- 如何使用exe4j把jar打包成exe文件最近,做了几个javafx的项目,想要把jar包打成exe的可执行软件,下面时我使用exe4j打包
- 一、实现原理使用MockMvc发起请求,然后执行API中相应的代码,在执行的过程中使mock模拟底层数据的返回,最后结果验证。二、常用注解介
- 1.设置url-pattern为*.do(最为常见的方式)只要你的请求url中包含配置的url-pattern,该url就可以到达Dispa
- 作者:精致码农出处:http://cnblogs.com/willick联系:liam.wang@live.com最近工作中遇到一个这样的需
- 下面本文将针对以上几点问题进行描述讨论,我们就以“中文”两个字为例来说明,查找相关资料可知“中文”的GB2312编码是“d6d0 cec4”
- FTPS:一种多传输协议,相当于加密版的FTP。当你在FTP服务器上收发文件的时候,你面临两个风险。第一个风险是在上载文件的时候为文件加密。
- 本文实例讲述了Java实现的简单音乐播放器功能。分享给大家供大家参考,具体如下:应用名称:Java简单的音乐播放器用到的知识:Java GU
- 使用场景当我们在应用的Assets目录中需要加入文件时,可以直接将源文件放入,但这样会造成打包后的apk整体过大,此时就需要将放入的文件进行
- Android 如何修改APK的默认名称用Android Studio 打包App时生成的名称默认是 app-release.apk(已签名
- — 遇到问题今天在IDEA里面运行项目的时候报了一个错,如下图所示:— 找到问题根源其实控制台给出的错误信息提示说的很明显:类加载器加载文件
- Spring Rest接口路径参数可选我有一个 Spring Rest 服务,其中有一个路径参数是可选的(实际情况是我原来将参数放到路径中,
- 闲话不多说,直接上图。给大家讲讲我的编程思想吧。第一部分:沉浸式状态栏(API-Level 19, Android4.4 KitKat 之后
- 首先看一看什么是装箱和拆箱?简单的来说:装箱就是值类型转换为引用类型;拆箱就是引用类型转换为值类型。值类型,包括原类型(Sbyte、Byte
- 目录登陆界面的实现登陆界面代码Login类login的监听类 LoginListener聊天界面运行图Client类代码Server代码登陆
- 本文实例为大家分享了SpringMVC实现上传下载文件的具体代码,供大家参考,具体内容如下一、SpringMVC专门提供了CommonsMu