目录
1.添加依赖
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.3</version>
</dependency>
注意:版本最好是3.4以上的
2.配置分页拦截器
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页拦截器
PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor();
// 设置请求页数大于最大页时的操作:true 调回到首页,false 继续请求,默认为 false
innerInterceptor.setOverflow(false);
// 设置单页的限制数量,-1 表示不限制
innerInterceptor.setMaxLimit(500L);
interceptor.addInnerInterceptor(innerInterceptor);
return interceptor;
}
}
3.条件查询(MVC架构示例)
(1)实体类Product
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
public class Product implements Serializable {
private Integer pid;
private String pname;
private Double price;
@TableField(value = "category_id")
private String categoryId;
private static final long serialVersionUID = 1L;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname == null ? null : pname.trim();
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId == null ? null : categoryId.trim();
}
}
(2)数据传输对象DTO
DTO可以理解成是用来封装条件查询属性的集合
这里以下面三个属性作为条件进行查询
import lombok.Data;
@Data
public class ProductDTO {
private Integer pid;
private String pname;
private Double price;
}
(3)Mapper接口
注意这里一定要继承Mybatis-plus提供的BaseMapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhan.springboot.springbootmybatis.pojo.DTO.ProductDTO;
import com.zhan.springboot.springbootmybatis.pojo.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
IPage<Product> selectProduct(IPage<Product> page, @Param("productDTO")ProductDTO productDTO);
}
(4)Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhan.springboot.springbootmybatis.mapper.ProductMapper">
<select id="selectProduct" resultType="com.zhan.springboot.springbootmybatis.pojo.Product">
select *
from springboot.product
<where>
<if test="productDTO.pid != null and productDTO.pid != ''">pid=#{productDTO.pid}</if>
<if test="productDTO.pname != null and productDTO.pname != ''">And pname=#{productDTO.pname}</if>
<if test="productDTO.price != null and productDTO.price != ''">And price=#{productDTO.price}</if>
</where>
</select>
这里的条件查询可以自定义,例如增加模糊查询
(5)Service层
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhan.springboot.springbootmybatis.pojo.DTO.ProductDTO;
import com.zhan.springboot.springbootmybatis.pojo.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface ProductService {
IPage<Product> selectProduct(int pageNum, int pageSize, @Param("productDTO")ProductDTO productDTO);
}
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhan.springboot.springbootmybatis.mapper.ProductMapper;
import com.zhan.springboot.springbootmybatis.pojo.DTO.ProductDTO;
import com.zhan.springboot.springbootmybatis.pojo.Product;
import com.zhan.springboot.springbootmybatis.service.ProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper productMapper;
@Override
public IPage<Product> selectProduct(int pageNum, int pageSize, @Param("productDTO")ProductDTO productDTO) {
Page<Product> page = new Page<>(pageNum, pageSize);
// 非空判断
if (productDTO == null) {
productDTO = new ProductDTO();
}
return productMapper.selectProduct(page,productDTO);
}
}
(6)Controller层
import com.zhan.springboot.springbootmybatis.Common.AjaxResult;
import com.zhan.springboot.springbootmybatis.pojo.DTO.ProductDTO;
import com.zhan.springboot.springbootmybatis.pojo.Product;
import com.zhan.springboot.springbootmybatis.service.ProductService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@ResponseBody
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@PostMapping
public AjaxResult selectProduct(@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "5")int pageSize, @RequestBody(required = false)ProductDTO productDTO){
return AjaxResult.success(productService.selectProduct(pageNum,pageSize,productDTO));
}
}
注意这里的AjaxResult是统一封装返回结果集,可以自定义,也可以参考我之前的博客。
(7)sql创建
create table product
(
pid int auto_increment
primary key,
pname varchar(20) not null,
price double null,
category_id varchar(20) null
);
这里自定义添加数据