springboot+mybatisplus+thymeleaf整合分页(带前端和后端代码)

@springboot+mybatisplus+thymeleaf整合分页

最近一直在使用mybatis-plus开发,发现方便的地方挺多的,包括自带的分页插件,而且整合了springboot之后,很多基本的sql都不需要再编写了,但使用thymeleaf实现前端分页的资料非常少,自己总结了一些,供参考

准备

使用sts4(eclipse开发springboot工具)创建一个springboot项目:

  1. pom.xml文件

			com.github.theborakompanioni
			thymeleaf-extras-shiro
			2.0.0
		
		
			com.oracle
			ojdbc8
			19.3.0.0.0
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.projectlombok
			lombok
			true
		
		
			org.springframework.boot
			spring-boot-devtools
			runtime
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
		
			com.baomidou
			mybatis-plus-boot-starter
			3.2.0
		
		
  1. application.properties 对数据源的配置
#pool config
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/库名
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.hikari.connection-timeout=10000
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.maximum-pool-size=100
#配置mybatis日志的打印输出
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

3.model层实体类:使用lombok注解类

import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GoodsCategory implements Serializable{
	private static final long serialVersionUID = 1L;
	@TableId(type = IdType.UUID)
	private String cid;
	private String cname;
}
  1. Mapper 没有其他要求,只需要继承BaseMapper就可以了
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dl.model.GoodsCategory;
@Mapper
public interface GoodsCategoryMapper extends BaseMapper{
}
  1. Service接口及实现类
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dl.model.GoodsCategory;
//接口
public interface GoodsCategoryService {
	IPage selectByPage(int start, int size);//分页显示全部
	IPage searchByPage(int start, int size, String cname);//按cname分页查询
}
//实现类
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService{
	@Autowired
	private GoodsCategoryMapper mapper;
	@Override
	public IPage selectByPage(int start, int size) {
		Page page=new Page<>(start,size);
		mapper.selectPage(page, null);
		return page;
	}
	@Override
	public IPage searchByPage(int start,int size, String cname) {
		Page page=new Page<>(start,size);
		mapper.selectPage(page, new QueryWrapper().like("cname", "%"+cname+"%"));
		return page;
	}
}

6.Controller层

@Controller
@RequestMapping("/back/goodscategory")
public class GoodsCategroyController {
	@Autowired
	private GoodsCategoryService service;

	@RequestMapping("/view")
	//pn是每次传回来的当前页
	public Object view(HttpServletRequest request,
			@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn) {
		IPage page = service.selectByPage(pn, 5);
		request.setAttribute("jumpUrl", "/back/goodscategory/view?pn=");
		//此处得到的page对象,包含了current(当前页),pages(总页数),total(总记录数),records(记录,就是查询到的List集合)
		request.setAttribute("page", page);
		return "/back/category/view";
	}
	@RequestMapping("/searchsubmit")
	public String searchsubmit(String cname, HttpServletRequest request,
			@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn) {
		IPage page = service.searchByPage(pn, 5, cname);
		request.setAttribute("jumpUrl", "/back/goodscategory/searchsubmit?cname="+cname+"&pn=");
		request.setAttribute("page", page);
		return "/back/goodscategory/view";
	}
}

7.前面的后台分页处理,可以在很多其他博客中找到,后面的页面是重点了
8. 单独的分页html页面




当前第页,共页,总记录数
  1. 列表页面
  
序号 类别名称 操作指令

10.查看结果
springboot+mybatisplus+thymeleaf整合分页(带前端和后端代码)_第1张图片

你可能感兴趣的:(thymeleaf分页,thymeleaf分页前端代码)