SpringBoot+Mybatis+Oracle分页实现

        在日常开发中,对数据的分页实现是在所难免的,而分页可以通过原始的方法来实现,也可以使用相应的分页插件。

Idea工程目录如下:

SpringBoot+Mybatis+Oracle分页实现_第1张图片

实体类:

数据库字段类:

package com.per.springtest.entity;

import lombok.Data;

/**
 * Created with IntelliJ IDEA.
 *
 * @author mark
 * @Date: 2020-2-28
 */
@Data
public class Sales {
    private String cstName;
    private String goods;
    private String name;
    private String status;
}

前端请求参数实体类:

package com.per.springtest.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Created with IntelliJ IDEA.
 *
 * @author mark
 * @Date: 2020-2-28
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SalesParam {
    private String cstName;
    private String goods;
    private String name;
    //起始页
    private int start;
    //结束页
    private int end;
    //请求页
    private int page;
    //请求数据量
    private int pageSize;
}

前端请求后返回的结果类:

package com.per.springtest.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author mark
 * @Date: 2020-2-28
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    //状态码 0表示成功返回数据 1为查询到数据
    private String statusCode;
    List data;
}
 
  

dao层实现(数据访问):

package com.per.springtest.dao;

import com.per.springtest.entity.SalesParam;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author mark
 * @Date: 2020-2-28
 */
@Repository
public interface SalesDao {
    List getSaleList(SalesParam param);
}
 
  

service层代码实现:

package com.per.springtest.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.per.springtest.dao.SalesDao;
import com.per.springtest.entity.Result;
import com.per.springtest.entity.SalesParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author mark
 * @Date: 2020-2-28
 */
@Service
public class SalesService {
    @Autowired
    SalesDao salesDao;
    public Result getList(SalesParam param){
        Result result;
        //分页插件
        //PageHelper.startPage(param.getPage(),param.getPageSize());
        
        //原始分页
        param.setStart((param.getPage()-1)*param.getPageSize()+1);
        param.setEnd(param.getPage()*param.getPageSize());
        List data= salesDao.getSaleList(param);
        if(data!=null){
            result=new Result("0",data);
        }else
            result=new Result("1",null);
        return result;
    }
}

 
  

控制层代码实现:

package com.per.springtest.controller;

import com.per.springtest.entity.Result;
import com.per.springtest.entity.SalesParam;
import com.per.springtest.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author mark
 * @Date: 2020-2-28
 */
@RestController
public class SaleController {
    @Autowired
    SalesService salesService;
    @PostMapping("getInfo")
    public Result getData(@RequestBody SalesParam param){
        System.out.println("入参 "+param);
        return salesService.getList(param);
    }
}

配置类代码:

package com.per.springtest.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class CORSConfiguration extends WebMvcConfigurerAdapter {
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		//所有请求都允许跨域
		registry.addMapping("/**")
				.allowedOrigins("*")
				.allowedMethods("*")
				.allowedHeaders("*");
	}
}

SpringBoot启动类代码:

package com.per.springtest;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.per.springtest.dao")
@SpringBootApplication
public class SpringtestApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringtestApplication.class, args);
    }

}

mapper文件原始分页实现:




    

SpringBoot配置文件:

spring:
  datasource:
    url: jdbc:oracle:thin:@服务器地址:端口/数据库
    driver-class-name: oracle.jdbc.driver.OracleDriver
    username: 用户名
    password: 密码
    druid:
      filter:
        log4j2:
          enabled: true
          statement-create-after-log-enabled: false
          statement-close-after-log-enabled: false
          result-set-open-after-log-enabled: false
          result-set-close-after-log-enabled: false
server:
  servlet:
    context-path: /test
  port: 8088

mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pagehelper:
  helper-dialect: oracle
  reasonable: true
  params: count
  support-methods-arguments: true

pom文件:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.5.RELEASE
         
    
    com.per
    springtest
    0.0.1-SNAPSHOT
    springtest
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.5
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            com.oracle
            ojdbc6
            11.2.0.1.0
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


前端请求后数据如下:
SpringBoot+Mybatis+Oracle分页实现_第2张图片

使用PageHelper分页插件代码修改如下图:

SpringBoot+Mybatis+Oracle分页实现_第3张图片mapper中SaleDao.xml文件修改如下:




    

运行结果与前者一致。PageHelper具体使用可以参考https://pagehelper.github.io/

你可能感兴趣的:(oracle,mybatis,spring,boot)