rowbounds default = new rowbounds();
时间: 2023-09-03 16:02:16 浏览: 38
RowBounds是MyBatis中用于分页查询的类。它用于指定查询结果所返回的行数范围。当我们不显式指定RowBounds时,默认创建一个空的RowBounds对象。
RowBounds对象有两个重要的属性:offset和limit。offset表示查询结果的偏移量,即从第几行开始返回结果;limit表示每次查询返回的最大行数。
当我们创建一个空的RowBounds对象时,默认的offset为0,即从第一行开始返回结果。limit为Integer.MAX_VALUE,表示返回所有满足条件的行。
在使用MyBatis进行分页查询时,可以通过RowBounds对象来指定查询的起始位置和返回的行数,以达到分页的效果。
另外,MyBatis还提供了一个带参数的RowBounds构造方法,可以通过传入offset和limit的值来创建指定范围的RowBounds对象。
总结来说,当我们不显式指定RowBounds时,默认的RowBounds对象会返回所有满足条件的行,并从第一行开始返回结果。
相关问题
分页拦截器配置类 代码
这是一个基于 MyBatis 的分页拦截器配置类示例,可以用于实现分页查询:
```java
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Properties;
@Intercepts(@Signature(type = org.apache.ibatis.executor.Executor.class, method = "query", args = {org.apache.ibatis.mapping.MappedStatement.class, Object.class, RowBounds.class, org.apache.ibatis.session.ResultHandler.class}))
@Component
public class PaginationInterceptor implements Interceptor {
private static final String PAGE = "page";
private static final String ROWS = "rows";
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
RowBounds rowBounds = (RowBounds) args[2];
if (rowBounds != RowBounds.DEFAULT) {
// 如果是分页查询,则进行分页处理
args[2] = new RowBounds(getOffset(rowBounds), rowBounds.getLimit());
// 将分页参数设置到 ThreadLocal 中
setPageParameter(rowBounds.getOffset(), rowBounds.getLimit());
}
return invocation.proceed();
}
private void setPageParameter(int offset, int limit) {
Map<String, Object> pageParams = PageContextHolder.getPageParams();
if (pageParams != null) {
pageParams.put(PAGE, offset / limit + 1);
pageParams.put(ROWS, limit);
}
}
private int getOffset(RowBounds rowBounds) {
return rowBounds.getOffset() <= 0 ? 0 : rowBounds.getOffset();
}
@Override
public Object plugin(Object o) {
return o instanceof org.apache.ibatis.executor.Executor ? org.apache.ibatis.plugin.Plugin.wrap(o, this) : o;
}
@Override
public void setProperties(Properties properties) {
}
}
```
在这个配置类中,我们定义了一个 `PaginationInterceptor` 拦截器,它实现了 `Interceptor` 接口,可以拦截 MyBatis 的查询方法。在 `intercept` 方法中,我们判断是否为分页查询,如果是,则进行分页处理并将分页参数设置到 `ThreadLocal` 中。在 `setPageParameter` 方法中,我们将分页参数设置到 `Map<String, Object>` 类型的 `PageContextHolder` 中,这个类是一个自定义的上下文工具类,用于存储分页参数。在 `plugin` 方法中,我们将拦截器包装成一个插件,用于拦截 MyBatis 的查询方法,最后在 Spring 容器中将这个拦截器配置为一个 Bean。
mybatis使用拦截器分页
Mybatis提供了一个拦截器(Interceptor)接口,可以在执行SQL语句之前或之后对其进行拦截和修改。我们可以利用这个拦截器来实现分页功能。
实现步骤如下:
1. 定义一个类实现Interceptor接口,并重写它的intercept方法,该方法会在执行SQL语句之前或之后被调用。
```java
public class PageInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// TODO: 实现分页逻辑
return invocation.proceed();
}
// ...
}
```
2. 在intercept方法中获取Mapper方法的参数和返回值,判断是否需要进行分页,并修改SQL语句和参数。
```java
public class PageInterceptor implements Interceptor {
// ...
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 获取Mapper方法的参数和返回值
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
Object parameterObject = args[1];
RowBounds rowBounds = (RowBounds) args[2];
// 判断是否需要进行分页
if (rowBounds != RowBounds.DEFAULT) {
BoundSql boundSql = ms.getBoundSql(parameterObject);
String sql = boundSql.getSql();
// 修改SQL语句
String pageSql = getPageSql(sql, rowBounds);
BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql,
boundSql.getParameterMappings(), parameterObject);
// 修改参数
Object additionalParameters = BoundSqlUtils.copyAdditionalParameters(boundSql);
BoundSqlUtils.setAdditionalParameter(pageBoundSql, additionalParameters);
args[0] = copyFromMappedStatement(ms, pageBoundSql);
args[2] = RowBounds.DEFAULT;
}
return invocation.proceed();
}
// ...
}
```
3. 实现getPageSql方法,根据原始SQL语句和分页参数生成新的SQL语句。
```java
private String getPageSql(String sql, RowBounds rowBounds) {
StringBuilder pageSql = new StringBuilder(sql);
if (rowBounds instanceof PageRowBounds) {
int offset = ((PageRowBounds) rowBounds).getOffset();
int limit = rowBounds.getLimit();
pageSql.append(" limit ").append(offset).append(",").append(limit);
} else {
pageSql.append(" limit ").append(rowBounds.getOffset()).append(",")
.append(rowBounds.getLimit());
}
return pageSql.toString();
}
```
4. 在Mybatis的配置文件中配置拦截器,并指定需要拦截的Mapper方法。
```xml
<plugins>
<plugin interceptor="com.example.PageInterceptor">
<property name="dialect" value="mysql" />
</plugin>
</plugins>
<mappers>
<mapper resource="com/example/MyMapper.xml">
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.example.MyExample">
<!-- 拦截该Mapper方法 -->
<interceptor type="com.example.PageInterceptor" />
select
<include refid="Base_Column_List" />
from my_table
<where>
<include refid="MyExample_Where_Clause" />
</where>
</select>
</mapper>
</mappers>
```
这样,我们就可以在Mapper方法中使用RowBounds参数实现分页了。例如:
```java
List<User> list = sqlSession.selectList("com.example.UserMapper.selectByExample",
new UserExample(), new PageRowBounds(1, 10));
```
其中,PageRowBounds是RowBounds的子类,用于指定分页的起始位置和页大小。需要注意的是,如果使用PageRowBounds,Mybatis会先查询所有的记录,然后再截取指定的范围,因此对于大数据量的查询可能会影响性能。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)