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

 更新时间:2020年12月01日 08:44:41   作者:林奇lc  
这篇文章主要介绍了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>

以上这篇mybatis 多表关联mapper文件写法操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java Socket实现聊天室功能

    Java Socket实现聊天室功能

    这篇文章主要为大家详细介绍了Java Socket实现聊天室功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • java Split 实现去除一个空格和多个空格

    java Split 实现去除一个空格和多个空格

    这篇文章主要介绍了java Split 实现去除一个空格和多个空格,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • Java Spring Security认证与授权及注销和权限控制篇综合解析

    Java Spring Security认证与授权及注销和权限控制篇综合解析

    Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展了多年了,但是使用的并不多,安全管理这个领域,一直是 Shiro 的天下
    2021-10-10
  • Java实现经典游戏打砖块游戏的示例代码

    Java实现经典游戏打砖块游戏的示例代码

    这篇文章主要介绍了如何利用Java实现经典的游戏—打砖块。玩家操作一根萤幕上水平的“棒子”,让一颗不断弹来弹去的“球”在撞击作为过关目标消去的“砖块”的途中不会落到萤幕底下。感兴趣的小伙伴可以了解一下
    2022-02-02
  • java8之lambda表达式用法总结

    java8之lambda表达式用法总结

    这篇文章主要介绍了java8之lambda表达式用法总结,需要的朋友可以参考下
    2020-02-02
  • java中常见的中文乱码总结

    java中常见的中文乱码总结

    本文主要介绍了java中常见的中文乱码以及解决方法,主要包括字节码文件读取时出现的乱码问题,本文通过实例代码给大家介绍的非常详细,具有很好的参考价值,感兴趣的朋友跟随小编一起看看吧
    2017-03-03
  • Spring Cloud引入Eureka组件,完善服务治理

    Spring Cloud引入Eureka组件,完善服务治理

    这篇文章主要介绍了Spring Cloud引入Eureka组件,完善服务治理的过程详解,帮助大家更好的理解和使用spring cloud,感兴趣的朋友可以了解下
    2021-02-02
  • 100-200之间所有素数求和程序代码(二个版本)

    100-200之间所有素数求和程序代码(二个版本)

    写一个求100-200之间素数,并求和的程序,大家参考使用吧
    2013-11-11
  • SpringMVC使用MultipartFile实现文件上传

    SpringMVC使用MultipartFile实现文件上传

    这篇文章主要为大家详细介绍了SpringMVC使用MultipartFile实现文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • SpringAOP实现自定义接口权限控制

    SpringAOP实现自定义接口权限控制

    本文主要介绍了SpringAOP实现自定义接口权限控制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11

最新评论