项目使用Mybatis作为持久化框架时,我们可以使用一些别人再次封装的一些插件来提高开发效率,使用较多的如通用mapper,PageHelper等,这次介绍一个更好用的插件Mybatis-Plus,感觉这个比较像通用mapper+PageHelper,并且分页方面还可以选出总条数。
Mybatis-plus官网->https://baomidou.gitee.io/mybatis-plus-doc/#/
首先给出项目结构
1 初始化和配置
首先更新pom.xml
com.baomidou
mybatis-plus-boot-starter
3.1.2
2 配置application.yml
server:
port: 8082
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
global-config:
# 关闭MP3.0自带的banner
banner: false
db-config:
# 默认数据库表下划线命名
table-underline: true
3 编写application.java 添加分页配置
package cn.yunlingfly.springbootmybatisplus;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@MapperScan("cn.yunlingfly.springbootmybatisplus.infra.mapper")
public class SpringbootMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
}
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
4 编写Mapper
package cn.yunlingfly.springbootmybatisplus.infra.mapper;
import cn.yunlingfly.springbootmybatisplus.api.dto.TestDTO;
import cn.yunlingfly.springbootmybatisplus.domain.entity.TestEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Component
public interface ITestMapper extends BaseMapper {
IPage getPageByName(IPage page, @Param("name") String name);
}
5 编写Service接口
package cn.yunlingfly.springbootmybatisplus.api.service;
import cn.yunlingfly.springbootmybatisplus.api.dto.TestDTO;
import cn.yunlingfly.springbootmybatisplus.domain.entity.TestEntity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
public interface ITestService extends IService {
IPage getPageByName(Page page, String name);
}
6 编写Service实现类
package cn.yunlingfly.springbootmybatisplus.api.service.impl;
import cn.yunlingfly.springbootmybatisplus.api.dto.TestDTO;
import cn.yunlingfly.springbootmybatisplus.api.service.ITestService;
import cn.yunlingfly.springbootmybatisplus.domain.entity.TestEntity;
import cn.yunlingfly.springbootmybatisplus.infra.mapper.ITestMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl extends ServiceImpl implements ITestService {
@Autowired
private ITestMapper testMapper;
public IPage getPageByName(Page page, String name) {
return testMapper.getPageByName(page, name);
}
}
7 编写Controller
package cn.yunlingfly.springbootmybatisplus.api.controller;
import cn.yunlingfly.springbootmybatisplus.api.dto.Result;
import cn.yunlingfly.springbootmybatisplus.api.dto.TestDTO;
import cn.yunlingfly.springbootmybatisplus.api.service.ITestService;
import cn.yunlingfly.springbootmybatisplus.domain.entity.TestEntity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class TestController {
@Autowired
private ITestService testService;
@RequestMapping(value = "/getOne/{id}", method = RequestMethod.GET)
public Result getOne(@PathVariable String id) {
Result result = new Result<>();
TestEntity aa = testService.getById(id);
if (aa == null) {
result.error500("未找到对应实体");
} else {
result.setResult(aa);
result.setSuccess(true);
}
return result;
}
@RequestMapping(value = "/list/{pageNum}/{pageSize}", method = RequestMethod.GET)
public Result> queryPageList(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
Result> result = new Result<>();
// IPage的convert()方法做包装
IPage pageList = testService.getPageByName(new Page(pageNum, pageSize), "aa");
result.setSuccess(true);
result.setResult(pageList);
return result;
}
}
访问http://localhost:8082/list/1/1,查看返回值,可以看到total-总数,current-当前页码,pages-总页数,records-结果集
{
"success": true,
"message": "操作成功!",
"code": 0,
"result": {
"records": [{
"id": "3"
}],
"total": 3,
"size": 1,
"current": 1,
"orders": [],
"searchCount": true,
"pages": 3
},
"timestamp": 1567136573151
}
最后放上笔者的该项目github仓库地址,有需要的可以当测试:https://github.com/Yunlingfly/springboot-mybatis-plus