【大型电商项目开发】品牌管理之品牌分类关联&级联更新-23

一:分页功能

1.新建config包,添加MybatisConfig类

package com.sysg.gulimail.product.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
 * @Configuration 标注其是一个配置类
 * @EnableTransactionManagement 开启事务
 * @MapperScan("com.sysg.gulimail.product.dao") 扫描那些mapper文件
 * @author 77916
 */
@Configuration
@EnableTransactionManagement
@MapperScan("com.sysg.gulimail.product.dao")
public class MybatisConfig {
    //引入分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //设置请求页面大于最大页的操作,true跳回首页,false继续请求,默认是false
        paginationInterceptor.setOverflow(true);
        //设置最大单页限制数量,默认500条,-1不受限制
        paginationInterceptor.setLimit(1000);
        return paginationInterceptor;
    }
}

  • @Configuration 标注其是一个配置类
  • @EnableTransactionManagement 开启事务
  • @MapperScan(“com.sysg.gulimail.product.dao”) 扫描那些mapper文件

2.查看页面分页信息

在这里插入图片描述

二:模糊查询

编辑BrandServiceImpl

package com.sysg.gulimail.product.service.impl;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sysg.common.utils.PageUtils;
import com.sysg.common.utils.Query;
import com.sysg.gulimail.product.dao.BrandDao;
import com.sysg.gulimail.product.entity.BrandEntity;
import com.sysg.gulimail.product.service.BrandService;
import org.springframework.util.StringUtils;
/**
 * @author 77916
 */
@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //1.获取key
        String key = (String) params.get("key");
        //2.构造参数
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }
}
  • String key = (String) params.get(“key”);通过param获取参数key,然后将key转化为string类型。
  • QueryWrapper wrapper = new QueryWrapper<>();新建构造器
  • wrapper.eq(“brand_id”,key).or().like(“name”,key);为构造器绑定参数

三.关联分类功能实现

1.列表回显

1)编辑CategoryBrandRelationController

   /**
     * 获取当前品牌关联的所有分类列表
     */
    @RequestMapping(value = "/catelog/list",method = RequestMethod.GET)
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R catelogList(@RequestParam("brandId") Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", data);
    }

2)查看页面
在这里插入图片描述

二.保存功能

1)编辑CategoryBrandRelationController的保存方法

   /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

2)编辑CategoryBrandRelationServiceImpl的saveDetail方法

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //1.查询详细名字
        String brandName = brandDao.selectById(brandId).getName();
        String categoryName = categoryDao.selectById(catelogId).getName();
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);
        this.save(categoryBrandRelation);
    }

四:冗余字段的数据一致性

我们在保存的过程中,要将冗余的字段也同步更新,保持数据的一致性。

1.编辑BrandController,修改update方法

   /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateDetail(brand);
        return R.ok();
    }

2.编辑BrandServiceImpl,修改updateDetail方法

    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        //更新冗余存储的字段,保证冗余字段的数据一致性
        this.updateById(brand);
        //判断是否更新品牌名
        if(!StringUtils.isEmpty(brand.getName())){
            //同步更新其他关联表的数据
            categoryBrandRelationService.updateBarnd(brand.getBrandId(),brand.getName());
            //todo 更新其他关联

        }
    }

3.编辑CategoryController,修改update方法

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

4.编辑CategoryServiceImpl,修改updateCascade方法

    /**
     * 级联更新所有关联的数据
     * @param category
     */
    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        //更新自己
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());

    }

5.在CategoryBrandRelationServiceImpl实现updateCategory方法

@Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
    }

6.编辑CategoryBrandRelationDao.xml

    <update id="updateCategory">
        update pms_category_brand_relation set catelog_name = #{name} where catelog_id = #{catId}
    </update>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随意石光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值