@Controller
public class IndexController {
@Autowired
QuestionService questionService;
@GetMapping("/")
public String index(@RequestParam(name="page",defaultValue = "1") Integer page,
@RequestParam(name="size",defaultValue = "5") Integer size,
Model model) {
PaginationDTO pagination = questionService.list(page,size);
model.addAttribute("pagination",pagination);
return "index";
}
}
2、QuestionService
-----------------
package life.majiang.community.service;
…
@Service
public class QuestionService {
@Autowired
private QuestionMapper questionMapper;
@Autowired
private UserMapper userMapper;
public PaginationDTO list(Integer page, Integer size) {
PaginationDTO paginationDTO = new PaginationDTO();
Integer totalCount = questionMapper.count();
paginationDTO.setPagination(totalCount,page,size);
if(page<1){
page = 1;
}
if(page>paginationDTO.getTotalPage()){
page = paginationDTO.getTotalPage();
}
Integer offset = size * (page-1);
List<Question> questions = questionMapper.list(offset,size);
List<QuestionDTO> questionDTOList = new ArrayList<>();
for (Question question:questions){
User user = userMapper.getUserBySingleName(question.getCreator());
int comments = questionMapper.getComment_count(question);
int views = questionMapper.getView_count(question);
question.setComment_count(comments);
question.setView_count(views);
QuestionDTO questionDTO = new QuestionDTO();
BeanUtils.copyProperties(question,questionDTO);
questionDTO.setUser(user);
questionDTOList.add(questionDTO);
}
paginationDTO.setQuestions(questionDTOList);
return paginationDTO;
}
}
3、QuestionMapper
----------------
package life.majiang.community.mapper;
…
@Mapper
public interface QuestionMapper {
public void insertQuestion(Question question);
public String selectQuestionTitle(Question question);
public void updateQuestion(Question question);
@Select("select * from question limit #{offset},#{size}")
public List<Question> list(@Param(value = "offset") Integer offset,
@Param(value = "size") Integer size);
public Integer count();
public int getComment_count(Question question);
public int getView_count(Question question);
public int getCreatorTitleIsExist(Question question);
@Select("select * from question where creator=#{creator} limit #{offset},#{size}")
List<Question> listByCreator(@Param(value = "creator") String creator, @Param(value = "offset") Integer offset,@Param(value = "size") Integer size);
}
4、QuestionMapper.xml
--------------------
## 最后
以上分享的全部分布式技术专题+面试解析+相关的手写和学习的笔记pdf,**[高清完整版戳这里免费领取](https://gitee.com/vip204888/java-p7)**
还有更多Java笔记分享如下:
ize);
}
4、QuestionMapper.xml
最后
以上分享的全部分布式技术专题+面试解析+相关的手写和学习的笔记pdf,高清完整版戳这里免费领取
还有更多Java笔记分享如下: