springboot2.x+mybatis+pagehelper操作数据库并分页

SpringBoot 整合Mybatis

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.5.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.demo</groupId>
    <artifactId>bit-demo</artifactId>
    <version>6.0.0-SNAPSHOT</version>
    <name>bit-demo</name>
    <packaging>pom</packaging>
    <description>project for Spring Boot</description>

    <properties>
        <mysql.version>8.0.19</mysql.version>
        <mybatis.version>2.1.2</mybatis.version>
        <pagehelper.version>1.2.13</pagehelper.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!-- 打包跳过测试模块 -->
        <skipTests>true</skipTests>
    </properties>

    <dependencies>
    	<!--web-->
	<dependency>
    	    <groupId>org.springframework.boot</groupId>
    	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
 	<!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pagehelper.version}</version>
        </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>
    </dependencies>

    <!--标识依赖的版本-->
    <dependencyManagement>
        <dependencies>
            <!--mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
                <scope>runtime</scope>
            </dependency>
        <dependencies>
    </dependencyManagement>
</project>

2.application.yml

spring:
  application:
    name: bit-demo
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: demo
    password: xxxxxxxxxxxx
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/smartlog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    hikari:
      connection-test-query: SELECT 1
      
mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.bit.demo.entity
  configuration:
    use-generated-keys: true
    default-result-set-type: default
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印sql, 方便调试
    
pagehelper:
  helperDialect: mysql
  offsetAsPageNum: true
  rowBoundsWithCount: true
  reasonable: false

3.pageHelper示例
此处在dao层返回的数据需要做二次处理,这个时候如果我们直接返回处理后的数据,分页信息会失效,所以我们需要对分页进行二次处理。处理逻辑如下:

/**
 * @Deacription 通知策略管理服务实现
 * @Author wenyt
 * @Date 2020/5/31 16:59
 * @Version 1.0
 **/

@Service
public class NotifyServiceImplBak implements NotifyService {

    private static final SimpleDateFormat DTF = new SimpleDateFormat("yyyy年MM月dd日");

    @Autowired
    private NotifyMapper notifyMapper;

    @Override
    public PageInfo<SmartlogNotifyDto> findAllByPage(Integer pageNum, Integer pageSize) {
        //获取源分页信息
        PageInfo<SmartlogNotify> source = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            //执行数据库查询
            notifyMapper.findAll();
        });
        // 需要转换的对象
        PageInfo<SmartlogNotifyDto> target = new PageInfo<>();
        // 复制分页属性
        BeanUtils.copyProperties(source, target);
        List<SmartlogNotifyDto> notifyDtoList = new ArrayList<>();
        List<SmartlogNotify> all = source.getList();
        all.forEach(notify -> {
            SmartlogNotifyDto notifyDto = convertBean2Model(notify);
            notifyDtoList.add(notifyDto);
        });
        target.setList(notifyDtoList);
        return target;
    }
    

    /**
     * 将通知策略实体转化为通知策略业务模型
     *
     * @param notify 通知策略实体
     * @return SmartlogNotifyDto(通知策略业务模型)
     */
    private SmartlogNotifyDto convertBean2Model(SmartlogNotify notify) {
        if (notify == null) {
            return null;
        }
        SmartlogNotifyDto notifyDto = new SmartlogNotifyDto();
        BeanUtils.copyProperties(notify, notifyDto);
        Date createTime = notify.getCreateTime();
        notifyDto.setCreateTime(DTF.format(createTime));
        return notifyDto;
    }

}

这样我们就可以方便的对数据库进行分页操作了。

你可能感兴趣的:(SpringBoot,mybatis,java,spring,spring,boot,mysql)