温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在mybatis中实现多对一关联查询

发布时间:2021-06-02 16:04:07 来源:亿速云 阅读:138 作者:Leah 栏目:开发技术

怎么在mybatis中实现多对一关联查询?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

第一种关联方式

1.修改实体类Student,追加关联属性,用于封装关联的数据

修改完以后重新生成get set方法还有toString方法

private Teacher teacher;
private Classes classes;

2.修改TeacherMapper相关配置

1.接口类 增加

Teacher selectTeacherById(Integer tid);

2.xml映射文件 增加

<sql id="params">tid,tname</sql>
<select id="selectTeacherById" resultType="Teacher">
    select
    <include refid="params"></include>
    from teacher where tid=#{tid}
</select>

3.修改ClassesMapper 相关配置

1.接口类 增加

Classes selectClassesById(Integer cid);

2.xml映射文件 增加

<resultMap type="Classes" id="clsMap">
  <id property="cid" column="cid"></id>
  <result property="cname" column="cname"/>
 </resultMap>
 <select id="selectClassesById" resultMap="clsMap">
  select * from classes where cid = #{cid}
 </select>

4.修改StudentMapper 相关配置

1.接口类 增加

Student selectStudentById(Integer sid);

2.xml映射文件 增加

ps:

多对一关联属性配置:

        property:关联对象名

        javaType:关联对象类型

        select:引用的关联查询sql对应id名

        column:引用的关联查询sql语句需要的参数

<resultMap type="Student" id="stuMap">
  <id property="sid" column="sid"/>
  <result property="sname" column="sname"/>
  <result property="age" column="age"/>
  <result property="email" column="email"/>
  <association property="teacher" select="com.yc.dao.TeacherMapper.selectTeacherById" column="tid" javaType="Teacher"></association>
  <association property="classes" select="com.yc.dao.ClassesMapper.selectClassesById" column="cid" javaType="Classes"></association>
 </resultMap>
        <select id="selectStudentById" resultMap="stuMap">
  select * from student where sid = #{sid}
 </select>

5.测试代码

@Test
 public void test1() {
  Student stu = studentMapper.selectStudentById(100001);
  System.out.println(stu);
 }

第二种配置方式

1.修改实体类Student,追加关联属性,用于封装关联的数据

跟前面一样的

2.修改studentMapper.xml映射文件

ps:接口里面的方法还是跟原来一样的

<resultMap type="Student" id="stuMap">
  <id property="sid" column="sid" />
  <result property="sname" column="sname" />
  <result property="age" column="age" />
  <result property="email" column="email" />
  <association property="teacher" javaType="Teacher">
   <id property="tid" column="tid"></id>
   <result property="tname" column="tname" />
  </association>
  <association property="classes" javaType="Classes">
   <id property="cid" column="cid"></id>
   <result property="cname" column="cname" />
  </association>
 </resultMap>
 <select id="selectStudentById" resultMap="stuMap">
  select * from student s,teacher t,classes c
  where s.sid=#{sid} and s.tid = t.tid and s.cid=c.cid
 </select>

小结一下:个人感觉第二种方法是会简单许多,只是说,因为查询语句中,连接条件的限制,当cid或者tid为空时,查询到的数据就会是null,而第一种的话,如果只是cid为空,至少还是会显示student和teacher的相关信息

第三种配置方式 全局映射

ps:student类和studentmapper接口类不变

1.修改全局配置文件的setting信息

<setting name="autoMappingBehavior" value="FULL" />
<setting name="mapUnderscoreToCamelCase" value="true" />

2. 修改studentMapper.xml映射文件

ps:

1.实体类的属性名要和表字段名保存一致,或按照驼峰命名规则(属性名:aColumn,字段名:A_COLUMN),settion属性mapUnderscoreToCamelCase设置成true 

2.关联表的字段名不能有相同的名字

<resultMap type="Student" id="stuMap">
  <association property="teacher" javaType="Teacher" />
  <association property="classes" javaType="Classes" />
 </resultMap>
 <select id="selectStudentById" resultMap="stuMap">
  select * from student s,teacher t,classes c where s.sid = #{sid} and
  s.tid=t.tid
  and s.cid=c.cid
 </select>

关于怎么在mybatis中实现多对一关联查询问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI