mybatis多表查询和动态sql

动态sql(掌握)

	首先需要使用if标签进行条件判断,test属性作为判断条件。当test的值为true时,标签体就拼接到sql主体中。
		<if test="userName != null">
		    and username = #{userName}
		</if>
	然后,使用where标签可以省去恒等式的编写。注意:mybatis会自动在第一个条件语句前方添加where关键字
		select * from user 
		<where>
		    <if test="userName != null">
		        and username = #{userName}
		    </if>
		    <if test="userSex != null">
		        and sex = #{userSex}
		    </if>
		</where>
	如果有集合类型的参数需要遍历,使用foreach标签
		<foreach collection="ids" open="and id in (" item="uid" separator="," close=")">
		属性:
			collection:指定集合名称
			open:指定sql语句开始部分
			close:指定sql语句的结尾部分
			separator:分隔符,代表每个元素之间用什么字符分隔。
			item:循环时每次循环取出的元素存入item指定的变量

mybatis多表查询*****

一对一
多对一(在mybatis中看成一对一)
	1.实体类中包含了另一个实体类的对象
	2.查询时,通过多表关联,查询两张表中的数据。内连接
	3.通过配置,把结果集中的数据和实体类中的数据的映射关系指定。
		<resultMap id="accountUserMap" type="account">                      type:指定的是结果集最终封装到哪个实体类中,就是结果的数据类型
		    <id property="id" column="aid"></id>                            主键列封装到哪个属性中,封装到Account对象中
		    <result property="uid" column="uid"></result>                   非主键列封装到哪个属性中,封装到Account对象中
		    <result property="money" column="money"></result>               非主键列封装到哪个属性中,封装到Account对象中
		    <!-- 一对一的关系映射:配置封装user的内容-->              ------以下的字段都是User对象中的-------
		    <association property="user" javaType="user">                    association标签代表的就是一对一的关系指定。property属性指定account中的user实体的属性名称(变量名)。javaType就是User对象的类型(实际数据类型)
		        <id property="id" column="id"></id>
		        <result column="username" property="username"></result>      主键列封装到哪个属性中,封装到User对象中
		        <result column="address" property="address"></result>        非主键列封装到哪个属性中,封装到User对象中
		        <result column="sex" property="sex"></result>                非主键列封装到哪个属性中,封装到User对象中
		        <result column="birthday" property="birthday"></result>      非主键列封装到哪个属性中,封装到User对象中
		    </association>
		</resultMap>
	4.在select标签上,通过resultMap指定上面定义的resultMap的id
一对多
多对多(在mybatis中看成两个一对多)
	1.实体类中包含了另一个实体类对象的集合
	2.查询时,通过多表关联,查询两张表中的数据。关联方式要使用外连接
	3.通过配置,把结果集中的数据和实体类中的数据的映射关系指定。
		<resultMap id="userAccountMap" type="user">                         type:指定的是结果集最终封装到哪个实体类中,就是结果的数据类型
		     <id property="id" column="id"></id>                            主键列封装到哪个属性中,封装到User对象中
		     <result property="username" column="username"></result>        非主键列封装到哪个属性中,封装到User对象中
		     <result property="address" column="address"></result>          非主键列封装到哪个属性中,封装到User对象中
		     <result property="sex" column="sex"></result>                  非主键列封装到哪个属性中,封装到User对象中
		     <result property="birthday" column="birthday"></result>        非主键列封装到哪个属性中,封装到User对象中
		     <!-- 配置user对象中accounts集合的映射 -->         ------以下的字段都是Account对象(Account存在于List集合中)中的-------
		     <collection property="accounts" ofType="account">              collection标签表示user中有一个集合属性,property指定集合的属性名(变量名),ofType指定集合的泛型
		         <id column="aid" property="id"></id>                       主键列封装到哪个属性中,封装到Account对象中
		         <result column="uid" property="uid"></result>              非主键列封装到哪个属性中,封装到Account对象中
		         <result column="money" property="money"></result>          非主键列封装到哪个属性中,封装到Account对象中
		     </collection>
		</resultMap>
	4.在select标签上,通过resultMap指定上面定义的resultMap的id
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值