您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Springboot2中如何集成pagehelper

2024/4/23 4:10:50发布13次查看
1、pom.xml
<?xml version="1.0" encoding="utf-8"?><project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelversion>4.0.0</modelversion>  <parent>    <groupid>org.springframework.boot</groupid>    <artifactid>spring-boot-starter-parent</artifactid>    <version>2.2.1.release</version>    <relativepath/> <!-- lookup parent from repository -->  </parent>  <groupid>qinfeng.zheng</groupid>  <artifactid>learn-pagequery</artifactid>  <version>0.0.1-snapshot</version>  <name>learn-pagequery</name>  <description>demo project for spring boot</description>  <properties>    <java.version>1.8</java.version>  </properties>  <dependencies>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter-web</artifactid>    </dependency>    <dependency>      <groupid>org.mybatis.spring.boot</groupid>      <artifactid>mybatis-spring-boot-starter</artifactid>      <version>1.3.2</version>    </dependency>    <dependency>      <groupid>mysql</groupid>      <artifactid>mysql-connector-java</artifactid>      <version>5.1.47</version>      <scope>runtime</scope>    </dependency>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter-test</artifactid>      <scope>test</scope>      <exclusions>        <exclusion>          <groupid>org.junit.vintage</groupid>          <artifactid>junit-vintage-engine</artifactid>        </exclusion>      </exclusions>    </dependency>    <dependency>      <groupid>com.github.pagehelper</groupid>      <artifactid>pagehelper-spring-boot-starter</artifactid>      <version>1.2.12</version>    </dependency>  </dependencies>  <build>    <plugins>      <plugin>        <groupid>org.springframework.boot</groupid>        <artifactid>spring-boot-maven-plugin</artifactid>      </plugin>    </plugins>  </build></project>
2. application.peroperties
#pagehelper
pagehelper.helper-dialect=mysql
pagehelper.params=count=countsql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
#mysql
spring.datasource.driver-class-name = com.mysql.jdbc.driver
spring.datasource.url= jdbc:mysql://120.79.xx.xx:3306/test?useunicode=yes&characterencoding=utf-8&usessl=false
spring.datasource.username = root
spring.datasource.password = 1212212
3.实体类
public class country implements serializable {  private static final long serialversionuid = 6569081236403751407l;  private int  id;  private string countryname;  private string countrycode;  public int getid() {    return id;  }  public void setid(int id) {    this.id = id;  }  public string getcountryname() {    return countryname;  }  public void setcountryname(string countryname) {    this.countryname = countryname;  }  public string getcountrycode() {    return countrycode;  }  public void setcountrycode(string countrycode) {    this.countrycode = countrycode;  }}
4,mapper接口类
@mapperpublic interface countrymapper {  @select(select * from country)  list<country> findall();}
5.cotroller类
@restcontrollerpublic class countrycontroller {  @autowired  private countrymapper countrymapper;  @getmapping(/findall)  public list<country> findall(@requestparam(defaultvalue = 1) integer pagenum, @requestparam(defaultvalue = 20) integer pagesize) {    pagehelper.startpage(pagenum, pagesize);    list<country> countries = countrymapper.findall();    page page = (page) countries;    system.out.println(每页展示条数: + page.getpagesize());    system.out.println(总条数: + page.gettotal());    system.out.println(当前页: + page.getpagenum());    system.out.println(总页数: + page.getpages());    return countries;  }}
7.测试数据
直接抄官方数据
drop table country if exists;create table country ( id int primary key auto_increment, countryname varchar(32), countrycode varchar(2));insert into country (id, countryname, countrycode) values(1,'angola','ao');insert into country (id, countryname, countrycode) values(2,'afghanistan','af');insert into country (id, countryname, countrycode) values(3,'albania','al');insert into country (id, countryname, countrycode) values(4,'algeria','dz');insert into country (id, countryname, countrycode) values(5,'andorra','ad');insert into country (id, countryname, countrycode) values(6,'anguilla','ai');insert into country (id, countryname, countrycode) values(7,'antigua and barbuda','ag');insert into country (id, countryname, countrycode) values(8,'argentina','ar');insert into country (id, countryname, countrycode) values(9,'armenia','am');insert into country (id, countryname, countrycode) values(10,'australia','au');insert into country (id, countryname, countrycode) values(11,'austria','at');insert into country (id, countryname, countrycode) values(12,'azerbaijan','az');insert into country (id, countryname, countrycode) values(13,'bahamas','bs');insert into country (id, countryname, countrycode) values(14,'bahrain','bh');insert into country (id, countryname, countrycode) values(15,'bangladesh','bd');insert into country (id, countryname, countrycode) values(16,'barbados','bb');insert into country (id, countryname, countrycode) values(17,'belarus','by');insert into country (id, countryname, countrycode) values(18,'belgium','be');insert into country (id, countryname, countrycode) values(19,'belize','bz');insert into country (id, countryname, countrycode) values(20,'benin','bj');insert into country (id, countryname, countrycode) values(21,'bermuda is.','bm');insert into country (id, countryname, countrycode) values(22,'bolivia','bo');insert into country (id, countryname, countrycode) values(23,'botswana','bw');insert into country (id, countryname, countrycode) values(24,'brazil','br');insert into country (id, countryname, countrycode) values(25,'brunei','bn');insert into country (id, countryname, countrycode) values(26,'bulgaria','bg');insert into country (id, countryname, countrycode) values(27,'burkina-faso','bf');insert into country (id, countryname, countrycode) values(28,'burma','mm');insert into country (id, countryname, countrycode) values(29,'burundi','bi');insert into country (id, countryname, countrycode) values(30,'cameroon','cm');insert into country (id, countryname, countrycode) values(31,'canada','ca');insert into country (id, countryname, countrycode) values(32,'central african republic','cf');insert into country (id, countryname, countrycode) values(33,'chad','td');insert into country (id, countryname, countrycode) values(34,'chile','cl');insert into country (id, countryname, countrycode) values(35,'china','cn');insert into country (id, countryname, countrycode) values(36,'colombia','co');insert into country (id, countryname, countrycode) values(37,'congo','cg');insert into country (id, countryname, countrycode) values(38,'cook is.','ck');insert into country (id, countryname, countrycode) values(39,'costa rica','cr');insert into country (id, countryname, countrycode) values(40,'cuba','cu');insert into country (id, countryname, countrycode) values(41,'cyprus','cy');insert into country (id, countryname, countrycode) values(42,'czech republic','cz');insert into country (id, countryname, countrycode) values(43,'denmark','dk');insert into country (id, countryname, countrycode) values(44,'djibouti','dj');insert into country (id, countryname, countrycode) values(45,'dominica rep.','do');insert into country (id, countryname, countrycode) values(46,'ecuador','ec');insert into country (id, countryname, countrycode) values(47,'egypt','eg');insert into country (id, countryname, countrycode) values(48,'ei salvador','sv');insert into country (id, countryname, countrycode) values(49,'estonia','ee');insert into country (id, countryname, countrycode) values(50,'ethiopia','et');insert into country (id, countryname, countrycode) values(51,'fiji','fj');insert into country (id, countryname, countrycode) values(52,'finland','fi');insert into country (id, countryname, countrycode) values(53,'france','fr');insert into country (id, countryname, countrycode) values(54,'french guiana','gf');insert into country (id, countryname, countrycode) values(55,'gabon','ga');insert into country (id, countryname, countrycode) values(56,'gambia','gm');insert into country (id, countryname, countrycode) values(57,'georgia','ge');insert into country (id, countryname, countrycode) values(58,'germany','de');insert into country (id, countryname, countrycode) values(59,'ghana','gh');insert into country (id, countryname, countrycode) values(60,'gibraltar','gi');insert into country (id, countryname, countrycode) values(61,'greece','gr');insert into country (id, countryname, countrycode) values(62,'grenada','gd');insert into country (id, countryname, countrycode) values(63,'guam','gu');insert into country (id, countryname, countrycode) values(64,'guatemala','gt');insert into country (id, countryname, countrycode) values(65,'guinea','gn');insert into country (id, countryname, countrycode) values(66,'guyana','gy');insert into country (id, countryname, countrycode) values(67,'haiti','ht');insert into country (id, countryname, countrycode) values(68,'honduras','hn');insert into country (id, countryname, countrycode) values(69,'hongkong','hk');insert into country (id, countryname, countrycode) values(70,'hungary','hu');insert into country (id, countryname, countrycode) values(71,'iceland','is');insert into country (id, countryname, countrycode) values(72,'india','in');insert into country (id, countryname, countrycode) values(73,'indonesia','id');insert into country (id, countryname, countrycode) values(74,'iran','ir');insert into country (id, countryname, countrycode) values(75,'iraq','iq');insert into country (id, countryname, countrycode) values(76,'ireland','ie');insert into country (id, countryname, countrycode) values(77,'israel','il');insert into country (id, countryname, countrycode) values(78,'italy','it');insert into country (id, countryname, countrycode) values(79,'jamaica','jm');insert into country (id, countryname, countrycode) values(80,'japan','jp');insert into country (id, countryname, countrycode) values(81,'jordan','jo');insert into country (id, countryname, countrycode) values(82,'kampuchea (cambodia )','kh');insert into country (id, countryname, countrycode) values(83,'kazakstan','kz');insert into country (id, countryname, countrycode) values(84,'kenya','ke');insert into country (id, countryname, countrycode) values(85,'korea','kr');insert into country (id, countryname, countrycode) values(86,'kuwait','kw');insert into country (id, countryname, countrycode) values(87,'kyrgyzstan','kg');insert into country (id, countryname, countrycode) values(88,'laos','la');insert into country (id, countryname, countrycode) values(89,'latvia','lv');insert into country (id, countryname, countrycode) values(90,'lebanon','lb');insert into country (id, countryname, countrycode) values(91,'lesotho','ls');insert into country (id, countryname, countrycode) values(92,'liberia','lr');insert into country (id, countryname, countrycode) values(93,'libya','ly');insert into country (id, countryname, countrycode) values(94,'liechtenstein','li');insert into country (id, countryname, countrycode) values(95,'lithuania','lt');insert into country (id, countryname, countrycode) values(96,'luxembourg','lu');insert into country (id, countryname, countrycode) values(97,'macao','mo');insert into country (id, countryname, countrycode) values(98,'madagascar','mg');insert into country (id, countryname, countrycode) values(99,'malawi','mw');insert into country (id, countryname, countrycode) values(100,'malaysia','my');insert into country (id, countryname, countrycode) values(101,'maldives','mv');insert into country (id, countryname, countrycode) values(102,'mali','ml');insert into country (id, countryname, countrycode) values(103,'malta','mt');insert into country (id, countryname, countrycode) values(104,'mauritius','mu');insert into country (id, countryname, countrycode) values(105,'mexico','mx');insert into country (id, countryname, countrycode) values(106,'moldova, republic of','md');insert into country (id, countryname, countrycode) values(107,'monaco','mc');insert into country (id, countryname, countrycode) values(108,'mongolia','mn');insert into country (id, countryname, countrycode) values(109,'montserrat is','ms');insert into country (id, countryname, countrycode) values(110,'morocco','ma');insert into country (id, countryname, countrycode) values(111,'mozambique','mz');insert into country (id, countryname, countrycode) values(112,'namibia','na');insert into country (id, countryname, countrycode) values(113,'nauru','nr');insert into country (id, countryname, countrycode) values(114,'nepal','np');insert into country (id, countryname, countrycode) values(115,'netherlands','nl');insert into country (id, countryname, countrycode) values(116,'new zealand','nz');insert into country (id, countryname, countrycode) values(117,'nicaragua','ni');insert into country (id, countryname, countrycode) values(118,'niger','ne');insert into country (id, countryname, countrycode) values(119,'nigeria','ng');insert into country (id, countryname, countrycode) values(120,'north korea','kp');insert into country (id, countryname, countrycode) values(121,'norway','no');insert into country (id, countryname, countrycode) values(122,'oman','om');insert into country (id, countryname, countrycode) values(123,'pakistan','pk');insert into country (id, countryname, countrycode) values(124,'panama','pa');insert into country (id, countryname, countrycode) values(125,'papua new cuinea','pg');insert into country (id, countryname, countrycode) values(126,'paraguay','py');insert into country (id, countryname, countrycode) values(127,'peru','pe');insert into country (id, countryname, countrycode) values(128,'philippines','ph');insert into country (id, countryname, countrycode) values(129,'poland','pl');insert into country (id, countryname, countrycode) values(130,'french polynesia','pf');insert into country (id, countryname, countrycode) values(131,'portugal','pt');insert into country (id, countryname, countrycode) values(132,'puerto rico','pr');insert into country (id, countryname, countrycode) values(133,'qatar','qa');insert into country (id, countryname, countrycode) values(134,'romania','ro');insert into country (id, countryname, countrycode) values(135,'russia','ru');insert into country (id, countryname, countrycode) values(136,'saint lueia','lc');insert into country (id, countryname, countrycode) values(137,'saint vincent','vc');insert into country (id, countryname, countrycode) values(138,'san marino','sm');insert into country (id, countryname, countrycode) values(139,'sao tome and principe','st');insert into country (id, countryname, countrycode) values(140,'saudi arabia','sa');insert into country (id, countryname, countrycode) values(141,'senegal','sn');insert into country (id, countryname, countrycode) values(142,'seychelles','sc');insert into country (id, countryname, countrycode) values(143,'sierra leone','sl');insert into country (id, countryname, countrycode) values(144,'singapore','sg');insert into country (id, countryname, countrycode) values(145,'slovakia','sk');insert into country (id, countryname, countrycode) values(146,'slovenia','si');insert into country (id, countryname, countrycode) values(147,'solomon is','sb');insert into country (id, countryname, countrycode) values(148,'somali','so');insert into country (id, countryname, countrycode) values(149,'south africa','za');insert into country (id, countryname, countrycode) values(150,'spain','es');insert into country (id, countryname, countrycode) values(151,'sri lanka','lk');insert into country (id, countryname, countrycode) values(152,'st.lucia','lc');insert into country (id, countryname, countrycode) values(153,'st.vincent','vc');insert into country (id, countryname, countrycode) values(154,'sudan','sd');insert into country (id, countryname, countrycode) values(155,'suriname','sr');insert into country (id, countryname, countrycode) values(156,'swaziland','sz');insert into country (id, countryname, countrycode) values(157,'sweden','se');insert into country (id, countryname, countrycode) values(158,'switzerland','ch');insert into country (id, countryname, countrycode) values(159,'syria','sy');insert into country (id, countryname, countrycode) values(160,'taiwan','tw');insert into country (id, countryname, countrycode) values(161,'tajikstan','tj');insert into country (id, countryname, countrycode) values(162,'tanzania','tz');insert into country (id, countryname, countrycode) values(163,'thailand','th');insert into country (id, countryname, countrycode) values(164,'togo','tg');insert into country (id, countryname, countrycode) values(165,'tonga','to');insert into country (id, countryname, countrycode) values(166,'trinidad and tobago','tt');insert into country (id, countryname, countrycode) values(167,'tunisia','tn');insert into country (id, countryname, countrycode) values(168,'turkey','tr');insert into country (id, countryname, countrycode) values(169,'turkmenistan','tm');insert into country (id, countryname, countrycode) values(170,'uganda','ug');insert into country (id, countryname, countrycode) values(171,'ukraine','ua');insert into country (id, countryname, countrycode) values(172,'united arab emirates','ae');insert into country (id, countryname, countrycode) values(173,'united kiongdom','gb');insert into country (id, countryname, countrycode) values(174,'united states of america','us');insert into country (id, countryname, countrycode) values(175,'uruguay','uy');insert into country (id, countryname, countrycode) values(176,'uzbekistan','uz');insert into country (id, countryname, countrycode) values(177,'venezuela','ve');insert into country (id, countryname, countrycode) values(178,'vietnam','vn');insert into country (id, countryname, countrycode) values(179,'yemen','ye');insert into country (id, countryname, countrycode) values(180,'yugoslavia','yu');insert into country (id, countryname, countrycode) values(181,'zimbabwe','zw');insert into country (id, countryname, countrycode) values(182,'zaire','zr');insert into country (id, countryname, countrycode) values(183,'zambia','zm');
好,一切准备就绪,启动springboot项目,浏览器访问,注意controller中打印
第一次请求:
controller打印:
第二次请求:
controller打印:
ok ,验证完毕!完美!!!
补充:
在做分页功能时,前端分页插件一般都需要一些诸如当前页数,总条数,总共多少页之类的数据,这时可以引用pageinfo这个对象,它完全能够满足插件的分页要求。
@getmapping(/findall)  public pageinfo<country> findall(@requestparam(defaultvalue = 1) integer pagenum, @requestparam(defaultvalue = 20) integer pagesize) {    pagehelper.startpage(pagenum, pagesize);    list<country> countries = countrymapper.findall();//    page page = (page) countries;//    system.out.println(每页展示条数: + page.getpagesize());//    system.out.println(总条数: + page.gettotal());//    system.out.println(当前页: + page.getpagenum());//    system.out.println(总页数: + page.getpages());    pageinfo<country> result = new pageinfo<>(countries);    return result;  }
请求测试:
查看pageinfo类的源码,不难发现,它对前端分页插件的属性支持还是很全面的
private int pagenum; // 当前页码  private int pagesize; // 每页展示的多少条数据  private int size;     // 数据总条数  private int startrow;    private int endrow;  private int pages;   // 总共多少页  private int prepage;  // 前一页  private int nextpage; // 后一页  private boolean isfirstpage;  private boolean islastpage;  private boolean haspreviouspage;  private boolean hasnextpage;  private int navigatepages;  private int[] navigatepagenums;  private int navigatefirstpage;  private int navigatelastpage;
以上就是springboot2中如何集成pagehelper的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product