mybatis多对一关联查询 详解mybatis多对一关联查询的方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

mybatis多对一关联查询 详解mybatis多对一关联查询的方式

tianzebeibei   2021-06-02 我要评论
想了解详解mybatis多对一关联查询的方式的相关内容吗,tianzebeibei在本文为您仔细讲解mybatis多对一关联查询的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:mybatis多对一关联查询,mybatis关联查询配置,mybatis联查,下面大家一起来学习吧。

根据ID查询学生信息,要求该学生的教师和班级信息一并查出

第一种关联方式

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>

还是连接查询为空的问题~

总结

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们