Mybaits 多表查询【mybaits】

多表查询

在这里插入图片描述

1、多对一处理

多对一:

  • 多个账户对应一个用户
  • 对于账户而言,关联…多个账户,关联一个用户【多对一】
  • 对于用户而言,集合,一个用户,有很多账号【一对多】

SQL:

//用户表
DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(32) NOT NULL COMMENT '用户名称',
  `birthday` datetime default NULL COMMENT '生日',
  `sex` char(1) default NULL COMMENT '性别',
  `address` varchar(256) default NULL COMMENT '地址',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



insert  into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (41,'老王','2018-02-27 17:47:08','男','北京'),
																       (42,'小二王','2018-03-02 15:09:37','女','北京金燕龙'),
																	   (43,'小二王','2018-03-04 11:34:34','女','北京金燕龙'),
																	   (45,'传智播客','2018-03-04 12:04:06','男','北京金燕龙'),
																	   (46,'老王','2018-03-07 17:37:26','男','北京'),
																	   (48,'小马宝莉','2018-03-08 11:44:00','女','北京修正');
																	



//账户表
DROP TABLE IF EXISTS `account`;

CREATE TABLE `account` (
  `ID` int(11) NOT NULL COMMENT '编号',
  `UID` int(11) default NULL COMMENT '用户编号',
  `MONEY` double default NULL COMMENT '金额',
  PRIMARY KEY  (`ID`),
  KEY `FK_Reference_8` (`UID`),
  CONSTRAINT `FK_Reference_8` FOREIGN KEY (`UID`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



insert  into `account`(`ID`,`UID`,`MONEY`) values (1,46,1000),(2,45,1000),(3,46,2000);

测试环境搭建

1.新建实体类 User,Account
2.建立Mapper接口
3.建立Mapper.xml文件
4.在核心文件中绑定注册 Mapper接口或者文件!【方式很多】
5.测试查询是否成功
在这里插入图片描述
在这里插入图片描述

按照查询嵌套处理

<!--
	思路:
	    1.查询所有的账户信息
	    2.根据查询出来的账号的uid,寻找对应的用户!  子查询
-->
<select id="getAccount" resultMap="AccountUser">
    select * from account
</select>

<resultMap id="AccountUser" type="account">
    <result property="id" column="id"/>
    <result property="uid" column="uid"/>
    <result property="money" column="money"/>
    <association property="user" column="uid" javaType="user" select="getUser"/>
</resultMap>

<select id="getUser" resultType="user">
    select * from user where id=#{id}
</select>

按照结果嵌套处理

<resultMap id="AccountUserMap" type="account">
    <id property="id" column="aid"></id>
    <result property="uid" column="uid"></result>
    <result property="money" column="money"></result>
    <!--一对一关系映射,封装user内容-->
    <association property="user" column="uid" javaType="user">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
    </association>
</resultMap>

<!-- 联表查询 -->
<select id="findAll" resultMap="AccountUserMap">
    select u.*,a.id as aid,a.uid,a.money from  account a,user u where u.id=a.uid
</select>

Mysql一对多查询方式:

  • 子查询
  • 联表查询

2、一对多处理

  • 对于用户而言,集合,一个用户,有很多账号【一对多】

环境搭建:
同上…

实体类
在这里插入图片描述
在这里插入图片描述

按照结果嵌套处理

<!--定义User的resultMap-->
 <resultMap id="userAccount" type="user">
     <id property="id" column="id"></id>
     <result property="username" column="username"></result>
     <result property="address" column="address"></result>
     <result property="sex" column="sex"></result>
     <result property="birthday" column="birthday"></result>
     <!--配置user对象中accounts集合的映射-->
     <collection property="accounts" ofType="account">
         <id property="id" column="aid"></id>
         <result property="uid" column="uid"></result>
         <result property="money" column="money"></result>
     </collection>
 </resultMap>


 <!-- 配置查询所有 -->
 <select id="findAll" resultMap="userAccount">
     SELECT u.*,a.id as aid,a.uid,a.money FROM `user` u LEFT OUTER JOIN account a ON u.id=a.UID
 </select>

按照查询嵌套处理

<!-- 配置查询所有 子查询-->
<select id="findAll2" resultMap="userAccount2">
    SELECT * FROM `user`
</select>

<resultMap id="userAccount2" type="user">
    <collection property="accounts" javaType="ArrayList" ofType="account" select="getAcountsByUserId" column="id"/>
</resultMap>

<select id="getAcountsByUserId" resultType="account">
    select * from account where uid=#{uid}
</select>

小结

1.关联 - association【多对一】
2.集合 - collection 【一对多】
3.javaType & ofType

  • javaType 用来指定实体类中属性的类型
  • ofType 用来指定映射到List或者集合中的pojo类型,泛型中的约束类型

注意点:

  • 保证SQL的可读性,保证通俗易懂
  • 注意一对多和多对一中,属性名和字段的问题
  • 排查问题,建议使用 Log4j
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mybatis是一种流行的Java持久层框架,可以对数据库进行操作。在mybatis中,实现一对多关联查询可以使用两种方式:嵌套查询和嵌套结果映射。嵌套查询是指在主查询中执行子查询来获取关联对象的数据,而嵌套结果映射是指在主查询的结果映射中包含关联对象的映射。 对于嵌套查询,可以使用select标签在主查询中执行子查询。例如,假设有一个班级和学生的关联关系,可以使用如下的SQL语句进行一对多关联查询: ``` <select id="getBanjiWithStudents" resultType="Banji"> SELECT * FROM Banji WHERE id = #{id} </select> <select id="getStudentsByBanjiId" resultType="Student"> SELECT * FROM Student WHERE banji_id = #{id} </select> ``` 然后,在Mapper接口中定义对应的方法: ``` public interface BanjiMapper { Banji getBanjiWithStudents(int id); } ``` 在配置文件中进行映射: ``` <mapper namespace="com.example.BanjiMapper"> <select id="getBanjiWithStudents" resultType="Banji"> SELECT * FROM Banji WHERE id = #{id} </select> <select id="getStudentsByBanjiId" resultType="Student"> SELECT * FROM Student WHERE banji_id = #{id} </select> </mapper> ``` 然后可以通过调用`getBanjiWithStudents`方法来进行一对多关联查询。 对于嵌套结果映射,可以使用association和collection标签来进行配置。例如: ``` <resultMap id="banjiResultMap" type="Banji"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="students" ofType="Student"> <id property="id" column="student_id"/> <result property="name" column="student_name"/> </collection> </resultMap> ``` 然后,在Mapper接口中定义对应的方法: ``` public interface BanjiMapper { Banji getBanjiWithStudents(int id); } ``` 在配置文件中进行映射: ``` <mapper namespace="com.example.BanjiMapper"> <resultMap id="banjiResultMap" type="Banji"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="students" ofType="Student"> <id property="id" column="student_id"/> <result property="name" column="student_name"/> </collection> </resultMap> <select id="getBanjiWithStudents" resultMap="banjiResultMap"> SELECT b.id, b.name, s.id as student_id, s.name as student_name FROM Banji b LEFT JOIN Student s ON b.id = s.banji_id WHERE b.id = #{id} </select> </mapper> ``` 同样可以通过调用`getBanjiWithStudents`方法来进行一对多关联查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值