springboot用thymeleaf模板的paginate分页完整代码

网友投稿 328 2023-04-24


springboot用thymeleaf模板的paginate分页完整代码

本文根据一个简单的user表为例,展示 springboot集成mybatis,再到前端分页完整代码(新手自学,不足之处欢迎纠正);

先看java部分

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-autohttp://=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 Map UserList(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

select id, name, password from user order by id limit #{page.start},#{page.row}

select count(1) from user

到此时后台代码结束

附加个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;

XUNACYfoq 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,(虽然代码不多,封装这点东西花费我半天时间!!)

xmlns:th="http://thymeleaf.org">

接下来就是引用分页插件了。

在templates目录下建立userlist.html.

xmlns:th="http://thymeleaf.org">

可以看到 引用分页的代码 只有两句,是不是很好用


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:谈谈VUE种methods watch和compute的区别和联系
下一篇:angular $watch 一个变量的变化(实例讲解)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~