码迷,mamicode.com
首页 > 移动开发 > 详细

10—mybatis 通用mapper插件 pagehelper 分页

时间:2019-11-14 00:13:19      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:autowire   文件   hub   gen   src   get   location   return   href   

spring boot真的太好用了,大家以后多多使用,今天来说说pagehelper 来做mybatis分页,我用的是spring boot 做的开发,后面会把源码发出来。

pagehelper(https://github.com/pagehelper/Mybatis-PageHelper) 是什么我就不多说了,网上太多太多了。

还是上代码吧

Card表

CREATE TABLE `card` (
  `id` int(11) NOT NULL DEFAULT 0,
  `cid` int(11) DEFAULT NULL,
  `title` varchar(60) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `card` VALUES (1, 2, aldsjkfla);
INSERT INTO `card` VALUES (2, 3, 2222aldsjkfla);
INSERT INTO `card` VALUES (3, 1, 2323);
INSERT INTO `card` VALUES (4, 4, 4444444444);
INSERT INTO `card` VALUES (5, 5, 55555555555555);
INSERT INTO `card` VALUES (6, 6, 66666666666666);
INSERT INTO `card` VALUES (7, 7, 77777777777777);
INSERT INTO `card` VALUES (8, 8, 88888888888888888888);
INSERT INTO `card` VALUES (9, 9, 999999999999999999);
INSERT INTO `card` VALUES (10, 10, swswswsws);
INSERT INTO `card` VALUES (11, 11, 112121222222);
INSERT INTO `card` VALUES (12, 12, aaaaaa);
INSERT INTO `card` VALUES (13, 13, asdfasdfadfadfadsf);
INSERT INTO `card` VALUES (14, 14, aswsweerererer);
INSERT INTO `card` VALUES (15, 15, dfdfdfdfdfa);
INSERT INTO `card` VALUES (16, 16, dfdsfsadf);
INSERT INTO `card` VALUES (17, 17, agtgtgtgtg);
INSERT INTO `card` VALUES (18, 18, aaaggtgtg);

 

下面开始编码:

1.新建一个spring boot项目

技术图片

 

2.导包

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>org.zn.test</groupId>
    <artifactId>testpage</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testpage</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.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>
        <!--自动构造java-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.14.8</version>
        </dependency>
        <!--通用mapper启动器-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>

        <!--分页助手启动器-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

3. 配置文件,包括数据地址等信息

application.yml

server:
  port: 8099
spring:
  application:
    name: testpage
  datasource:
    url: jdbc:mysql://localhost:3306/card
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

#显示sql语句
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4.建立实体类 Card.java

package org.zn.test.testpage.entity;

import lombok.Data;
import lombok.ToString;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "card")
@Data
@ToString
public class Card {
    @Id
    @KeySql(useGeneratedKeys = true)
    private Integer id;
    private Integer cid;
    private String title;
}

5.建立Mapper 

CardMapper.java,这里注意用是通用mapper来实现的,所有继承tk.mybatis.mapper.common.Mapper的Mapper

package org.zn.test.testpage.mapper;

import org.zn.test.testpage.entity.Card;
import tk.mybatis.mapper.common.Mapper;

public interface CardMapper extends Mapper<Card> {

}

6.建立Servcie

CardService.java


package org.zn.test.testpage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zn.test.testpage.entity.Card;
import org.zn.test.testpage.mapper.CardMapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CardService {
@Autowired
private CardMapper cardMapper;

public List<Card> GetPageList(Integer page,Integer rows,String sortBy,Boolean desc,String key)
{
//分页
PageHelper.startPage(page,rows);
//过滤
Example example=new Example(Card.class);
if(key!=""&&!key.equals("")){
example.createCriteria().orLike("title","%"+key+"%");
}
//排序
if(sortBy!=""&&!key.equals("")){
String orderByClause=sortBy+(desc?" DESC":" ASC");
example.setOrderByClause(orderByClause);
}
//查询
List<Card> list=cardMapper.selectByExample(example);
PageInfo<Card> info=new PageInfo<Card>(list);
return info.getList();
}

}

7.建立Controller

HomeController.java

package org.zn.test.testpage.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zn.test.testpage.entity.Card;
import org.zn.test.testpage.service.CardService;

import java.util.List;

@RestController
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private CardService cardService;

    @RequestMapping("/index")
    public String Index(int page,String key) {
        List<Card> list = cardService.GetPageList(page, 5, "id", true, key);

        StringBuilder sb = new StringBuilder();
        for (Card item : list) {
            sb.append(item+"</br>");
        }
        return sb.toString();
    }

}

8.配置启动类

TestpageApplication.java 就是spring boot启动类,要注意这里用@MapperScan("org.zn.test.testpage.mapper"),进入整包注入。

package org.zn.test.testpage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("org.zn.test.testpage.mapper")
public class TestpageApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestpageApplication.class, args);
    }

}

 

9.启动,并访问

http://localhost:8099/home/index?page=1&key=a

技术图片

 

技术图片

 

看一下,是数据分页。

 点击下载代码

10—mybatis 通用mapper插件 pagehelper 分页

标签:autowire   文件   hub   gen   src   get   location   return   href   

原文地址:https://www.cnblogs.com/itmu89/p/11854102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!