文章目录
1.sun-club-common封装分页 1.com/sunxiansheng/subject/common/eneity/PageInfo.java 2.com/sunxiansheng/subject/common/eneity/PageResult.java
2.sun-club-application-controller 1.SubjectInfoDTO.java 继承PageInfo并新增字段 2.SubjectController.java
3.sun-club-domain 1.SubjectInfoBO.java 继承PageInfo并增加字段 2.SubjectInfoDomainService.java 3.SubjectInfoDomainServiceImpl.java
4.sun-club-infra 1.SubjectInfoService.java 2.SubjectInfoServiceImpl.java 3.SubjectInfoDao.java 4.SubjectInfoDao.xml 5.测试
1.sun-club-common封装分页
1.com/sunxiansheng/subject/common/eneity/PageInfo.java
package com. sunxiansheng. subject. common. eneity ;
public class PageInfo {
private Integer pageNo = 1 ;
private Integer pageSize = 20 ;
public Integer getPageNo ( ) {
if ( pageNo == null || pageNo < 1 ) {
return 1 ;
}
return pageNo;
}
public Integer getPageSize ( ) {
if ( pageSize == null || pageSize < 1 || pageSize > Integer . MAX_VALUE ) {
return 20 ;
}
return pageSize;
}
public void setPageNo ( Integer pageNo) {
this . pageNo = pageNo;
}
public void setPageSize ( Integer pageSize) {
this . pageSize = pageSize;
}
}
2.com/sunxiansheng/subject/common/eneity/PageResult.java
package com. sunxiansheng. subject. common. eneity ;
import java. util. Collections ;
import java. util. List ;
public class PageResult < T > {
private Integer pageNo = 1 ;
private Integer pageSize = 20 ;
private Integer total = 0 ;
private Integer totalPages = 0 ;
private List < T > result = Collections . emptyList ( ) ;
private Integer start = 1 ;
private Integer end = 0 ;
public void setPageNo ( Integer pageNo) {
this . pageNo = pageNo;
calculateStartAndEnd ( ) ;
}
public void setPageSize ( Integer pageSize) {
this . pageSize = pageSize;
calculateStartAndEnd ( ) ;
}
public void setRecords ( List < T > result) {
this . result = result;
}
public void setTotal ( Integer total) {
this . total = total;
calculateTotalPages ( ) ;
calculateStartAndEnd ( ) ;
}
private void calculateTotalPages ( ) {
if ( this . pageSize > 0 ) {
this . totalPages = ( this . total / this . pageSize) + ( this . total % this . pageSize == 0 ? 0 : 1 ) ;
} else {
this . totalPages = 0 ;
}
}
private void calculateStartAndEnd ( ) {
if ( this . pageSize > 0 ) {
this . start = ( this . pageNo - 1 ) * this . pageSize + 1 ;
this . end = Math . min ( this . pageNo * this . pageSize, this . total) ;
} else {
this . start = 1 ;
this . end = this . total;
}
}
public Integer getDBStart ( ) {
return start - 1 ;
}
public Integer getPageSize ( ) {
return pageSize;
}
public Integer getPageNo ( ) {
return pageNo;
}
public Integer getTotal ( ) {
return total;
}
public Integer getTotalPages ( ) {
return totalPages;
}
public List < T > getResult ( ) {
return result;
}
public Integer getEnd ( ) {
return end;
}
public Integer getStart ( ) {
return start;
}
}
2.sun-club-application-controller
1.SubjectInfoDTO.java 继承PageInfo并新增字段
package com. sunxiansheng. subject. application. dto ;
import com. sunxiansheng. subject. common. eneity. PageInfo ;
import lombok. Data ;
import java. io. Serializable ;
import java. util. List ;
@Data
public class SubjectInfoDTO extends PageInfo implements Serializable {
private static final long serialVersionUID = - 99877276843752542L ;
private Long id;
private String subjectName;
private Integer subjectDifficult;
private Integer subjectType;
private Integer subjectScore;
private String subjectParse;
private String subjectAnswer;
private List < Long > categoryIds;
private List < Long > labelIds;
private List < SubjectAnswerDTO > optionList;
private Long categoryId;
private Long labelId;
}
2.SubjectController.java
@PostMapping ( "/getSubjectPage" )
public Result < PageResult < SubjectInfoDTO > > getSubjectPage ( @RequestBody SubjectInfoDTO subjectInfoDTO) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectController getSubjectPage SubjectInfoDTO, subjectInfoDTO:{}" , JSON . toJSONString ( subjectInfoDTO) ) ;
}
Preconditions . checkNotNull ( subjectInfoDTO. getCategoryId ( ) , "分类id不能为空" ) ;
Preconditions . checkNotNull ( subjectInfoDTO. getLabelId ( ) , "标签id不能为空" ) ;
SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter . INSTANCE . convertDTO2BO ( subjectInfoDTO) ;
subjectInfoBO. setPageNo ( subjectInfoDTO. getPageNo ( ) ) ;
subjectInfoBO. setPageSize ( subjectInfoDTO. getPageSize ( ) ) ;
PageResult < SubjectInfoBO > boPageResult = subjectInfoDomainService. getSubjectPage ( subjectInfoBO) ;
return Result . ok ( boPageResult) ;
} catch ( Exception e) {
log. error ( "SubjectController getSubjectPage error, subjectInfoDTO:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "分页查询题目列表失败" ) ;
}
}
3.sun-club-domain
1.SubjectInfoBO.java 继承PageInfo并增加字段
package com. sunxiansheng. subject. domain. entity ;
import com. sunxiansheng. subject. common. eneity. PageInfo ;
import lombok. Data ;
import java. io. Serializable ;
import java. util. List ;
@Data
public class SubjectInfoBO extends PageInfo implements Serializable {
private static final long serialVersionUID = 1L ;
private Long id;
private String subjectName;
private Integer subjectDifficult;
private Integer subjectType;
private Integer subjectScore;
private String subjectParse;
private String subjectAnswer;
private List < Long > categoryIds;
private List < Long > labelIds;
private List < SubjectAnswerBO > optionList;
private Long categoryId;
private Long labelId;
}
2.SubjectInfoDomainService.java
PageResult < SubjectInfoBO > getSubjectPage ( SubjectInfoBO subjectInfoBO) ;
3.SubjectInfoDomainServiceImpl.java
@Override
public PageResult < SubjectInfoBO > getSubjectPage ( SubjectInfoBO subjectInfoBO) {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectInfoDomainServiceImpl getSubjectPage SubjectInfoBO, SubjectInfoBO:{}" , subjectInfoBO) ;
}
PageResult < SubjectInfoBO > pageResult = new PageResult < > ( ) ;
pageResult. setPageNo ( subjectInfoBO. getPageNo ( ) ) ;
pageResult. setPageSize ( subjectInfoBO. getPageSize ( ) ) ;
SubjectInfo subjectInfo = SubjectInfoConverter . INSTANCE . convertBoToSubjectInfo ( subjectInfoBO) ;
int count = subjectInfoService. countByCondition ( subjectInfo, subjectInfoBO. getCategoryId ( ) , subjectInfoBO. getLabelId ( ) ) ;
if ( count == 0 ) {
return pageResult;
}
pageResult. setTotal ( count) ;
List < SubjectInfo > subjectInfoList = subjectInfoService. queryPage ( subjectInfo, subjectInfoBO. getCategoryId ( ) , subjectInfoBO. getLabelId ( ) ,
pageResult. getDBStart ( ) , pageResult. getPageSize ( ) ) ;
List < SubjectInfoBO > subjectInfoBOS = SubjectInfoConverter . INSTANCE . convertSubjectInfoToBo ( subjectInfoList) ;
pageResult. setRecords ( subjectInfoBOS) ;
return pageResult;
}
4.sun-club-infra
1.SubjectInfoService.java
int countByCondition ( SubjectInfo subjectInfo, Long categoryId, Long labelId) ;
List < SubjectInfo > queryPage ( SubjectInfo subjectInfo, Long categoryId, Long labelId, Integer start, Integer pageSize) ;
2.SubjectInfoServiceImpl.java
@Override
public int countByCondition ( SubjectInfo subjectInfo, Long categoryId, Long labelId) {
return subjectInfoDao. countByCondition ( subjectInfo, categoryId, labelId) ;
}
@Override
public List < SubjectInfo > queryPage ( SubjectInfo subjectInfo, Long categoryId, Long labelId, Integer start, Integer pageSize) {
return subjectInfoDao. queryPage ( subjectInfo, categoryId, labelId, start, pageSize) ;
}
3.SubjectInfoDao.java
int countByCondition ( @Param ( "subjectInfo" ) SubjectInfo subjectInfo,
@Param ( "categoryId" ) Long categoryId,
@Param ( "labelId" ) Long labelId) ;
List < SubjectInfo > queryPage ( @Param ( "subjectInfo" ) SubjectInfo subjectInfo,
@Param ( "categoryId" ) Long categoryId,
@Param ( "labelId" ) Long labelId,
@Param ( "start" ) Integer start,
@Param ( "pageSize" ) Integer pageSize) ;
4.SubjectInfoDao.xml
< select id = " countByCondition" resultType = " java.lang.Integer" >
select count(1)
from subject_info a,
subject_mapping b
where a.id = b.subject_id
and a.is_deleted = 0
and b.is_deleted = 0
and a.subject_difficult = #{subjectInfo.subjectDifficult}
and b.category_id = #{categoryId}
and b.label_id = #{labelId}
</ select>
< select id = " queryPage" resultMap = " SubjectInfoMap" >
select a.id,
a.subject_name,
a.subject_difficult,
a.settle_name,
a.subject_type,
a.subject_score,
a.subject_parse,
a.created_by,
a.created_time,
a.update_by,
a.update_time,
a.is_deleted
from subject_info a,
subject_mapping b
where a.id = b.subject_id
and a.is_deleted = 0
and b.is_deleted = 0
and a.subject_difficult = #{subjectInfo.subjectDifficult}
and b.category_id = #{categoryId}
and b.label_id = #{labelId}
limit #{start}, #{pageSize}
</ select>
5.测试