怎么在springboot中利用thymeleaf模板实现一个paginate分页功能
今天就跟大家聊聊有关怎么在springboot中利用thymeleaf模板实现一个paginate分页功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联建站长期为上千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为罗田企业提供专业的网站制作、成都网站制作,罗田网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
pom.xml 加入
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf org.mybatis mybatis-spring 1.2.2 org.mybatis mybatis 3.2.8 org.mybatis.generator mybatis-generator-core 1.3.2 com.github.pagehelper pagehelper 3.6.3 org.apache.tomcat tomcat-jdbc mysql mysql-connector-java
application.properties文件
spring.datasource.platform=mysql spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=com.mysql.jdbc.Driver # Advanced configuration... spring.datasource.max-active=50 spring.datasource.max-idle=6 spring.datasource.min-idle=2 spring.datasource.initial-size=6 #create table spring.jpa.hibernate.ddl-auto=validate spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false
启动类 Application.java
@SpringBootApplication @MapperScan("com.boot.mapper") public class Application { //定义一个全局的记录器,通过LoggerFactory获取 private final static Logger logger = LoggerFactory.getLogger(Application.class); //----------------------------mybaits配置start------------------------------------------- //DataSource @Bean @ConfigurationProperties(prefix="spring.datasource") public DataSource dataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } //SqlSessionFactory @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } //------------------------------mybaits配置end--------------------------------- public static void main(String[] args){ System.out.println("Hello World!"); SpringApplication.run(Application.class, args); } }
以一个简单的user对象为例
private Integer id; private String name; private String password;
controller层
@RestController public class UserController { @Autowired UserService uSer; @RequestMapping("userlist") public Object userlist(@RequestParam(value="pageon",defaultValue="1")int pageon ,ModelAndView mv){ mv.addAllObjects(uSer.UserList(pageon)); return mv; } }
service层
public MapUserList(int pageon) { // TODO Auto-generated method stub Map map=new HashMap (); Page page=new Page(pageon); page.setRowcountAndCompute(userMapper.selectPageListCount(null)); map.put("page", page); map.put("list", userMapper.selectPageList(map)); return map; }
UserMapper.xml
到此时后台代码结束
附加个page工具类
package com.boot.utils; import java.io.Serializable; public class Page implements Serializable { /** * */ private static final long serialVersionUID = 1L; public int getPageon() { return pageon; } public void setPageon(int pageon) { this.pageon = pageon; } public int getRowcount() { return rowcount; } public void setRowcount(int rowcount) { this.rowcount = rowcount; } public int getPagecount() { return pagecount; } public void setPagecount(int pagecount) { this.pagecount = pagecount; } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public Page(int pageon, int row, int rowcount) { pageNumber = 11; this.pageon = pageon; this.row = row; this.rowcount = rowcount; compute(); } public Page(int pageon, int row) { pageNumber = 11; this.pageon = pageon; this.row = row; } public Page(int pageon) { pageNumber = 11; this.pageon = pageon; } public Page() { pageNumber = 11; } public int getPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; } public void compute() { if (rowcount <= 0) return; if (row <= 0) row = 10; pagecount = rowcount % row != 0 ? rowcount / row + 1 : rowcount / row; if (pageon > pagecount) pageon = pagecount; if (pageon < 1) pageon = 1; start = (pageon - 1) * row; end = pageon * row; if (end > rowcount) end = rowcount; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getEnd() { return end; } public void setEnd(int end) { this.end = end; } public void setRowcountAndCompute(int rowcount) { this.rowcount = rowcount; compute(); } protected int pageon; protected int rowcount; protected int pagecount; protected int row; protected int start; protected int end; protected int pageNumber; }
前端代码开始
statis目录下加入如下几个文件
在templates目录下建立一个前端分页工具页面 page.html,(虽然代码不多,封装这点东西花费我半天时间!!)
接下来就是引用分页插件了。
在templates目录下建立userlist.html.
用户列表
ID 姓名 密码
ID 姓名 密码
可以看到 引用分页的代码 只有两句,是不是很好用
看完上述内容,你们对怎么在springboot中利用thymeleaf模板实现一个paginate分页功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
分享名称:怎么在springboot中利用thymeleaf模板实现一个paginate分页功能
网站链接:http://cdiso.cn/article/ggiejj.html