问题描述
在开发过程中在 mapper.xml 文件中自定义了一个 sql 查询列表的 select 语句,但是由于前端需要的是分页数据,需要在后端将列表查询改成分页查询
修改前
1. 查询数据类型
java
@Data
public class OppRelMoveWrapper implements BaseData {
/**
* ID
*/
private Long id;
/**
* 商机ID
*/
private Long oppId;
/**
* RELATIONSHIP
*/
private String relationship;
/**
* 关系评价附件
*/
private String attachmentRel;
/**
* CHIEF_RECORD
*/
private String chiefRecord;
/**
* 首席代表动态记录附件
*/
private String attachmentChief;
/**
* ORDINARY_RECORD
*/
private String ordinaryRecord;
/**
* 普通动态记录附件
*/
private String attachmentOrdinary;
/**
* 录入人
*/
private Long createStaff;
/**
* 录入时间
*/
private Date createDate;
/**
* 更新时间
*/
private Date updateDate;
}
2. mapper定义
java
@Mapper
public interface OppRelationshipInfoMoveMapper extends BaseMapper<OppRelationshipInfoMove> {
List<OppRelMoveWrapper> queryMoveClientList(@Param("query") OppRelMoveQuery query);
}
3. xml文件定义
该语句查询的是一个返回数据类型为OppRelationshipInfoMoveMap
的列表
xml
<resultMap type="com.wrapper.OppRelMoveWrapper"
id="OppRelationshipInfoMoveMap">
<result property="id" column="ID" jdbcType="INTEGER"/>
<result property="oppId" column="OPP_ID" jdbcType="INTEGER"/>
<result property="relationship" column="RELATIONSHIP" jdbcType="VARCHAR"/>
<result property="attachmentRel" column="attachment_rel" jdbcType="VARCHAR"/>
<result property="chiefRecord" column="CHIEF_RECORD" jdbcType="VARCHAR"/>
<result property="attachmentChief" column="attachment_chief" jdbcType="VARCHAR"/>
<result property="ordinaryRecord" column="ORDINARY_RECORD" jdbcType="VARCHAR"/>
<result property="attachmentOrdinary" column="attachment_ordinary" jdbcType="VARCHAR"/>
<result property="createStaff" column="CREATE_STAFF" jdbcType="INTEGER"/>
<result property="createDate" column="CREATE_DATE" jdbcType="TIMESTAMP"/>
<result property="updateDate" column="UPDATE_DATE" jdbcType="TIMESTAMP"/>
</resultMap>
<select id="queryMoveClientList" resultMap="OppRelationshipInfoMoveMap">
SELECT
ID,
OPP_ID,
RELATIONSHIP,
attachment_rel,
CHIEF_RECORD,
attachment_chief,
ORDINARY_RECORD,
attachment_ordinary,
CREATE_STAFF,
CREATE_DATE,
UPDATE_DATE
FROM opp_relationship_info_move
WHERE 1=1
<if test="query.createDate != null and query.createDate != ''">
AND DATE_FORMAT(CREATE_DATE, '%Y-%m-%d') = #{query.createDate}
</if>
<if test="query.updateDate != null and query.updateDate != ''">
AND DATE_FORMAT(UPDATE_DATE, '%Y-%m-%d') = #{query.updateDate}
</if>
<if test="query.oppId != null and query.oppId != ''">
AND OPP_ID = #{query.oppId}
</if>
ORDER BY CREATE_DATE DESC
</select>
4. 函数使用
java
List<OppRelMoveWrapper> moveWrapperList = oppRelationshipInfoMoveMapper.queryMoveClientList(query);
修改后
修改成分页查询比较简单,直接将 mapper 的查询方法修改即可
1. 修改mapper
将函数返回的数据改成 Page类型
java
@Mapper
public interface OppRelationshipInfoMoveMapper extends BaseMapper<OppRelationshipInfoMove> {
Page<OppRelMoveWrapper> queryMoveClientList(Page<Object> page, @Param("query") OppRelMoveQuery query);
}
2. 函数使用
注意: 构造Page时里面的 pageNum 和 pageSize 一定要存在!!! 否则获得的 Page 中的 records 为空!!!
java
Page<OppRelMoveWrapper> moveWrapperPage = oppRelationshipInfoMoveMapper.queryMoveClientList(
new Page(query.getPage(), query.getPageSize(), true), query);
结语
很香的一个方法,使我的列表查询变成分页查询。