目录
1.添加依赖
2.配置分页拦截器
3.条件查询(MVC架构示例)
(2)数据传输对象DTO
(3)Mapper接口
(4)Mapper.xml
(5)Service层
(6)Controller层
(7)sql创建
4.结果示例
(1)以price为3000.0时查出的结果为例
(2)不添加任何条件时默认全部查询
com.baomidou
mybatis-plus-boot-starter
3.4.3.3
注意:版本最好是3.4以上的
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;
}
}
(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();
}
}
DTO可以理解成是用来封装条件查询属性的集合
这里以下面三个属性作为条件进行查询
import lombok.Data;
@Data
public class ProductDTO {
private Integer pid;
private String pname;
private Double price;
}
注意这里一定要继承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 {
IPage selectProduct(IPage page, @Param("productDTO")ProductDTO productDTO);
}
这里的条件查询可以自定义,例如增加模糊查询
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 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 selectProduct(int pageNum, int pageSize, @Param("productDTO")ProductDTO productDTO) {
Page page = new Page<>(pageNum, pageSize);
// 非空判断
if (productDTO == null) {
productDTO = new ProductDTO();
}
return productMapper.selectProduct(page,productDTO);
}
}
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是统一封装返回结果集,可以自定义,也可以参考我之前的博客。
create table product
(
pid int auto_increment
primary key,
pname varchar(20) not null,
price double null,
category_id varchar(20) null
);
这里自定义添加数据