CGB2111SpingBoot第三天

1.商品分类

1.1商品分类修改
编辑ItemCatController

/**
     * url地址:/itemCat/status/{id}/{status}
     * 参数:id/status
     * 返回值:SysResult对象
     */
    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(ItemCat itemCat){

        itemCatService.updateStatus(itemCat);
        return SysResult.success();
    }

编辑ItemCatService

@Update("update item_cat set status=#{status},updated=#{updated} where id=#{id}")
    void updateStatus(ItemCat itemCat);

ItemCatMapper

@Update("update item_cat set status=#{status},updated=#{updated} where id=#{id}")
    void updateStatus(ItemCat itemCat);

1.2商品分类修改操作
商品分类有父子关系,所以在修改操作中,不能提供修改父子的操作,应该如果提供则后期的代码维护 将会翻倍的提升. 所以如果需要修改父子关系,则一般先删除,再新增
总结: 以后父子关系的修改操作, 需要留意!!!
编辑ItemCatController

@PutMapping("/updateItemCat")
    public SysResult updateItemCat(@RequestBody ItemCat itemCat){

        itemCatService.updateItemCat(itemCat);
        return  SysResult.success();
    }

编辑ItemCatService

 @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        //只需要修改name/updated
        itemCat.setUpdated(new Date());
        itemCatMapper.updateItemCat(itemCat);
    }

编辑ItemCatMapper

 @Update("update item_cat set name=#{name}," +
            "updated=#{updated} where id=#{id}")
    void updateItemCat(ItemCat itemCat);

1.3商品分类删除操作

当有123级菜单时需要注意细节:
比如用户删除三级菜单则直接删除三级
用户删除二级菜单需要先删除三级菜单才可以删除二级
以此类推

编辑ItemCatController

 /**
     * 业务说明:删除商品分类信息
     * url: /itemCat/deleteItemCat?id=xx&level=xx
     * 参数: id/level
     * 返回值: SysResult对象
     */
    @DeleteMapping("/deleteItemCat")
    public SysResult deleteItemCat(ItemCat itemCat){

        itemCatService.deleteItemCat(itemCat);
        return SysResult.success();
    }

编辑ItemCatService

 @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        //判断level到底是几级菜单
        if(itemCat.getLevel() == 3){
            itemCatMapper.deleteItemCatById(itemCat.getId());
        }

        if(itemCat.getLevel() == 2){
            //删除三级和二级
            itemCatMapper.deleteItemCat2(itemCat.getId());
        }

        if(itemCat.getLevel() == 1){
            itemCatMapper.deleteItemCat1(itemCat.getId());
        }
    }

编辑ItemCatMapper

void deleteItemCat1(Integer id);

xml映射文件

