SpringBoot 12 mybatis分页插件

1:原理

底层修改SQL语句。

2:修改pom.xml文件,添加依赖

<!-- 整合mybatis分页插件  -->
		<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

3:用法

package com.zhaoy.datasourceipp1.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.zhaoy.entity.Comment;

public interface CommentMapper1 {
	@Insert("INSERT INTO COMMENT(ID,PRODUCT_ID,COMMENT_TIME,IS_DELETED) VALUE "
			+ "(#{id}, #{productId}, #{commentTime}, #{isDeleted})")
	int insertComment(@Param("id") String id, @Param("productId") String productId, 
			@Param("commentTime") String commentTime, @Param("isDeleted") Boolean isDeleted);
	
	@Results({@Result(property = "id", column = "ID", id = true),
		@Result(property = "productId", column = "product_id"),
		@Result(property = "CommentTime", column = "comment_time"),
		@Result(property = "isDeleted", column = "is_deleted")})
	@Select("SELECT * FROM COMMENT")
	List<Comment> selCommentList();
}
package com.zhaoy.datasourceipp1.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhaoy.datasourceipp1.mapper.CommentMapper1;
import com.zhaoy.datasourceipp2.mapper.CommentMapper2;
import com.zhaoy.entity.Comment;


@Service
public class CommentService1 {
	
	@Autowired
	private CommentMapper1 commentMapper1;
	
	public PageInfo<Comment> selCommentList(int pageNum, int pageSize){
		System.out.println("pageNum: " + pageNum + " pageSize: " + pageSize);
		PageHelper.startPage(pageNum, pageSize);
		List<Comment> selCommentList = commentMapper1.selCommentList();
		PageInfo<Comment> list = new PageInfo<Comment>(selCommentList);
		return list;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot整合MyBatis分页插件的步骤如下: 1. 在pom.xml文件中添加MyBatis分页插件的依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> ``` 2. 在application.properties文件中配置MyBatis分页插件的参数: ``` # 分页插件配置 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql ``` 3. 在Mapper接口中添加分页查询方法: ``` List<User> selectByPage(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize); ``` 4. 在Service层中调用分页查询方法: ``` PageHelper.startPage(pageNum, pageSize); List<User> userList = userDao.selectByPage(pageNum, pageSize); PageInfo<User> pageInfo = new PageInfo<>(userList); ``` 5. 在Controller层中返回分页查询结果: ``` return new Result<>(ResultCode.SUCCESS, pageInfo); ``` 以上就是Spring Boot整合MyBatis分页插件的基本步骤,希望对你有所帮助。 ### 回答2: Spring Boot是一个快速、方便的开发框架,可以帮助开发人员快速构建Web应用程序。MyBatis是一种优秀的持久化层框架,它可以让开发人员使用Java对象与关系数据库进行交互。分页插件MyBatis的一部分,它可以帮助开发人员实现分页查询数据的操作。下面将介绍如何在Spring Boot应用程序中整合MyBatis分页插件。 首先,在Spring Boot应用程序的pom.xml文件中添加MyBatis的依赖。这样可以保证应用程序能够正常地使用MyBatis框架。 接着,需要配置数据源。可以使用Spring Boot提供的自动配置,也可以手动配置数据源的相关信息。配置完成后,需要在mybatis-config.xml文件中添加分页插件的配置信息。具体配置如下: ```xml <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="dialect" value="mysql" /> <!--下面的参数根据实际情况进行调整--> <property name="reasonable" value="true" /> <property name="supportMethodsArguments" value="true" /> <property name="params" value="count=countSql" /> </plugin> </plugins> ``` 其中,dialect用于指定数据库的类型;reasonable用于指定是否合理化分页参数;supportMethodsArguments表示支持方法参数;params表示参数名映射。 最后,在Mapper接口的方法中添加分页参数即可。具体代码如下: ```java @Select("SELECT * FROM user WHERE username = #{username}") List<User> findByUsername(@Param("username") String username, PageBounds pageBounds); ``` 其中,PageBounds是分页插件所支持的参数类型,它包含分页信息的相关属性,例如当前页码、每页记录数等。然后,在Mapper.xml文件中添加相应的SQL语句即可实现分页功能。 综上所述,Spring Boot整合MyBatis分页插件的过程并不复杂,只需要按照上述步骤进行操作即可。这样可以快速方便地实现分页功能,提高Web应用程序的性能和效率。 ### 回答3: Spring Boot 是一款快速开发的框架,可帮助Java开发人员快速搭建基于Spring的应用程序。MyBatis则是一种ORM框架,它可以将Java对象映射到数据库表中,从而实现数据库的操作。分页插件则是MyBatis中提供的一种插件,它可以对查询结果进行分页处理。本文将为大家介绍如何在Spring Boot中整合MyBatis分页插件。 首先,在 pom.xml 文件中添加如下代码片段,以依赖 MyBatisMyBatis分页插件: ```xml <!-- MyBatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- MyBatis 分页插件依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> ``` 然后,创建 MyBatis 配置文件 mybatis-config.xml,在其中添加如下分页插件配置: ```xml <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="dialect" value="mysql"/> </plugin> </plugins> ``` 紧接着,创建 MyBatis 的 Mapper 文件,并在其中添加查询方法: ```java public interface UserMapper { List<User> findAll(); } ``` 在 Controller 中,则可以通过注入 UserMapper 对象来直接调用查询方法,从而实现分页查询: ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/list") public PageInfo<User> getUsers(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.findAll(); return new PageInfo<>(userList); } } ``` 以上的代码中,我们使用 @RequestParam 注解来接收前端传来的 pageNum 和 pageSize 参数,然后调用 PageHelper.startPage 方法设置分页信息。接着调用 UserMapper 接口中的查询方法,并将结果封装在 PageInfo 对象中返回给前端。 最后,启动 Spring Boot 应用程序,访问 http://localhost:8080/users/list 即可实现基于 MyBatis 分页插件的分页查询。 综上所述,通过如上步骤,我们就成功地完成了 Spring Boot 对 MyBatis 分页插件的整合,并实现了分页查询功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值