springboot+mybatis集成分页功能

1.搭建使用idea搭建srpingboot项目

在pom.xml文件中引入如下的依赖:

                <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- swagger文档相关依赖-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.1.2</version>
		</dependency>
		<!-- swagger文档相关依赖上下2个依赖的版本号必须一致-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- mybatis相关依赖-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.2.2</version>
		</dependency>
		<!-- 使用阿里的druid数据库连接池-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<!-- jdbc相关插件-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
			<version>1.5.19.RELEASE</version>
		</dependency>
		<!-- mysql相关插件-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>

		<!-- 分页插件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.3</version>
		</dependency>
		<!-- 用来将对象转成json和将json转为对象 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.12</version>
		</dependency>

spring项目配置文件:application.yml,如果是application.properties,那么建议改成yml格式,配置如下:

server:
  port: 2300
spring:
  application:
    name: run
  datasource:
    druid:
      url: jdbc:mysql://192.168.50.117:23306/shop?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
      username: shop
      password: shop
      filter:
        stat:
          db-type: mysql
          log-slow-sql: true
          slow-sql-millis: 3000
          enabled: true
        wall:
          config:
            multi-statement-allow: true
          enabled: true
      filters: stat,wall #防御SQL注入的filter:wall
      initial-size: 1 #初始化连接数量
      max-active: 10 #最大并发连接数
      min-idle: 1 #最小空闲连接数
      max-wait: 60000 #配置获取连接等待超时的时间
      time-between-eviction-runs-millis: 60000 #间隔多久才进行一次检测
      min-evictable-idle-time-millis: 300000 #一个连接在池中最小生存的时间
      test-on-borrow: false
      test-on-return: false
      test-while-idle: true
      validation-query: SELECT ‘lll‘
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 内置监控页面的首页是/druid/index.html
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        login-username: flex
        login-password: flex
        reset-enable: false

//包结构如下创建:

//其中domain,mapper接口和mapper.xml可以通过mybatis生产工具生成.一般可以在百度下载一个生产工具,然后生成对应的类后就拷贝到项目来:

此处可以参考别人提供的方法:https://www.cnblogs.com/smileberry/p/4145872.html

如下是我下载的一个项目:

接着在主配置类上配上mapper的包扫描路径:

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

//创建service和实现类(具体分页逻辑后面分析)

public interface IOrderMastService {

    ShopOrderMast getOrderMastByOrderId(String orderId);

    PageInfo<ShopOrderMast> selectOrderList(OrderQueryObject   queryObject);

}

@Service
public class OrderMastServiceImpl implements IOrderMastService{
    private final static Logger logger =  LoggerFactory.getLogger(OrderMastServiceImpl.class);
    @Autowired
    private ShopOrderMastMapper orderMastMapper;
    @Override
    public ShopOrderMast getOrderMastByOrderId(String orderId) {
        return orderMastMapper.findOrderByOrderId(orderId);
    }

    @Override
    public PageInfo<ShopOrderMast> selectOrderList(OrderQueryObject queryObject) {
        logger.info("请求参数:{}", JSON.toJSONString(queryObject));
        PageHelper.startPage(queryObject.getCurrentNum(),queryObject.getPageSize());
        List<ShopOrderMast> list= orderMastMapper.selectOrderList(queryObject);
        logger.info("返回值:{}", JSON.toJSONString(list));
        return new PageInfo<>(list);
    }
}

//mapper接口
@Repositorypublic interface ShopOrderMastMapper {    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table shop_order_mast     *     * @mbggenerated     */    int deleteByPrimaryKey(Long codId);

    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table shop_order_mast     *     * @mbggenerated     */    int insert(ShopOrderMast record);

    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table shop_order_mast     *     * @mbggenerated     */    int insertSelective(ShopOrderMast record);

    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table shop_order_mast     *     * @mbggenerated     */    ShopOrderMast selectByPrimaryKey(Long codId);

    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table shop_order_mast     *     * @mbggenerated     */    int updateByPrimaryKeySelective(ShopOrderMast record);

    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table shop_order_mast     *     * @mbggenerated     */    int updateByPrimaryKey(ShopOrderMast record);

    ShopOrderMast findOrderByOrderId(String orderId);

    List<ShopOrderMast> selectOrderList(OrderQueryObject queryObject);}