<delete id="deleteItemCat1">
        delete from item_cat
           where
                parent_id in
                (select id from item_cat where parent_id=#{id})
           or
            parent_id=#{id}
           or
            id=#{id}
    </delete>

2.Mybatis-Puls

Mp特点:Mybatis-Plus简称MP是一个MyBatis(opens new window)的增强工具,在 MyBatis
的基础上只做增强不做改变,为简化开发、提高效率而生。

2.1 MP实现原理
要求对象与表一一对应
对象中的属性与表中的字段一一对应 使用特定的注解进行标识
MP将常见的CURD的接口方法进行了封装,以后用户只需要继承接口即可.
MP根据自定的接口,动态的生成CURD的Sql,从此单表操作,程序员无需编辑Sql.

MP入门

导入jar包

		<!--spring整合mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>

编辑POJO

@Data
@Accessors(chain = true) //不需要写getset方法
@TableName("item") //对象与表一一映射
public class Item extends BasePojo{
    @TableId(type = IdType.AUTO)//主键自增
    //@TableField("id")      //如果字段名称与属性名称一致,则省略,包含驼峰规则
    private Integer id;         //商品Id号
    private String title;       //商品标题信息
    private String sellPoint;   //卖点信息
    private Integer price;      //商品价格
    private Integer num;        //商品数量
    private String images;       //商品图片
    private Integer itemCatId;  //商品分类ID号
    private Boolean status;     //状态信息    0 下架 1 上架
}

继承BaseMapper接口

package com.jt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.Item;

//必须添加泛型,因为泛型对象与表关联
public interface ItemMapper extends BaseMapper<Item> {


}

案例

package com.jt;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@SpringBootTest
public class TestMP {

    @Autowired
    private ItemMapper itemMapper;

    @Test
    public void test01(){
        //查询全部数据,不需要条件
        List<Item> list = itemMapper.selectList(null);
        System.out.println(list);
    }

    /**
     * 2.测试数据新增入库操作
     * 核心: 以对象的方式操作数据库
     */
    @Test
    public void test02(){
        Item item = new Item();
        item.setTitle("商品标题信息")
            .setSellPoint("卖点信息")
                    .setNum(100);
        itemMapper.insert(item);
    }

    /**
     * 3.查询id=1的数据
     */
    @Test
    public void test03(){
       Item item = itemMapper.selectById(1);
       System.out.println(item);
    }

    /**
     * 4.根据title/sellPoint查询数据
     *   查询titile="手机" and sellPoint="贵就好"
     * 核心:
     *      1.以对象的方式操作数据库.
     *      2.根据对象中不为null的属性,充当条件
     */
    @Test
    public void test04(){
        //对象的方式封装数据,只能实现and连接
        Item item = new Item();
        item.setTitle("手机").setSellPoint("贵就好");
        //条件构造器: 动态拼接where条件的  set赋值解析之后变为 =号
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>(item);
        List<Item> list = itemMapper.selectList(queryWrapper);
        System.out.println(list);
    }

    /**
     * 查询 价格大于500,小于200000
     * 1.条件构造器的连接符:
     *      1.等号  =   eq
     *      2.大于  >   gt
     *      3.小于  <   lt
     *      4.大于等于 >=  ge
     *      5.小于等于 <=  le
     *      6.不等于  !=  ne
     *  2.条件间关联用法  默认使用and连接,
     *                  如果使用or,则写or方法
     */
    @Test
    public void test05(){
       QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
       queryWrapper.gt("price",500)
                   //.or()
                   .lt("price",200000);
       List<Item> list = itemMapper.selectList(queryWrapper);
        System.out.println(list);
    }

    /**
     * 查询价格大于600的商品,并且要求按照 价格降序排列.
     * Sql:select * from item where price>600 order by price desc
     */
    @Test
    public void test06(){
        QueryWrapper<Item> queryWrapper = new QueryWrapper();
        queryWrapper.gt("price",600)
                    .orderByDesc("price");
        List<Item> list = itemMapper.selectList(queryWrapper);
        System.out.println(list);
    }

    /**
     * 查询: title中包含"华为"的数据
     * Sql: select * from item where title like "%华为%"
     * 知识点:
     *      1. 以xxx开头  "xxx%"  右侧 LikeRight
     *      2. 以xxx结尾  "%xxx"  左侧 likeLeft
     *      3. xxx       "%xxx%" like
     */
    @Test
    public void test07(){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title","华为");
        List<Item> list = itemMapper.selectList(queryWrapper);
        System.out.println(list);
    }

    /**
     * 查询: id为 1,2,3,4的数据
     * Sql: select * from item where id in (1,2,3,4)
     */
    @Test
    public void test08(){
        Integer[] ids = {1,2,3,4};
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids);
        List<Item> list = itemMapper.selectList(queryWrapper);
        //2.API2 数组转化为集合??
        List idList = Arrays.asList(ids);
        List<Item> list2 = itemMapper.selectBatchIds(idList);
        System.out.println(list);
        System.out.println(list2);
    }

    /**
     * 查询: 根据对象中不为null的数据,查询数据库
     * 核心知识点: 动态sql拼接
     */
    @Test
    public void test09(){
       String title = "";
       String sellPoint = "工艺好";
       QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq(StringUtils.hasLength(title),"title",title);
       queryWrapper.eq(StringUtils.hasLength(sellPoint),"sell_point",sellPoint);

       List<Item> list = itemMapper.selectList(queryWrapper);
       System.out.println(list);
    }


    /**
     * 查询: 只查询主键信息,
     * 适用场景: 适用关联查询!!!    主键可以被其它的关联
     */
    @Test
    public void test10(){

        List idList = itemMapper.selectObjs(null);
        System.out.println(idList);
    }

    /**
     * 作业: 自己完成删除/更新的用法
     */

}

 查询 价格大于500,小于200000
      1.条件构造器的连接符:
      1.等号  =   eq
      2.大于  >   gt
      3.小于  <   lt
      4.大于等于 >=  ge
      5.小于等于 <=  le
      6.不等于  !=  ne
  2.条件间关联用法  默认使用and连接,
                   如果使用or,则写or方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值