public List<User> selectUsers(Integer role, PageSort page) { log.info("批量查询用户信息,查询的角色权限:" + role); List<User> usersList = new ArrayList<User>(); int offset = (page.getPageNum() - 1) * page.getPageSize(); int limit = page.getPageSize(); RowBounds rowBounds = new RowBounds(offset, limit); List<User> userList = userInfoMapper.selectByRoleAndPage(role, rowBounds); return usersList; } @SelectProvider(type = UserSqlProvider.class, method = "selectByRoleAndPage") List<User> selectByRoleAndPage(@Param("role") Integer role, RowBounds rowBounds); public class UserSqlProvider { public String selectByRoleAndPage(Integer role, RowBounds rowBounds) { String sql = "SELECT * FROM user WHERE 1=1"; if (role != -1) { sql += " AND role = #{role}"; } sql += " LIMIT #{1.offset}, #{1.limit}"; return sql; } } 报错Error invoking SqlProvider method (com.ruifan.taihaolian.biz.mapper.provider.UserSqlProvider.selectByRoleAndPage). Cause: java.lang.IllegalArgumentException: wrong number of arguments报错was not registered for synchronization because synchronization is not active
时间: 2024-04-06 14:30:04 浏览: 48
这段代码中存在一个问题,可能导致报错。在 UserSqlProvider 类的 selectByRoleAndPage 方法中,SQL 语句中的参数占位符应该是 #{0} 和 #{1},而不是 #{role} 和 #{1}。请将代码中的这一行:
sql += " LIMIT #{1.offset}, #{1.limit}";
修改为:
sql += " LIMIT #{0.offset}, #{0.limit}";
另外,报错信息中提到“synchronization is not active”,这可能是因为没有开启 MyBatis 的事务管理。如果需要在方法中使用事务,请在方法上添加 @Transactional 注解。
相关问题
请将这个方法public Page<User> selectUsers(Integer role, PageSort page) { log.info("批量查询用户信息,查询的角色权限:" + role); PageHelper.startPage(page.getPageNum(), page.getPageSize()); List<User> userList = null; if(role == -1){ userList = userInfoMapper.selectAll(); } else { User user = User.builder().role(role).build(); userList = userInfoMapper.select(user); } log.info("批量查询用户信息,查询的角色权限:" + role); return (Page<User>) userList; }中的查询结果去去调用pageQuery 方法
好的,这里是将 `selectUsers` 方法中的查询结果转换为调用 `pageQuery` 方法的示例代码:
```java
public PageInfo<User> selectUsers(Integer role, PageSort page) {
log.info("批量查询用户信息,查询的角色权限:" + role);
PageQueryInterface pageQuery = new PageQuery(page.getPageNum(), page.getPageSize(), page.getSort(), page.getOrder());
PageInfo<User> pageInfo = pageQuery(pageQuery, () -> {
List<User> userList = null;
if (role == -1) {
userList = userInfoMapper.selectAll();
} else {
User user = User.builder().role(role).build();
userList = userInfoMapper.select(user);
}
return userList;
});
log.info("批量查询用户信息,查询的角色权限:" + role);
return pageInfo;
}
```
在这个方法中,我们首先将 `PageSort` 对象转换为 `PageQuery` 对象,然后调用 `pageQuery` 方法进行分页查询。在 `pageQuery` 方法的第二个参数中,我们使用 Lambda 表达式来提供数据供应者 `supplier`。该 Lambda 表达式中的具体实现和 `selectUsers` 方法中的查询逻辑是一样的,只不过在返回查询结果时,我们将其封装成了 `PageInfo` 对象并返回。
public List<User> selectUsers(Integer role, PageSort page) { log.info("批量查询用户信息,查询的角色权限:" + role); List<User> usersList = new ArrayList<User>(); int offset = (page.getPageNum() - 1) * page.getPageSize(); int limit = page.getPageSize(); RowBounds rowBounds = new RowBounds(offset, limit); List<User> userList = userInfoMapper.selectByRoleAndPage(role, rowBounds); return usersList; } @SelectProvider(type = UserSqlProvider.class, method = "selectByRoleAndPage") List<User> selectByRoleAndPage(@Param("role") Integer role, RowBounds rowBounds); public class UserSqlProvider { public String selectByRoleAndPage(Integer role, RowBounds rowBounds) { String sql = "SELECT * FROM user WHERE 1=1"; if (role != -1) { sql += " AND role = #{0}"; } sql += " LIMIT #{1.offset}, #{1.limit}"; return sql; } }方法selectByRoleAndPage没有关联上
selectUsers方法,应该在UserSqlProvider类中添加@Param注解来指定参数名称,如下所示:
```
public String selectByRoleAndPage(@Param("role") Integer role, @Param("rowBounds") RowBounds rowBounds) {
String sql = "SELECT * FROM user WHERE 1=1";
if (role != -1) {
sql += " AND role = #{role}";
}
sql += " LIMIT #{rowBounds.offset}, #{rowBounds.limit}";
return sql;
}
```
同时,在selectUsers方法中,应该将userList赋值给usersList,如下所示:
```
public List<User> selectUsers(Integer role, PageSort page) {
log.info("批量查询用户信息,查询的角色权限:" + role);
List<User> usersList = new ArrayList<User>();
int offset = (page.getPageNum() - 1) * page.getPageSize();
int limit = page.getPageSize();
RowBounds rowBounds = new RowBounds(offset, limit);
List<User> userList = userInfoMapper.selectByRoleAndPage(role, rowBounds);
usersList.addAll(userList);
return usersList;
}
```
阅读全文