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 > #{time,jdbcType=BIGINT}
</if>
<if test=" status == 2">
and a.begin_time < #{time,jdbcType=BIGINT}
and
(a.end_time > #{time,jdbcType=BIGINT}
or a.end_time = 0)
</if>
<if test=" status == 3">
and a.end_time < #{time,jdbcType=BIGINT}
and a.end_time
!= 0
</if>
</select>
其实就是普通的左联查询语句,只不过以前用左联查询出来的结果是一片,现在mybatis帮你把数据分解好了,根据主对象的id将数据进行分割封装好了给你