mybatis 对象内包含对象如何查询(一)

mybatis在进行查询时,对象内的属性是另一种对象的数组

同样是用collection标签,先介绍第一种,也是醉实用的方法,每次只要访问一遍数据库就可以将结果全部出来并且帮你封装好

主对象

public class ReachPrivilege {
	private List<ReachPrivilegeDetail> rpds;// 关联对象的集合

	private Long rpId;

	private String rpName;

	private Long beginTime;

	private Long endTime;
//省略get和set方法
}

被关联的对象

public class ReachPrivilegeDetail {
    private Long rpdId;

    private Long rpId;

    private Integer level;

    private Integer require;

    private Integer remissionCash;

    private Integer pinkage;

    private Integer sendPoint;

    private Integer universal;

    private Long couponId;
//省略get和set方法
}

xml的resultMap配置

<resultMap type="com.impression.model.ReachPrivilege" id="rpdsMap">
		<id column="rp_id" property="rpId" jdbcType="BIGINT" />
		<result column="rp_name" property="rpName" jdbcType="VARCHAR" />
		<result column="begin_time" property="beginTime" jdbcType="BIGINT" />
		<result column="end_time" property="endTime" jdbcType="BIGINT" />
         <!--  开始配置关联属性 -->
		<collection property="rpds" javaType="java.util.List"
			ofType="com.impression.model.ReachPrivilegeDetail">
			<id column="rpd_id" property="rpdId" jdbcType="BIGINT" />
			<result column="rpd_rp_id" property="rpId" jdbcType="BIGINT" />
			<result column="rpd_level" property="level" jdbcType="INTEGER" />
			<result column="rpd_require" property="require" jdbcType="INTEGER" />
			<result column="rpd_remission_cash" property="remissionCash"
				jdbcType="INTEGER" />
			<result column="rpd_pinkage" property="pinkage" jdbcType="INTEGER" />
			<result column="rpd_send_point" property="sendPoint" jdbcType="INTEGER" />
			<result column="rpd_universal" property="universal" jdbcType="INTEGER" />
			<result column="rpd_coupon_id" property="couponId" jdbcType="BIGINT" />
		</collection>
</resultMap>

注:这边就关系到数据库的列名规范了,以前我无所谓列名规范,自从用了这个我已经开始严格遵守规范了,对于这个提示,懂了也就懂了吧,不懂得等多踩点坑以后也会懂的

接下来教你如何写sql进行查询

<select id="queryAndRpdsByStatus" resultMap="rpdsMap">
		select
		a.rp_id, a.rp_name, a.begin_time, a.end_time,
		b.rpd_id as rpd_id,
		b.rp_id as rpd_rp_id,
		b.level as rpd_level,
		b.require as rpd_require,
		b.remission_cash as rpd_remission_cash,
		b.pinkage as rpd_pinkage,
		b.send_point as rpd_send_point,
		b.universal as rpd_universal,
		b.coupon_id as rpd_coupon_id
		from im_reach_privilege a
		left join
		im_reach_privilege_detail b
		on a.rp_id=b.rp_id
		where 1=1
		<if test=" status == 1">
			and a.begin_time &gt; #{time,jdbcType=BIGINT}
		</if>
		<if test=" status == 2">
			and a.begin_time &lt; #{time,jdbcType=BIGINT}
			and
			(a.end_time &gt; #{time,jdbcType=BIGINT}
			or a.end_time = 0)
		</if>
		<if test=" status == 3">
			and a.end_time &lt; #{time,jdbcType=BIGINT}
			and a.end_time
			!= 0
		</if>
	</select>

其实就是普通的左联查询语句,只不过以前用左联查询出来的结果是一片,现在mybatis帮你把数据分解好了,根据主对象的id将数据进行分割封装好了给你

转载于:https://my.oschina.net/2892328252/blog/760507

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值