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 층 에서 돌아 온 데 이 터 는 2 차 처리 가 필요 합 니 다. 이 럴 때 우리 가 처리 한 데 이 터 를 직접 되 돌려 주면 페이지 정보 가 효력 을 잃 기 때문에 페이지 를 2 차 처리 해 야 합 니 다.처리 논 리 는 다음 과 같다.
/**
 * @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;
    }

}

이렇게 하면 우 리 는 편리 하 게 데이터 베 이 스 를 페이지 별로 조작 할 수 있다.

좋은 웹페이지 즐겨찾기