这篇文章主要介绍了mybatis 多表关联mapper文件写法操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
两张表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 使用extends 继承上级map
select写法
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}
补充知识:Mybatis中resultMap标签实现多表查询(多个对象)
1 n+1
1 在teacher中添加List student,
public class Teacher { private int id; private String name; private List list;
2 在studentMapper.xml中添加通过tid查询
select * from student where tid=#{0}
select * from teacher
2 多表联合
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
以上这篇mybatis 多表关联mapper文件写法操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。