Mybatis关联查询心得【一对一,一对多,left jion方式使用对比】

在项目中遇到多表关联查询,主要就是 一对一,一对多关联和 left join方式的对比,结合前端框架 做内容展示和搜索功能时的区别!

此处只做简单实例,就不写实体、controller和service了。

假设有2张表(pk为表主键):
user表:

pkidnamesex

userImg(用户图片)表:

pkidurluserIdtype

1、方式一:Mybatis的一对多,一对一查询方式

为了解释和left join 的区别,一对多就是下面的one 改成 many = @Many(select = " "),
就不单独写了。

//查询userImg表 同时关联查询user表,type字段作为搜索条件
@Select(value="
<script>
  select id,url,userId,type  from userImg 
  <where>
    <if test = 'type != null and type != ""'> type = #{type}</if>
  </where>
</script>")
 @Results({
            @Result(property = "userId" , column = "userId"),//设置关联字段
            @Result(property = "name" , column = "userId" ,//property 中的name 需要在userImg实体类中声明,单独查一个字段就声明一个字段,若要所有字段,也可以声明整个实体类
                    one = @One(select="com.test.mapper.userMapper.getUserName"))  //此处为关联查询的方法路径
    })
List<UserImg> getlist(UserImg userImg);

//根据上面写的查询,一般不在同一个Mapper类中,此处方便展示
@Select(value="Select name from user where id = #{userId}")
String getUserName (int userId);

此处只举例关联一个表,要关联多个表,只需在@Results里面继续增加@Result,设置好字段和代理名字、方法路径即可。

2.方式二:left join 方式

//查询userImg表 同时关联查询user表,type字段作为搜索条件,此处不同点在于多加了一个name的搜索(稍后解释)
@Select(value="
<script>
  select  a.id,a.url,a.userId,a.type,b.name from userImg  a 
  left join user b on a.userId = b.id
  <where>
    <if test = 'type != null and type != ""'> a.type = #{type}</if>
    <if test = 'name != null and type != ""'> and b.name = #{name}</if>
  </where>
</script>")
List<UserImg> getlist(UserImg userImg);//同样因为用的userImg 实体接受,需要在实体中加上name字段

同样,若需要关联查多张表只需要不停的left join 就可以了。

3.总结:这两种方式的区别和优劣

简述两种方式的区别:

方式一: 本质还是分开单独去查每张表,最后拿到结果返回到实体中。
方式二: 已左表为主,查询各表相关数据然后结合涉及到的字段,排序后,形成新表返
回到实体中。

目前在项目中使用,发现了以下区别和优劣势:

①若关联的表数量较多
  • 方式一相比方式二要快,表越多越明显(left join 个人建议最2张表关联用且数据量不大的时候用一用,个人亲测关联3张表时,20W+数据,自定义了排序,然后查了2分钟)
②在查询功能开发时
  • 方式二比方式一友好,如果涉及到需要查询关联的表中字段时(如上代码,需要查询name时),方式一不能直接查,方式二可以直接查。
目前改善方法:
  • 方式一和方式二结合使用
  • 另外写一个查询方法赋值(感觉很没效率,暂时没找到其他方法,有dalao知道的话,麻烦教一下,谢谢!)
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus是一款MyBatis的增强工具,它提供了很多实用的功能,比如一对多关联查询。在MyBatis-Plus中,一对多关联查询可以通过使用@TableName注解和@TableField注解来实现。 假设我们有两张表,一张是学生表,另一张是课程表,一个学生可以选多门课程,那么我们就可以用一对多关联查询查询某个学生选的所有课程。 首先,在学生表中定义一个属性List<Course> courses,并使用@TableField注解将该属性与课程表的外键关联起来: ``` public class Student { @TableId private Long id; private String name; @TableField(exist = false) private List<Course> courses; } ``` 然后,在课程表中定义一个属性Long studentId,并使用@TableField注解将该属性与学生表的主键关联起来: ``` public class Course { @TableId private Long id; private String name; @TableField("student_id") private Long studentId; } ``` 最后,我们使用MyBatis-Plus提供的wrapper类进行关联查询: ``` QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", studentId); List<Student> students = studentMapper.selectList(queryWrapper); for (Student student : students) { QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>(); courseQueryWrapper.eq("student_id", student.getId()); List<Course> courses = courseMapper.selectList(courseQueryWrapper); student.setCourses(courses); } ``` 以上就是MyBatis-Plus实现一对多关联查询的方法。如果您还有其他问题或需要进一步的帮助,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值