springboot mybatis pagehelper整合项目

17 篇文章 0 订阅

资源包下载https://download.csdn.net/download/afasdsdfgsdgf/15451390

首先创建一个maven项目

pom配置

 <dependencies>
        <!--web,servlet引入-->
        <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>2.0.1</version>
        </dependency>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--快速操作实体类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

    </dependencies>

application

server:
  port: 8099

spring:
  datasource:
    url: jdbc:mysql://192.168.0.129:3306/order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

# type-aliases-package批量设置别名作用:就是在mapper.xml文件中直接写类名,不配置就需要写类的全路径名
mybatis:
  type-aliases-package: com.ticomm.web.entity
  mapper-locations: classpath:mapper/*.xml

# 配置pagehelper参数
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

logging:
  level:
    com.ticomm.web.dao.OrderDao: #debug

controller

package com.ticomm.web.controller;

import com.ticomm.web.entity.Order;
import com.ticomm.web.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping(value = "getOrder")
    public String getOrder(Order order){
        System.out.println(order.getId());
        return orderService.getOrder();
    }
}

service

package com.ticomm.web.service;

public interface OrderService {
    String getOrder();
}

 

serviceimpl

package com.ticomm.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.ticomm.web.dao.OrderDao;
import com.ticomm.web.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Override
    public String getOrder() {
        PageHelper.startPage(1,10);
        return orderDao.getOrder().getCount();
    }
}

dao

package com.ticomm.web.dao;

import com.ticomm.web.entity.Order;

public interface OrderDao {
    Order getOrder();
}

mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ticomm.web.dao.OrderDao">
    <select id="getOrder" resultType="com.ticomm.web.entity.Order" parameterType="com.ticomm.web.entity.Order">
        select id,count from sys_order_t
    </select>
</mapper>

app

package com.ticomm.web;


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

@SpringBootApplication
@MapperScan(value = {"com.ticomm.web.dao"})
public class App8099 {

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

entity

package com.ticomm.web.entity;

import lombok.Data;

@Data
public class Order {

    private String id;

    private String count;

}

启动即可

Spring Boot整合MyBatis PageHelper可以实现分页查询功能。下面是一个简单的示例: 首先,需要在pom.xml文件中添加MyBatis PageHelper的依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 然后,在application.properties或application.yml文件中配置数据源相关信息,例如: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 接下来,在Spring Boot的启动类上添加`@MapperScan`注解,指定要扫描的Mapper接口所在的包路径,例如: ```java @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 然后,在Mapper接口中定义查询方法,使用PageHelper提供的静态方法`startPage`来启动分页功能,例如: ```java import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; @Mapper public interface UserMapper { List<User> getUsers(); @Select("SELECT * FROM users") @ResultMap("userResultMap") Page<User> getUsersWithPage(); } ``` 最后,在Service或Controller层调用Mapper接口的方法即可实现分页查询,例如: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getUsers() { return userMapper.getUsers(); } public Page<User> getUsersWithPage(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); return userMapper.getUsersWithPage(); } } ``` 这样就完成了Spring BootMyBatis PageHelper整合,可以通过调用`getUsersWithPage`方法实现分页查询操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你好毒你好毒你好毒毒毒毒毒

请我吃个辣条吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值