mybatis 多表关联mapper文件写法操作
两张表SystemParam(系统参数表)Suit(主题)
SystemParam与Suit是多对一
Suit的higerSuit字段是Suit的父及主题id是多对一,需要自连接查询,因为重名所以父表sql字段加别名
mapper方法
SystemparamselectJoinSuit(Stringstrparamcode);
Po类
publicclassSystemparam{ //ManyToOne"主题" privateSuitsuitobj; privateStringstrparamcode; privateStringstrenable; privateStringstrparamname; //suit表主键 privateStringsuit; privateStringstrparamvalue; } publicclassSuit{ //ManyToOne privateSuitsuit; //主键 privateStringstrsuitcode; privateStringstrsuitname; //父级id privateStringhigersuit; }
resultMap的写法
resultMap使用extends继承上级map
select写法
select systempara0_.*, suit1_.*, suit2_.strSuitCodepstrSuitCode, suit2_.strSuitNamepstrSuitName, suit2_.higerSuitphigerSuit fromSystemParamsystempara0_ LEFTOUTERJOIN Suitsuit1_ ONsystempara0_.suit=suit1_.strSuitCode LEFTOUTERJOIN Suitsuit2_ ONsuit1_.higerSuit=suit2_.strSuitCode WHERE systempara0_.strParamCode=#{strparamcode,jdbcType=VARCHAR}
补充知识:Mybatis中resultMap标签实现多表查询(多个对象)
1n+1
1在teacher中添加Liststudent,
publicclassTeacher{ privateintid; privateStringname; privateListlist;
2在studentMapper.xml中添加通过tid查询
select*fromstudentwheretid=#{0}
3在TeacherMapper.xml中添加查询全部
select*fromteacher
其中collection是当属性为集合类型时使用的标签
2多表联合
selectt.idtid,t.nametname,s.idsid,s.namesname,age,tidfromteachertleftjoinstudentsont.id=s.tid
以上这篇mybatis多表关联mapper文件写法操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。