Mybatis-pagehelper分页插件

Mybatis-pagehelper分页插件(简单入门)

Maven方式使用:

使用方法
  1. 导入依赖包
//可以查看最新的maven依赖
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.2.1</version>
</dependency>
  1. 编程方式配置拦截器
//主要语句:加载拦截器  (方式一)
		/*配置拦截器插件,使用pagehelper插件来分页*/
        PageInterceptor pi = new PageInterceptor();
        cfg.addInterceptor(pi);
 public static void main(String[] args) throws IOException {
   
        //建立数据源,使用阿里连接池
        //通过读取properties文件来加载数据库信息
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
        Properties pro = new Properties();
        //或者:pro.load(Resources.getResourceAsReader("db.properties")); //来加载文件流
        pro.load(in);
        //阿里连接池加载数据库配置
        DruidDataSource ds = new DruidDataSource();
        ds.setConnectProperties(pro);
        
        //建立事务管理对象
        TransactionFactory tx = new JdbcTransactionFactory();
        //建立mybatis环境
        Environment env = new Environment("development", tx, ds);
        //建立配置对象,相当于加个mybatis-config.xml
        Configuration cfg = new Configuration(env);
        
        //加载指定的一个接口类,没有下面直接加载所有类好
        //cfg.addMapper(UserMapper.class);
        //直接加载cn.webrx.mapper 包下的所有接口类,及相同名称的映射文件xml
        cfg.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 是一个 MyBatis 的增强工具包,提供了很多方便的功能和特性,其中包括分页查询插件。使用 MyBatis-Plus 的分页查询插件可以简化分页查询的操作。 在 MyBatis-Plus 中,分页查询可以通过 `Page` 对象和 `PageHelper` 工具类来实现。下面是使用 MyBatis-Plus 进行分页查询的示例代码: 1. 首先,添加 MyBatis-Plus 和分页插件的依赖到你的项目中。你可以在项目的 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 2. 创建一个 `Page` 对象,并设置分页参数: ```java // 创建一个 Page 对象 Page<User> page = new Page<>(current, size); // 设置分页参数 page.setPages(current); // 当前页码 page.setSize(size); // 每页显示的记录数 ``` 3. 在 MyBatis 的 Mapper 接口中使用 `@Param` 注解传递 `Page` 对象,并在 SQL 中使用 MyBatis-Plus 提供的分页查询方法: ```java @Mapper public interface UserMapper extends BaseMapper<User> { List<User> selectUserPage(@Param("page") Page<User> page, @Param("name") String name); } ``` 4. 在 Service 层中调用分页查询方法: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUserPage(long current, long size, String name) { Page<User> page = new Page<>(current, size); return userMapper.selectUserPage(page, name); } } ``` 这样,你就可以使用 MyBatis-Plus 的分页查询插件进行分页查询了。记得在你的 SQL 语句中使用 MyBatis-Plus 提供的分页查询方法,而不是传统的 LIMIT 语句。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值