【Mybatis 框架(自学)】Day07(重点)--2022/3/15

复杂查询环境搭建


多对一处理

多对一:即 关联

一对多:即 集合

联立查询

联立查询的最关键点就是SQL语句的建立,这是第一步,再根据查询的SQL语句对实体类接口进行方法的封装,最后传入映射器Mapper里

SELECT * FROM tbstudent
SELECT * FROM tbinfo.i,tbuser.u WHERE i.userid = u.id

正常的单表查询数据是直接获取的,如果想要联表查询,则需要将两张表取别名,再通过一对多或者多对一的字段进行匹配,最后查询出来

按照查询嵌套处理:子查询

<!--
	Info表字段:id,name,gender,phone,userid
		实体类:id,name,gender,phone,teacher

	User表字段:id,username,password,role
		实体类:id,username,password,role

	如果想将两张表一起查询,那么得想办法利用上实体类当中的userid字段!!!
-->

<!--  1、先建立查询  -->
<select id="getInfo" resultMap="InfoToUser">
	select * from tbinfo
</select>

<select id="getUser" resultType="User">
	select * from tbuser where id = #{userid}
</select>
<!--因为查询出来的结果与Info实体类当中的字段对应,所以得想办法把getUser()的方法给传入到getInfo当中-->

<!--  2、映射结果集  -->
<resultMap id="InfoToUser" type="Info">
	<id property="id" column="id"></id>
    <result property="name" column="name"></result>
    <result property="gender" column="gender"></result>
    <!--
		前三步操作皆是将数据库表中的字段映射,但最后的teacher属于一个对象
		
		则需要进行特殊处理
		
		需要注意的是:
					association:针对于 对象
					collection:则针对于 集合
	-->
    <association property="user" colum="userid" JavaType="User" select="getUser"></association>
</resultMap>

<!--
	Info表字段:id,name,gender,phone,userid
		实体类:id,name,gender,phone,teacher

	首先将getUser()的方法写出,并建立结果集的映射
	然后将userid这一字段中传入user表中的字段,column="userid"
	又因为传入的是个对象(Info实体类中的teacher),所以property的参数为user对象,JavaType的参数则为实体类
	最后传入字段需要方法,select的参数为getUser
-->

按照结果嵌套处理:联立查询

<!--
	分析完上面的子查询,再来做联立查询
-->

<!--  建立查询,直接映射结果集  -->
<select id="getInfo2" resultMap="InfoToUser2">
	SELECT i.id as iid,i.name as iname,i.gender as igender,i.phone as iphone,
           u.username as uusername,u.password as upassword,u.role as urole
    FROM tbinfo i,tbuser u 
    WHERE i.userid = u.id
</select>

<resultMap id="InfoToUser2" type="Info">
	<id property ="id" column="iid"></id>
    <result property ="name" column="iname"></result>
    <result property ="gender" column="igender"></result>
    <result property ="phone" column="iphone"></result>
    
    <association property ="user" JavaType="User">
    	<result property ="username" column="uusername"></result>
        <result property ="password" column="upassword"></result>
        <result property ="role" column="urole"></result>
    </association>
</resultMap>
<!--
	此方法虽然简单,但是不适合多个表的多字段查询,sql语句很难去写

	注意:当SQL语句出现别名的时候,查询出来的列也是以别名显示的,
	所以在映射结果集的时候列名这一行一定要与SQL的别名对应

	SQL语句的别名命名也可以不用加“as”,但为了可读性,便加入其中了	
-->

多对一中使用的查询方式:

  • 子查询

  • 联立查询


一对多处理

按照查询嵌套处理:子查询

<!--步骤同上,关键是用:collection-->

<collection property="infoList" ofType="Info">
     <result property="name" column="iname"></result>
     <result property="gender" column="igender"></result>
     <result property="phone" column="iphone"></result>
</collection>

按照结果嵌套处理:联立查询

<!--步骤同上,一样是:collection-->

<collection property="infoList" ofType="Info" column="id" select="getInfo" 		                     javaType="ArrayList">
</collection>

总结

关于collection、association的理解

  • Collection:集合,当实体类中有ArrayList的存在,则需要用它来进行映射 【一对多】
  • Association:关联,当实体类中有对象的存在,则需要用它进行映射 【多对一】

关于ofType、javaType的理解

  • JavaType:指定实体类中属性的类型
  • ofType:指定映射到List或集合中的pojo类型,泛型中的约束类型
posted @   VVS,冷漠的心  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

囚鸟该如何超越今生?

点击右上角即可分享
微信分享提示