mybatis-plus

本文介绍了如何在SpringBoot项目中使用MyBatisPlus进行数据库操作,包括依赖配置、实体类定义、接口扩展、基础CRUD功能测试以及使用QueryWrapper实现复杂的查询条件和分页功能。
摘要由CSDN通过智能技术生成

依赖

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

配置:

spring:
  application:
    name: itemservice
  datasource:
    url: jdbc:mysql://localhost:3306/heimamanage?useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config: 
    db-config: # 打个table 全局配置表和数据库映射 表  id 自增  table 上面加tbl_
      id-type: auto 
      table-prefix: tbl_

编写接口 继承接口baseMapper<item>

@Mapper
public interface ItemMapper extends BaseMapper<Item> {
}

实体类:

package com.itheima.mplus.pojo;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Date;

@Data
@TableName("tb_item")
public class Item {
    @TableId(type = IdType.AUTO)
    @ExcelProperty(value = "id",index = 0)
    private Long id;//商品id
    @ExcelProperty(value = "商品名称",index = 1)
    private String name;//商品名称
    @ExcelProperty(value = "价格",index = 2)
    private Long price;//价格(分)
    @ExcelProperty(value = "库存数量",index = 3)
    private Integer stock;//库存数量
    @ExcelProperty(value = "商品图片",index = 4)
    private String image;//商品图片
    @ExcelProperty(value = "分类名称",index = 5)
    private String category;//分类名称
    @ExcelProperty(value = "品牌名称",index = 6)
    private String brand;//品牌名称
    @ExcelProperty(value = "规格",index = 7)
    private String spec;//规格
    @ExcelProperty(value = "销量",index = 8)
    private Integer sold;//销量
    @ExcelProperty(value = "评论数",index = 9)
    private Integer commentCount;//评论数
    @ExcelProperty(value = "商品状态",index = 10)
    private Integer status;//商品状态 1-正常,2-下架
    @TableField("isAD")
    @ExcelProperty(value = "商品状态",index = 11)
    private Boolean isAD;//商品状态 1-正常,2-下架
    @ExcelProperty(value = "创建时间",index = 12)
    private Date createTime;//创建时间
    @ExcelProperty(value = "更新时间",index = 13)
    private Date updateTime;//更新时间
}
测试类测试例如查找:
 @Autowired
    private ItemMapper itemMapper;

    @Test //junit jupiter  // sql
    public void testSelectAll(){
      List<Item> items = itemMapper.selectList(null); // null 则代表查找全部数据
        System.out.println(items.size());
    }

增删改查功能测试:

例如: update 查找  和修改 都用 继承了baseMapper 的itemMapper 进行操作 : 

 @Test
    public  void testUpdateById(){
        Item item = itemMapper.selectById(100002672304L); // 加L
        System.out.println("修改前"+item);
        item.setPrice(item.getPrice()-10000);
        itemMapper.updateById(item);
        Item item1 = itemMapper.selectById(100002672304L);
        System.out.println("修改后"+item1);
    }

###### 分页功能: ----------------------------- 之前都用的是pageHelper 依赖的工具进行操作

  /*分页查询*/
    @Test
    public  void testpage(){

        int pageSize = 10;// 每页大小
        int pageNum = 2; // 当前页
        IPage<Item> page = new Page<>(pageNum,pageSize);
        itemMapper.selectPage(page,null);
        List<Item> records = page.getRecords();
        System.out.println(records);
        System.out.println("总页数:"+page.getPages());
        System.out.println("总条数:"+page.getTotal());
    }

多条件查询 查询 item 表中的

/* price >=100 and price<=200*/
 @Test
    public  void testQueryWrapper(){
// 条件查询
/*
        QueryWrapper<Item> qw = new QueryWrapper<>();
        qw.ge("price",100);
        qw.lt("price",200);
        // 支持链式编程
//        qw.ge().It();
        List<Item> items = itemMapper.selectList(qw);
        for (Item item : items) {
            System.out.println(item);
        }
*/
/*        QueryWrapper<Item> qw = new QueryWrapper<>();
       qw.lambda().ge(Item::getPrice, 100).lt(Item::getPrice, 200);
        // 支持链式编程
//        qw.ge().It();
        List<Item> items = itemMapper.selectList(qw);
        for (Item item : items) {
            System.out.println(item);
        }*/

        LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>(); // Lamber 表达式 简写
        qw.ge(Item::getPrice, 100).lt(Item::getPrice, 200); // 链式编程 直接一步到位
        // 支持链式编程
//        qw.ge().It();
        List<Item> items = itemMapper.selectList(qw);
        for (Item item : items) {
            System.out.println(item);
        }
        
        
        

最后一个 比较特别要写在() 内 则 lamber  new c 直接出来

   LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();

        qw.eq(Item::getBrand, "飞利浦").and(itemLambdaQueryWrapper -> itemLambdaQueryWrapper.eq(Item::getCategory,"手机").or().lt(Item::getPrice, 500));
        IPage<Item> page= new Page<>(1,10);
        itemMapper.selectPage(page,qw);
        System.out.println(page.getRecords());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值