mybatis-plus模糊分页查询
时间: 2023-04-26 10:03:03 浏览: 430
mybatis-plus模糊分页查询可以通过使用Wrapper构建查询条件,然后使用Page对象进行分页查询。其中,模糊查询可以使用like方法,例如:
```
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("username", "张三");
Page<User> page = new Page<>(1, 10);
IPage<User> userPage = userMapper.selectPage(page, wrapper);
```
上述代码中,使用QueryWrapper构建查询条件,使用like方法进行模糊查询,然后使用Page对象进行分页查询,最后使用selectPage方法进行查询。查询结果会返回一个IPage对象,其中包含了查询结果和分页信息。
相关问题
mybatis-plus分页模糊查询
使用Mybatis-Plus进行分页模糊查询,可以通过以下步骤实现:
1. 在Mapper接口中定义分页查询方法,使用@Select注解指定SQL语句,使用@Param注解指定参数。
2. 在SQL语句中使用LIKE关键字进行模糊查询,使用LIMIT关键字进行分页查询。
3. 在Service层调用Mapper接口中定义的分页查询方法,传入分页参数和模糊查询参数。
4. 在Controller层中接收前端传来的分页参数和模糊查询参数,调用Service层中的方法进行查询。
5. 将查询结果封装成Page对象返回给前端。
示例代码:
Mapper接口:
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT * FROM user WHERE name LIKE CONCAT('%',#{name},'%') LIMIT #{offset},#{limit}")
List<User> selectUserList(@Param("name") String name, @Param("offset") int offset, @Param("limit") int limit);
}
Service层:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Page<User> getUserList(String name, int pageNum, int pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
List<User> userList = userMapper.selectUserList(name, page.getOffset(), page.getLimit());
page.setRecords(userList);
return page;
}
}
Controller层:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public Page<User> getUserList(@RequestParam(required = false) String name,
@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
return userService.getUserList(name, pageNum, pageSize);
}
}
springboot 整合 mybatis-plus 分页 并查询mapper.xml
好的,下面是 Spring Boot 整合 MyBatis-Plus 进行分页查询的示例代码:
首先,需要在 pom.xml 中添加 MyBatis-Plus 和 MySQL 的依赖:
```xml
<dependencies>
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
```
然后,需要在 application.properties 中配置 MySQL 数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
接下来,创建一个实体类和对应的 Mapper 接口:
```java
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
IPage<User> selectUserPage(Page<User> page, @Param("name") String name);
}
```
其中,User 类是实体类,UserMapper 是 Mapper 接口,继承了 MyBatis-Plus 的 BaseMapper 接口。
注意,在 UserMapper 接口中,我们定义了一个名为 selectUserPage 的方法,它接受一个 Page 对象和一个 name 参数,返回一个 IPage 对象。这个方法将在 Controller 中调用,用于分页查询用户信息。
最后,编写 Controller 类,通过调用 UserMapper 中定义的 selectUserPage 方法来进行分页查询:
```java
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/users")
public IPage<User> getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String name) {
Page<User> page = new Page<>(pageNum, pageSize);
return userMapper.selectUserPage(page, name);
}
}
```
在 getUserList 方法中,我们首先创建了一个 Page 对象,表示要查询的分页信息。然后,调用 userMapper.selectUserPage 方法来进行分页查询,并将结果返回给前端。
最后,这里提供一个 Mapper.xml 文件的示例,用于实现分页查询:
```xml
<select id="selectUserPage" resultMap="BaseResultMap">
select *
from user
<if test="name != null">
where name like concat('%', #{name}, '%')
</if>
<if test="orderBy != null">
order by ${orderBy}
</if>
</select>
```
其中,selectUserPage 方法的实现与上面的示例代码类似,这里不再赘述。注意,这里使用了 MyBatis 的动态 SQL 功能,根据传入的参数来动态生成 SQL 语句。具体来说,如果传入了 name 参数,则会在 SQL 语句中添加一个 where 子句,用于按照用户名进行模糊查询;如果传入了 orderBy 参数,则会在 SQL 语句中添加一个 order by 子句,用于按照指定字段排序。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)