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

导读:本篇文章讲解 【大型电商项目开发】品牌管理之品牌分类关联&级联更新-23,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一:分页功能

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>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/84126.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!