广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解mybatis多对一关联查询的方式
  • 337
分享到

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

2024-04-02 19:04:59 337人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

根据ID查询学生信息,要求该学生的教师和班级信息一并查出 第一种关联方式 1.修改实体类Student,追加关联属性,用于封装关联的数据 修改完以后重新生成get set方法还有t

根据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>

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

总结

到此这篇关于mybatis多对一关联查询的文章就介绍到这了,更多相关mybatis多对一关联查询内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解mybatis多对一关联查询的方式

本文链接: https://www.lsjlt.com/news/127408.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作