- 编写mybatis-plus配置类
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
我用的是mysql所以是DbType.MYSQL,mybatis-plus还支持其他很多数据库
MYSQL("mysql", "MySql数据库"),
MARIADB("mariadb", "MariaDB数据库"),
ORACLE("oracle", "Oracle11g及以下数据库(高版本推荐使用ORACLE_NEW)"),
ORACLE_12C("oracle12c", "Oracle12c+数据库"),
DB2("db2", "DB2数据库"),
H2("h2", "H2数据库"),
HSQL("hsql", "HSQL数据库"),
SQLITE("sqlite", "SQLite数据库"),
POSTGRE_SQL("postgresql", "Postgre数据库"),
SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库"),
SQL_SERVER("sqlserver", "SQLServer数据库"),
DM("dm", "达梦数据库"),
XU_GU("xugu", "虚谷数据库"),
KINGBASE_ES("kingbasees", "人大金仓数据库"),
PHOENIX("phoenix", "Phoenix HBase数据库"),
GAUSS("zenith", "Gauss 数据库"),
CLICK_HOUSE("clickhouse", "clickhouse 数据库"),
GBASE("gbase", "南大通用(华库)数据库"),
GBASEDBT("gbasedbt", "南大通用数据库"),
OSCAR("oscar", "神通数据库"),
SYBASE("sybase", "Sybase ASE 数据库"),
OCEAN_BASE("oceanbase", "OceanBase 数据库"),
FIREBIRD("Firebird", "Firebird 数据库"),
HIGH_GO("highgo", "瀚高数据库"),
CUBRID("cubrid", "CUBRID数据库"),
GOLDILOCKS("goldilocks", "GOLDILOCKS数据库"),
CSIIDB("csiidb", "CSIIDB数据库"),
SAP_HANA("hana", "SAP_HANA数据库"),
IMPALA("impala", "impala数据库"),
OTHER("other", "其他数据库");
- 在代码中使用
@Test
void testPageSelect(){
// 第一个参数是当前页, 第二个参数是当前页显示多少条数据
Page<User> page = new Page<>(1, 2);
Page<User> userPage = userMapper.selectPage(page, null);
// 输出查询到的用户
System.out.println(userPage.getRecords());
}