文章目录
1.我的点赞分页展示 1.分页查询工具类 1.PageInfo.java 需要分页查询的就继承它,传值的时候pageNo和pageSize可以为空 2.PageResult.java 根据条件从数据库中查询信息,然后设置这里的四个值即可得到分页查询结果
2.sun-club-application-controller 1.SubjectLikedDTO.java 继承PageInfo 2.SubjectLikedController.java 获取PageNo和PageSize(可以为空,因为PageInfo有默认值)
3.sun-club-domain 1.SubjectLikedBO.java 继承PageInfo 2.SubjectLikedDomainService.java 3.SubjectLikedDomainServiceImpl.java
4.sun-club-infra 1.SubjectLikedService.java 2.SubjectLikedServiceImpl.java 3.SubjectLikedDao.java 4.SubjectLikedDao.xml
5.测试 1.登录 2.点赞 3.使用定时任务将点赞信息同步到数据库 4.查询点赞信息发现查不出来,原因是同步点赞信息时没有设置is_delete=0,设置之后重新测试一遍 5.同步成功 6.total正确了但是没数据,发现是参数写错了,真醉了 7.再次测试,终于对了
2.快速刷题 1.sun-club-application-controller 1.SubjectInfoDTO.java 新增上一题和下一题字段
2.sun-club-domain 1.SubjectInfoBO.java 新增上一题下一题字段 2.SubjectInfoDomainServiceImpl.java 将上一题和下一题的id设置到bo中
3.sun-club-infra 1.SubjectJudgeService.java 2.SubjectInfoServiceImpl.java 3.SubjectInfoDao.java 4.SubjectInfoDao.xml 查询上一题和下一题的sql
4.测试 1.新增同一分类同一标签下的三个题目 2.找到第二个题目的题目id,发送请求 3.查询564得到563和565,没问题
1.我的点赞分页展示
1.分页查询工具类
1.PageInfo.java 需要分页查询的就继承它,传值的时候pageNo和pageSize可以为空
package com. sunxiansheng. subject. application. newpage ;
import java. util. Objects ;
public class PageInfo {
private Integer pageNo = 1 ;
private Integer pageSize = 20 ;
public Integer getPageNo ( ) {
return ( pageNo == null || pageNo < 1 ) ? 1 : pageNo;
}
public Integer getPageSize ( ) {
return ( pageSize == null || pageSize < 1 ) ? 20 : pageSize;
}
public PageInfo setPageNo ( Integer pageNo) {
this . pageNo = pageNo;
return this ;
}
public PageInfo setPageSize ( Integer pageSize) {
this . pageSize = pageSize;
return this ;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
PageInfo pageInfo = ( PageInfo ) o;
return Objects . equals ( pageNo, pageInfo. pageNo) &&
Objects . equals ( pageSize, pageInfo. pageSize) ;
}
@Override
public int hashCode ( ) {
return Objects . hash ( pageNo, pageSize) ;
}
@Override
public String toString ( ) {
return "PageInfo{" +
"pageNo=" + pageNo +
", pageSize=" + pageSize +
'}' ;
}
}
2.PageResult.java 根据条件从数据库中查询信息,然后设置这里的四个值即可得到分页查询结果
package com. sunxiansheng. subject. application. newpage ;
import java. util. Collections ;
import java. util. List ;
import java. util. Objects ;
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 PageResult < T > setPageNo ( Integer pageNo) {
this . pageNo = Objects . requireNonNull ( pageNo, "Page number cannot be null" ) ;
calculateStartAndEnd ( ) ;
return this ;
}
public PageResult < T > setPageSize ( Integer pageSize) {
this . pageSize = Objects . requireNonNull ( pageSize, "Page size cannot be null" ) ;
calculateStartAndEnd ( ) ;
return this ;
}
public PageResult < T > setRecords ( List < T > result) {
this . result = Objects . requireNonNull ( result, "Result list cannot be null" ) ;
return this ;
}
public PageResult < T > setTotal ( Integer total) {
this . total = Objects . requireNonNull ( total, "Total count cannot be null" ) ;
calculateTotalPages ( ) ;
calculateStartAndEnd ( ) ;
return this ;
}
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 getStart ( ) {
return start;
}
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;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
PageResult < ? > that = ( PageResult < ? > ) o;
return Objects . equals ( pageNo, that. pageNo) &&
Objects . equals ( pageSize, that. pageSize) &&
Objects . equals ( total, that. total) &&
Objects . equals ( totalPages, that. totalPages) &&
Objects . equals ( result, that. result) &&
Objects . equals ( start, that. start) &&
Objects . equals ( end, that. end) ;
}
@Override
public int hashCode ( ) {
return Objects . hash ( pageNo, pageSize, total, totalPages, result, start, end) ;
}
@Override
public String toString ( ) {
return "PageResult{" +
"pageNo=" + pageNo +
", pageSize=" + pageSize +
", total=" + total +
", totalPages=" + totalPages +
", result=" + result +
", start=" + start +
", end=" + end +
'}' ;
}
}
2.sun-club-application-controller
1.SubjectLikedDTO.java 继承PageInfo
2.SubjectLikedController.java 获取PageNo和PageSize(可以为空,因为PageInfo有默认值)
@PostMapping ( "/getSubjectLikedPage" )
public Result < PageResult < SubjectLikedDTO > > getSubjectLikedPage ( @RequestBody SubjectLikedDTO subjectLikedDTO) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectController.getSubjectLikedPage.dto:{}" , JSON . toJSONString ( subjectLikedDTO) ) ;
}
SubjectLikedBO subjectLikedBO = SubjectLikedDTOConverter . INSTANCE . convertDTOToBO ( subjectLikedDTO) ;
subjectLikedBO. setPageNo ( subjectLikedDTO. getPageNo ( ) ) ;
subjectLikedBO. setPageSize ( subjectLikedDTO. getPageSize ( ) ) ;
PageResult < SubjectLikedBO > boPageResult = subjectLikedDomainService. getSubjectLikedPage ( subjectLikedBO) ;
return Result . ok ( boPageResult) ;
} catch ( Exception e) {
log. error ( "SubjectCategoryController.getSubjectLikedPage.error:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "分页查询我的点赞失败" ) ;
}
}
3.sun-club-domain
1.SubjectLikedBO.java 继承PageInfo
2.SubjectLikedDomainService.java
PageResult < SubjectLikedBO > getSubjectLikedPage ( SubjectLikedBO subjectLikedBO) ;
3.SubjectLikedDomainServiceImpl.java
@Override
public PageResult < SubjectLikedBO > getSubjectLikedPage ( SubjectLikedBO subjectLikedBO) {
PageResult < SubjectLikedBO > pageResult = new PageResult < > ( ) ;
pageResult. setPageNo ( subjectLikedBO. getPageNo ( ) ) . setPageSize ( subjectLikedBO. getPageSize ( ) ) ;
int offset = ( subjectLikedBO. getPageNo ( ) - 1 ) * subjectLikedBO. getPageSize ( ) ;
int limit = subjectLikedBO. getPageSize ( ) ;
SubjectLiked subjectLiked = SubjectLikedBOConverter . INSTANCE . convertBOToEntity ( subjectLikedBO) ;
subjectLiked. setLikeUserId ( LoginUtil . getLoginId ( ) ) ;
int count = subjectLikedService. countByCondition ( subjectLiked) ;
if ( count == 0 ) {
return pageResult;
}
List < SubjectLiked > subjectLikedList = subjectLikedService. queryPage ( subjectLiked, offset,
limit) ;
List < SubjectLikedBO > subjectInfoBOS = SubjectLikedBOConverter . INSTANCE . convertListInfoToBO ( subjectLikedList) ;
subjectInfoBOS. forEach ( info -> {
SubjectInfo subjectInfo = subjectInfoService. queryById ( info. getSubjectId ( ) ) ;
info. setSubjectName ( subjectInfo. getSubjectName ( ) ) ;
} ) ;
pageResult. setRecords ( subjectInfoBOS) . setTotal ( count) ;
return pageResult;
}
4.sun-club-infra
1.SubjectLikedService.java
int countByCondition ( SubjectLiked subjectLiked) ;
List < SubjectLiked > queryPage ( SubjectLiked subjectLiked, int start, Integer pageSize) ;
2.SubjectLikedServiceImpl.java
@Override
public int countByCondition ( SubjectLiked subjectLiked) {
return this . subjectLikedDao. countByCondition ( subjectLiked) ;
}
@Override
public List < SubjectLiked > queryPage ( SubjectLiked subjectLiked, int offset, Integer limit) {
return this . subjectLikedDao. queryPage ( subjectLiked, offset, offset) ;
}
3.SubjectLikedDao.java
int countByCondition ( SubjectLiked subjectLiked) ;
List < SubjectLiked > queryPage ( @Param ( "entity" ) SubjectLiked subjectLiked,
@Param ( "offset" ) int offset,
@Param ( "limit" ) Integer limit) ;
4.SubjectLikedDao.xml
< select id= "countByCondition" resultType= "java.lang.Integer" >
select count ( 1 )
from subject_liked
where like_user_id = #{ likeUserId} and status = 1
and is_deleted = 0
< / select>
< select id= "queryPage" resultType= "com.sunxiansheng.subject.infra.basic.entity.SubjectLiked" >
select *
from subject_liked
where like_user_id = #{ entity. likeUserId}
and is_deleted = 0 and status = 1
limit #{ offset}
, #{ limit}
< / select>
5.测试
1.登录
2.点赞
3.使用定时任务将点赞信息同步到数据库
4.查询点赞信息发现查不出来,原因是同步点赞信息时没有设置is_delete=0,设置之后重新测试一遍
5.同步成功
6.total正确了但是没数据,发现是参数写错了,真醉了
7.再次测试,终于对了
2.快速刷题
1.sun-club-application-controller
1.SubjectInfoDTO.java 新增上一题和下一题字段
private Long lastSubjectId;
private Long nextSubjectId;
2.sun-club-domain
1.SubjectInfoBO.java 新增上一题下一题字段
private Long lastSubjectId;
private Long nextSubjectId;
2.SubjectInfoDomainServiceImpl.java 将上一题和下一题的id设置到bo中
private void assembleSubjectCursor ( SubjectInfoBO bo, SubjectInfoBO subjectInfoBO) {
Long labelId = subjectInfoBO. getLabelId ( ) ;
Long categoryId = subjectInfoBO. getCategoryId ( ) ;
Long subjectId = subjectInfoBO. getId ( ) ;
if ( Objects . isNull ( labelId) || Objects . isNull ( categoryId) ) {
return ;
}
Long nextSubjectId = subjectInfoService. querySubjectIdCursor ( subjectId, categoryId, labelId, 1 ) ;
bo. setNextSubjectId ( nextSubjectId) ;
Long lastSubjectId = subjectInfoService. querySubjectIdCursor ( subjectId, categoryId, labelId, 0 ) ;
bo. setLastSubjectId ( lastSubjectId) ;
}
3.sun-club-infra
1.SubjectJudgeService.java
Long querySubjectIdCursor ( Long subjectId, Long categoryId, Long labelId, int i) ;
2.SubjectInfoServiceImpl.java
@Override
public Long querySubjectIdCursor ( Long subjectId, Long categoryId, Long labelId, int i) {
return this . subjectInfoDao. querySubjectIdCursor ( subjectId, categoryId, labelId, i) ;
}
3.SubjectInfoDao.java
Long querySubjectIdCursor ( @Param ( "subjectId" ) Long subjectId,
@Param ( "categoryId" ) Long categoryId,
@Param ( "labelId" ) Long labelId,
@Param ( "cursor" ) int cursor) ;
4.SubjectInfoDao.xml 查询上一题和下一题的sql
< select id = " querySubjectIdCursor" resultType = " java.lang.Long" >
select a.id
from subject_info a
join subject_mapping b on a.id = b.subject_id
where b.category_id = #{categoryId}
and b.label_id = #{labelId}
< if test = " cursor != null and cursor == 1" >
and a.id > #{subjectId}
</ if>
< if test = " cursor != null and cursor == 0" >
and a.id < #{subjectId}
</ if>
order by a.id
< if test = " cursor != null and cursor == 0" >
desc
</ if>
limit 1
</ select>
4.测试
1.新增同一分类同一标签下的三个题目
2.找到第二个题目的题目id,发送请求
3.查询564得到563和565,没问题