//controller
@Controller@Api(value ="订单",description ="订单相关接口" ) //API注解是用来对类进行描述public class ShopOrderController {

    private final static Logger logger =  LoggerFactory.getLogger(ShopOrderController.class);    @Autowired    private IOrderMastService orderMastService;

    @RequestMapping(value = "shop/order/{orderId}",method = RequestMethod.GET)    @ResponseBody    @ApiOperation(value ="订单查询",notes ="通过订单号查询对应的订单",httpMethod ="POST")//对该方法进行说明    public ShopOrderMast findOrderMastByOrderId(@PathVariable("orderId") String orderId){        return orderMastService.getOrderMastByOrderId(orderId);    }

    @RequestMapping(value = "shop/order/list",method = RequestMethod.POST)    @ResponseBody    @ApiOperation(value ="订单列表",notes ="订单列表分页查询",httpMethod ="POST")//对该方法进行说明    public PageInfo<ShopOrderMast> findOrderMastByOrderId(@RequestBody OrderQueryObject queryObject){        return orderMastService.selectOrderList(queryObject);    }}

mapper.xml(提供部分信息)
<select id="findOrderByOrderId" resultMap="BaseResultMap">  select * from shop_order_mast WHERE  cod_order_id=#{orderId};</select><select id="selectOrderList" resultMap="BaseResultMap">  select * from shop_order_mast WHERE  flg_status_order=#{orderStatu} ORDER  by dat_order_submit DESC</select>

//分页请求参数对象
public class QueryObject {    @ApiModelProperty("当前页")    private int currentNum=1;    @ApiModelProperty("总页数")    private int pageSize=10;

    public int getCurrentNum() {        return currentNum;    }

    public void setCurrentNum(int currentNum) {        this.currentNum = currentNum;    }

    public int getPageSize() {        return pageSize;    }

    public void setPageSize(int pageSize) {        this.pageSize = pageSize;    }}

public class OrderQueryObject extends QueryObject {    @ApiModelProperty("订单状态")    private int orderStatu;

    public int getOrderStatu() {        return orderStatu;    }

    public void setOrderStatu(int orderStatu) {        this.orderStatu = orderStatu;    }}
分页实现总结:

 

原文地址:https://www.cnblogs.com/yangxiaohui227/p/10969348.html

时间: 2024-10-08 05:57:31

springboot+mybatis集成分页功能的相关文章

springboot + mybatis配置分页插件

1:首先配置springboot +mybatis框架  参考:http://www.cnblogs.com/liyafei/p/7911549.html 2:创建配置类MybatisConfig,对分页插件进行配置.将mybatis-config.xml移动到classpath路径下. package com.liyafei.util.pagehelper; import java.util.Properties; import org.apache.ibatis.plugin.Interce

SpringBoot+Mybatis集成搭建

本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了xml的其实也可以用xml来实现的,实现上具体用什么方式并不重要,主要是搭建一遍,对框架的运转就比较清晰.本博客还是用xml的方式来实现Mybatis的sql编写,不用注解方式. maven配置 <!-- springboot mybatis--> <dependency> <gr

springboot mybatis 集成mapper与pagehelper

1.引入jar到pom.xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>tk.mybati

SpringBoot+Mybatis+Pagehelper分页

1.pom.xml <!-- mybatis分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> 2.驼峰命名在application.pr

SpringBoot+MyBatis+PageHelper分页无效

POM.XML中的配置如下:<!-- 分页插件 --><!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --><dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.1

springboot +mybatis分页插件PageHelper

1.问题描述 JAVA界ORM的两位大佬Hibernate和Mybatis,hb自带分页(上手挺快,以前用了好几年hb,后期运维及优化快疯了),mybatis没有分页功能,需要借助第三方插件来完成,比较流行的三方框架:PageHelper,今天结合springboot做下介绍,直接贴线上配置,保证可用(如有遗漏,朋友们可以指正下). 2. 解决方案 2.1 配置项目pom.xml <!--分页--> <dependency> <groupId>com.github.pa

SpringBoot集成MyBatis的分页插件PageHelper

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

SpringBoot集成MyBatis的分页插件PageHelper(回头草)

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

spring和mybatis集成,自动生成model、mapper,增加mybatis分页功能

软件简介 Spring是一个流行的控制反转(IoC)和面向切面(AOP)的容器框架,在java webapp开发中使用广泛.http://projects.spring.io/spring-framework/ MyBatis是一个基于Java的数据持久层框架,其原名是iBatis,在升级到3.0版本后,更名为MyBatis.https://github.com/mybatis/mybatis-3/ MyBatis Generator是一个MyBatis的代码生成器,通过配置,可自动生成数据操作