springboot实现mybatisplus按照部门实现分页查询
时间: 2023-11-01 14:05:46 浏览: 114
1. 首先需要在pom.xml中添加mybatisplus和分页插件的依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
```
2. 在application.yml中配置mybatisplus和分页插件的参数:
```
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
field-strategy: not_empty
table-prefix: sys_
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
```
3. 编写Mapper接口:
```
@Mapper
public interface DeptMapper extends BaseMapper<Dept> {
List<Dept> selectPage(Page<Dept> page, @Param("deptName") String deptName);
}
```
4. 编写Service实现类:
```
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
@Override
public IPage<Dept> selectPage(Page<Dept> page, String deptName) {
QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
if (StringUtils.isNotBlank(deptName)) {
queryWrapper.like("dept_name", deptName);
}
return baseMapper.selectPage(page, queryWrapper);
}
}
```
5. 编写Controller:
```
@RestController
@RequestMapping("/dept")
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/list")
public Result list(Page<Dept> page, String deptName) {
IPage<Dept> data = deptService.selectPage(page, deptName);
return Result.success(data);
}
}
```
6. 在前端页面中,可以通过ajax请求/list接口,传入参数page和deptName实现分页查询。
阅读全文