【Java进阶营】Mybatis技术专题(1)如何清晰的解决出现「多对一模型」和「一对多模型」的问题

前提介绍

在mybatis如何进行多对一、一对多(一对一)的多表查询呢?本章带你认识如何非常顺滑的解决!

基础使用篇
一对一
association

association通常用来映射一对一的关系,例如,有个类user,对应的实体类如下:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Student {
private int id;
private String name;
/**
* 学生要关联一个老师
*/
private Teacher teacher;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Teacher {
private int id;
private String name;
}

Dao层进行Mapper查询操作

public interface TeacherMapper {
Teacher getTeacher(@Param(“tid”) int id);
Teacher getTeacher2(@Param(“tid”) int id);
}

Dao层进行Mapper.xml文件

select * from student select * from teacher where id = #{id} select s.id sid, s.name sname, t.name tname from student s, teacher t where s.tid = t.id
assocication:可以指定联合的JavaBean对象
    select:指定相关查询结果sqlid
    property="role“:指定哪个属性是联合的对象
    javaType:指定这个属性对象的类型
    column="{javabean熟悉=数据库字段,Javabean属性=数据库字段}"

<association property="role" javaType="com.queen.mybatis.bean.Role">
    <id column="role_id" property="id"/>
    <result column="roleName" property="roleName"/>
</association>

以上如果跨越命名空间的情况下:select:需要用namespace.selectId进行指定。

collection

@Alias(“Student”)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Student {
private int id;
private String name;
private int tid;
}
@Alias(“Teacher”)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Teacher {
private int id;
private String name;
/**
* 一个老师包含多个学生
*/
private List studentList;
}

Dao层进行Mapper查询操作

public interface TeacherMapper {
Teacher getTeacher(@Param(“tid”) int id);
Teacher getTeacher2(@Param(“tid”) int id);
}

Dao层进行Mapper.xml文件

select s.id sid, s.name sname, t.name name, t.id tid from student s, teacher t where s.tid = t.id and t.id = #{tid}
<resultMap id="TeacherStudent2" type="Teacher">
    <collection property="studentList" javaType="ArrayList" ofType="Student"
                select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getTeacher2" resultMap="TeacherStudent2">
    select *
    from teacher
    where id = #{tid}
</select>
<select id="getStudentByTeacherId" resultType="Student">
    select *
    from student
    where tid = #{tid}
</select>

注意:各个表之间尽量不要有重名字段,包括主键id,不然可能会造成数据混乱错误;

JavaType和ofType都是用来指定对象类型的
    property="指的是对象内部(List类型)的属性信息字段名称"
    JavaType是用来指定pojo中属性的类型
    ofType指定的是映射到list集合属性中pojo的类型。
    column="{javabean熟悉=数据库字段,Javabean属性=数据库字段}"
    select:指定相关查询结果sqlid

”特叔“使用篇
一对一映射
实体列 class Tb_blog/TbBlog

private long blogId;
private String blogTitle;
private String blogContent;
private Date createTime;
private  String blogType;
private String sId;

private Tb_author author;
List<TbAuthor> tbAuthorList;

实体类 class TbAuthor

private long id;
private String username;
private String password;
private String email;
private String address;
private String phone;
private TbBlog tbBlog;
private List<TbBlog> tbBlogList;

resultMap标签配置

select * from tb_blog g inner join tb_author r on g.blogId = r.id
在sql加入别名alias与field属性字段一样,也可以自动注入进入。

association标签配置

select * from tb_blog g inner join tb_author r on g.blogId = r.id

collection标签配置
mapper接口定义
AuthorMapper.interface

//!通过id 和映射文件中 association的column属性的值sId关联 来嵌套查询 嵌套查询的第二条sql语句都要写条件来关联第一张表
List selectAuthorandBlogAssociation(int id);

BlogMapper.interface

List selectBlogAndAuthorAssociation();

AuthorMapper.xml

select * from tb_author where id=#{id} select * from tb_author

BlogMapper.xml

<select id="selectBlogAndAuthor" resultType="com.xqh.pojo.TbBlog">
    select * from tb_blog where sId = #{id}
</select>

总结
多表查询一对一映射

association标签

不嵌套 property=当前实体类中的第二种表的属性名 javaType=返回的实体类
嵌套 多加两个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值)在此我向大家推荐一个架构学习交流圈。交流学习指导伪鑫:1253431195(里面有大量的面试题及答案)里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多
多表查询一对多

collection标签

不嵌套 property=当前实体类中的第二种表的属性名 ofType=返回是实体类
property=当前实体类中的第二种表的属性名 javaType=返回的实体类
嵌套 多加两个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值)
2.多表查询一对多
collection标签

不嵌套 property=当前实体类中的第二种表的属性名 ofType=返回是实体类
嵌套 多加一个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值) [ofType = collection一对多嵌套查询 嵌套查询所有结果 不需写返回类型因为 select已经映射]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值