本文整理汇总了Java中com.baomidou.mybatisplus.plugins.pagination.Pagination类的典型用法代码示例。如果您正苦于以下问题:Java Pagination类的具体用法?Java Pagination怎么用?Java Pagination使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Pagination类属于com.baomidou.mybatisplus.plugins.pagination包,在下文中一共展示了Pagination类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: queryTotal
import com.baomidou.mybatisplus.plugins.pagination.Pagination; //导入依赖的package包/类
/**
* 查询总记录条数
*
* @param sql
* @param mappedStatement
* @param boundSql
* @param page
*/
protected void queryTotal(boolean overflowCurrent, String sql, MappedStatement mappedStatement, BoundSql boundSql, Pagination page, Connection connection) {
try (PreparedStatement statement = connection.prepareStatement(sql)) {
DefaultParameterHandler parameterHandler = new MybatisDefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), boundSql);
parameterHandler.setParameters(statement);
int total = 0;
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
total = resultSet.getInt(1);
}
}
page.setTotal(total);
/*
* 溢出总页数,设置第一页
*/
int pages = page.getPages();
if (overflowCurrent && (page.getCurrent() > pages)) {
// 设置为第一条
page.setCurrent(1);
}
} catch (Exception e) {
logger.error("Error: Method queryTotal execution error !", e);
}
}
示例2: concatOrderBy
import com.baomidou.mybatisplus.plugins.pagination.Pagination; //导入依赖的package包/类
/**
* 查询SQL拼接Order By
*
* @param originalSql 需要拼接的SQL
* @param page page对象
* @param orderBy 是否需要拼接Order By
* @return
*/
public static String concatOrderBy(String originalSql, Pagination page, boolean orderBy) {
if (orderBy && page.isOpenSort()) {
StringBuilder buildSql = new StringBuilder(originalSql);
String ascStr = concatOrderBuilder(page.getAscs(), " ASC");
String descStr = concatOrderBuilder(page.getDescs(), " DESC");
if (StringUtils.isNotEmpty(ascStr) && StringUtils.isNotEmpty(descStr)) {
ascStr += ", ";
}
if (StringUtils.isNotEmpty(ascStr) || StringUtils.isNotEmpty(descStr)) {
buildSql.append(" ORDER BY ").append(ascStr).append(descStr);
}
return buildSql.toString();
}
return originalSql;
}
示例3: intercept
import com.baomidou.mybatisplus.plugins.pagination.Pagination; //导入依赖的package包/类
/**
* Physical Pagination Interceptor for all the queries with parameter {@link org.apache.ibatis.session.RowBounds}
*/
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
// 先判断是不是SELECT操作
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
return invocation.proceed();
}
RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
/* 不需要分页的场合 */
if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
return invocation.proceed();
}
BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
String originalSql = boundSql.getSql();
Connection connection = (Connection) invocation.getArgs()[0];
DBType dbType = JdbcUtils.getDbType(connection.getMetaData().getURL());
if (rowBounds instanceof Pagination) {
Pagination page = (Pagination) rowBounds;
if (page.isSearchCount()) {
this.queryTotal(JsqlParserUtils.jsqlparserCount(originalSql), mappedStatement, boundSql, page, connection);
if (page.getTotal() <= 0) {
return invocation.proceed();
}
}
originalSql = DialectFactory.buildPaginationSql(page, originalSql, dbType, null);
} else {
// support physical Pagination for RowBounds
originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dbType, null);
}
/*
* <p> 禁用内存分页 </p> <p> 内存分页会查询所有结果出来处理(这个很吓人的),如果结果变化频繁这个数据还会不准。</p>
*/
metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
return invocation.proceed();
}
示例4: main
import com.baomidou.mybatisplus.plugins.pagination.Pagination; //导入依赖的package包/类
/**
* 测试 Mybatis XML 修改自动刷新
*/
public static void main(String[] args) throws IOException, InterruptedException {
InputStream in = UserMapperTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
mf.setGlobalConfig(new GlobalConfiguration(new MySqlInjector()));
Resource[] resource = new ClassPathResource[]{new ClassPathResource("mysql/UserMapper.xml")};
SqlSessionFactory sessionFactory = mf.build(in);
new MybatisMapperRefresh(resource, sessionFactory, 0, 5);
boolean isReturn = false;
SqlSession session = null;
while (!isReturn) {
try {
session = sessionFactory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
userMapper.selectListRow(new Pagination(1, 10));
resource[0].getFile().setLastModified(SystemClock.now());
session.commit();
session.close();
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
Thread.sleep(5000);
}
}
System.exit(0);
}
示例5: intercept
import com.baomidou.mybatisplus.plugins.pagination.Pagination; //导入依赖的package包/类
/**
* Physical Pagination Interceptor for all the queries with parameter
* {@link org.apache.ibatis.session.RowBounds}
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget();
if (target instanceof StatementHandler) {
return super.intercept(invocation);
} else {
RowBounds rowBounds = (RowBounds) invocation.getArgs()[2];
if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
return invocation.proceed();
}
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Executor executor = (Executor) invocation.getTarget();
Connection connection = executor.getTransaction().getConnection();
Object parameterObject = invocation.getArgs()[1];
BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
String originalSql = boundSql.getSql();
if (rowBounds instanceof Pagination) {
Pagination page = (Pagination) rowBounds;
if (page.isSearchCount()) {
SqlInfo sqlInfo = SqlUtils.getCountOptimize(sqlParser, originalSql);
super.queryTotal(overflowCurrent, sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
if (page.getTotal() <= 0) {
return invocation.proceed();
}
}
}
}
return invocation.proceed();
}
示例6: main
import com.baomidou.mybatisplus.plugins.pagination.Pagination; //导入依赖的package包/类
/**
* 测试 Mybatis XML 修改自动刷新
*/
public static void main(String[] args) throws Exception {
Resource[] resource = new ClassPathResource[]{new ClassPathResource("mysql/UserMapper.xml")};
SqlSessionFactory sessionFactory = new CrudTest().sqlSessionFactory();
new MybatisMapperRefresh(resource, sessionFactory, 0, 5, true);
boolean isReturn = false;
SqlSession session = null;
while (!isReturn) {
try {
session = sessionFactory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
userMapper.selectListRow(new Pagination(1, 10));
resource[0].getFile().setLastModified(SystemClock.now());
session.commit();
session.close();
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
Thread.sleep(5000);
}
}
System.exit(0);
}
示例7: intercept
import com.baomidou.mybatisplus.plugins.pagination.Pagination; //导入依赖的package包/类
/**
* Physical Pagination Interceptor for all the queries with parameter {@link org.apache.ibatis.session.RowBounds}
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
this.sqlParser(metaObject);
// 先判断是不是SELECT操作
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
return invocation.proceed();
}
RowBounds rowBounds = (RowBounds) metaObject.getValue("delegate.rowBounds");
/* 不需要分页的场合 */
if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
// 本地线程分页
if (localPage) {
// 采用ThreadLocal变量处理的分页
rowBounds = PageHelper.getPagination();
if (rowBounds == null) {
return invocation.proceed();
}
} else {
// 无需分页
return invocation.proceed();
}
}
// 针对定义了rowBounds,做为mapper接口方法的参数
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
String originalSql = boundSql.getSql();
Connection connection = (Connection) invocation.getArgs()[0];
DBType dbType = StringUtils.isNotEmpty(dialectType) ? DBType.getDBType(dialectType) : JdbcUtils.getDbType(connection.getMetaData().getURL());
if (rowBounds instanceof Pagination) {
Pagination page = (Pagination) rowBounds;
boolean orderBy = true;
if (page.isSearchCount()) {
SqlInfo sqlInfo = SqlUtils.getCountOptimize(sqlParser, originalSql);
orderBy = sqlInfo.isOrderBy();
this.queryTotal(overflowCurrent, sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
if (page.getTotal() <= 0) {
return invocation.proceed();
}
}
String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
originalSql = DialectFactory.buildPaginationSql(page, buildSql, dbType, dialectClazz);
} else {
// support physical Pagination for RowBounds
originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dbType, dialectClazz);
}
/*
* <p> 禁用内存分页 </p>
* <p> 内存分页会查询所有结果出来处理(这个很吓人的),如果结果变化频繁这个数据还会不准。</p>
*/
metaObject.setValue("delegate.boundSql.sql", originalSql);
metaObject.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
metaObject.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
return invocation.proceed();
}
示例8: selectUserPage
import com.baomidou.mybatisplus.plugins.pagination.Pagination; //导入依赖的package包/类
List<Map<String, Object>> selectUserPage(Pagination page, Map<String, Object> params);