使用 layui 后端分页的实例

前端代码

写文章
    #foreach($e in $feeds)
  • $e.title

    $!e.creatorNickName $date.format("yyyy-MM-dd HH:mm:ss",$e.gmtCreate)
  • #end

后端 Controller 代码

    @GetMapping("/")
    @VelocityLayout("/velocity/layout/index.vm")
    fun root(@RequestParam(value = "title", required = false) title: String?,
             @RequestParam(value = "pageNum", defaultValue = "1", required = false) pageNum: Int,
             @RequestParam(value = "pageSize", defaultValue = "10", required = false) pageSize: Int,
             model: Model): String {

        model.addAttribute("feeds", KnowledgeService.page(title, pageNum, pageSize).list)
        model.addAttribute("total", KnowledgeService.page(title, pageNum, pageSize).total)
        model.addAttribute("pageNum", pageNum)
        model.addAttribute("pageSize", pageSize)
        model.addAttribute("title", title)
        return "index"
    }




@Service
@Qualifier("KnowledgeServiceImpl")
class KnowledgeServiceImpl : KnowledgeService {
    override fun page(title: String?, currentPage: Int, limit: Int): PageInfo {
        val orderBy = "id desc"//按照排序: 字段 倒序/排序
        PageHelper.startPage(currentPage, limit, orderBy)
        val list = search(title)
        return PageInfo(list)
    }

    override fun search(title: String?): MutableList {
        if (StringUtils.isEmpty(title)) {
            return KnowledgeMapper.listAll()
        } else {
            return KnowledgeMapper.searchByTile(title)
        }
    }

    @Autowired lateinit var KnowledgeMapper: KnowledgeMapper
}



package com.alibaba.microtek.mapper.microtek;

import com.alibaba.microtek.model.microtek.Knowledge;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public interface KnowledgeMapper {
    int deleteByPrimaryKey(Long id);

    int insert(Knowledge record);

    int insertSelective(Knowledge record);

    Knowledge selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Knowledge record);

    int updateByPrimaryKey(Knowledge record);

    @Select("SELECT * FROM knowledge WHERE title = #{title} limit 1")
    @ResultMap("BaseResultMap")
    Knowledge selectByTitle(String title);

    @Select("SELECT * FROM knowledge")
    @ResultMap("BaseResultMap")
    List listAll();

    @Select("SELECT * FROM knowledge where title like concat('%', #{title}, '%')")
    @ResultMap("BaseResultMap")
    List searchByTile(String title);

    @Select("SELECT count(*) FROM knowledge WHERE whos = #{url}")
    int countByUrl(String url);
}


效果:

image.png

源代码工程在这里:

https://github.com/KotlinSpringBoot/saber

请不吝您的 Star 哦.

你可能感兴趣的:(使用 layui 后端分页的实例)