欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot整合mybatis和分页插件pagehelper

程序员文章站 2022-07-15 10:26:57
...

最近使用mybatis比较少,也没有用过springboot整合过mybatis及其分页插件,在这个过程中出现了好多问题,开贴记录一下,希望大家能够避免入坑。

proterrties的配置,我这里没有贴出数据的配置
#mybatis
mybatis.mapper-locations=classpath:com.wyb.mybaties.mybaties.mapper/*.xml
#mybatis.type-aliases-package=com.wyb.mybaties.mybaties.pojo

#分页插件的配置
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true

分页插件在启动类的配置
package com.wyb.mybaties.mybaties;

import com.github.pagehelper.PageHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@SpringBootApplication
@ComponentScan(basePackages = {“com”})
@MapperScan(“com.wyb.mybaties.mybaties.mapper”)
public class MybatiesApplication {

public static void main(String[] args) {
	SpringApplication.run(MybatiesApplication.class, args);
}
/*
 * 注册MyBatis分页插件PageHelper
 */
@Configuration
public class MybatisConf {
	@Bean
	public PageHelper pageHelper() {
		System.out.println("MyBatisConfiguration.pageHelper()");
		PageHelper pageHelper = new PageHelper();
		Properties p = new Properties();
		p.setProperty("offsetAsPageNum", "true");
		p.setProperty("rowBoundsWithCount", "true");
		p.setProperty("reasonable", "true");
		pageHelper.setProperties(p);
		return pageHelper;
	}
}

}

贴出springboot的版本和pagehelper版本 这个版本也要匹配不然会抱错

java.util.ArrayList cannot be cast to com.github.pagehelper.Page
springboot整合mybatis和分页插件pagehelper
springboot整合mybatis和分页插件pagehelper