mybatis-plus一对多分页查询
网上对一对多的total并没有详解,很多存在部分误导,所以这里做简单的实例
我的表分为 客户表 和客户家庭表
T_CUSTOMER_CLIENT ,T_CUSTOMER_CLIENTFAMILY 表联系为clientID
1.xml
<!-- 根据条件查询内勤客户-->
<select id="selectCcbClients" resultMap="customerClientMap">
select a.*,b.clientFamilyId,b.clientId as sclientId,b.familyName,b.familyBirthDay,b.relation,b.yearIncome as syearIncome,b.clientCode as sclientCode from T_CUSTOMER_CLIENT a left join T_CUSTOMER_CLIENTFAMILY b
on a.clientId=b.clientId
<where>
<if test="client.clientName != '' and client.clientName != null">
AND clientName LIKE '%' || #{client.clientName} || '%'
</if>
<if test="client.mobile !='' and client.mobile!=null">
AND a.mobile=#{client.mobile}
</if>
</where>
</select>
``
2.一对多关系resultMap 相同字段采用别名
<resultMap id="customerClientMap" type="com.ccblife.yb.support.bean.entity.customer.CustomerClientVO">
<id column="clientId" property="clientId" jdbcType="BIGINT" />
<result column="clientCode" property="clientCode" jdbcType="VARCHAR" />
<result column="clientName" property="clientName" jdbcType="VARCHAR" />
<result column="pinYin" property="pinYin" jdbcType="VARCHAR" />
<result column="empCode" property="empCode" jdbcType="VARCHAR" />
<result column="empId" property="empId" jdbcType="BIGINT" />
<result column="MOBILE" property="mobile" jdbcType="VARCHAR" />
<result column="birthDay" property="birthDay" jdbcType="TIMESTAMP" />
<result column="SEX" property="sex" jdbcType="INTEGER" />
<result column="clientType" property="clientType" jdbcType="INTEGER" />
<result column="labelId" property="labelId" jdbcType="BIGINT" />
<result column="idCardNo" property="idCardNo" jdbcType="VARCHAR" />
<result column="ocpId" property="ocpId" jdbcType="VARCHAR" />
<result column="OCP" property="ocp" jdbcType="VARCHAR" />
<result column="POSITION" property="position" jdbcType="VARCHAR" />
<result column="COMPANY" property="company" jdbcType="VARCHAR" />
<result column="companyAddress" property="companyAddress" jdbcType="VARCHAR" />
<result column="homeAddress" property="homeAddress" jdbcType="VARCHAR" />
<result column="yearIncome" property="yearIncome" jdbcType="INTEGER" />
<result column="HOBBY" property="hobby" jdbcType="VARCHAR" />
<result column="delType" property="delType" jdbcType="INTEGER" />
<result column="labelName" property="labelName" jdbcType="VARCHAR" />
<result column="createTime" property="createTime" jdbcType="TIMESTAMP" />
<result column="createUser" property="createUser" jdbcType="INTEGER" />
<result column="updateUser" property="updateUser" jdbcType="INTEGER" />
<result column="updateTime" property="updateTime" jdbcType="TIMESTAMP" />
<result column="BEFOR7BIRTHDAY" property="befor7birthday" jdbcType="VARCHAR" />
<result column="isRemind" property="isRemind" jdbcType="INTEGER" />
<result column="SOCAL" property="socal" jdbcType="INTEGER" />
<result column="birthDayStr" property="birthDayStr" jdbcType="VARCHAR" />
<result column="isAdd" property="isAdd" jdbcType="INTEGER" />
<result column="sexStr" property="sexStr" jdbcType="VARCHAR" />
<result column="ISCCB" property="isccb" jdbcType="INTEGER" />
<result column="reMark" property="reMark" jdbcType="VARCHAR" />
<result column="isModify" property="isModify" jdbcType="INTEGER" />
<result column="cardNoType" property="cardNoType" jdbcType="VARCHAR" />
<result column="cardNoDate" property="cardNoDate" jdbcType="VARCHAR" />
<result column="taskId" property="taskId" jdbcType="INTEGER" />
<result column="isFinacial" property="isFinacial" jdbcType="INTEGER" />
<collection property="familys" ofType="com.ccblife.yb.support.bean.entity.customer.CustomerClientfamilyVO" column="clientId">
<id column="CLIENTFAMILYID" property="clientFamilyId" jdbcType="BIGINT" /><!-- 这里的column对应的是下面查询的别名,而不是表字段名 -->
<result column="sCLIENTID" property="clientId" ></result>
<result column="familyName" property="familyName" jdbcType="VARCHAR"/><!-- property对应JavaBean中的属性名 -->
<result column="familyBirthDay" property="familyBirthDay" jdbcType="TIMESTAMP"/>
<result column="relation" property="relation" jdbcType="VARCHAR"/>
<result column="sclientCode" property="clientCode" jdbcType="VARCHAR"/>
<result column="sdelType" property="delType" jdbcType="INTEGER"/>
<result column="syearIncome" property="yearIncome" jdbcType="INTEGER"/>
</collection>
</resultMap>
``
3.mapper
IPage<CustomerClientVO> selectCcbClients(IPage page, @Param("client") CustomerClientVO customerClientVo);
4.server
public IPage<CustomerClientVO> selectClientPage(int pageNo, int pageSize, CustomerClientVO customerClientVo) {
IPage<CustomerClientVO> page = new Page<CustomerClientVO>(pageNo, pageSize);
page=customerClientMapper.selectCcbClients(page,customerClientVo);
//因为一对多关系,page中total是错误的,所以这里做你的总条数查询total查询,我就偷下懒,直接设置了
page.setTotal(100);
return page;
}
5.测试结果,图片太大,total就为刚才设置的100.
欢迎指导讨论----------,