第187天学习打卡(项目 谷粒商城29 品牌分类与级联更新 )
Posted doudoutj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第187天学习打卡(项目 谷粒商城29 品牌分类与级联更新 )相关的知识,希望对你有一定的参考价值。
品牌分类关联与级联更新
重新弄回分类的原始数据
gulimall.product.config
MyBatisConfig.java
这个分页插件使用失败
package com.doudou.gulimall.product.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
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.doudou.gulimall.product.dao")
public class MyBatisConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
paginationInterceptor.setOverflow(true);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInterceptor.setLimit(1000);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
出现的错误
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.baomidou.mybatisplus.autoconfigure.IdentifierGeneratorAutoConfiguration$InetUtilsAutoConfig.identifierGenerator(IdentifierGeneratorAutoConfiguration.java:59)
The following method did not exist:
com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator.<init>(Ljava/net/InetAddress;)V
The method's class, com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator, is available from the following locations:
jar:file:/D:/Maven/apache-maven-3.6.3-bin/apache-maven-3.6.3/maven-repo/com/baomidou/mybatis-plus-core/3.3.1/mybatis-plus-core-3.3.1.jar!/com/baomidou/mybatisplus/core/incrementer/DefaultIdentifierGenerator.class
The class hierarchy was loaded from the following locations:
com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator: file:/D:/Maven/apache-maven-3.6.3-bin/apache-maven-3.6.3/maven-repo/com/baomidou/mybatis-plus-core/3.3.1/mybatis-plus-core-3.3.1.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator
Process finished with exit code 1
解决办法
gulimall.product.config
MyBatisConfig.java
使用mybatisplus新分页插件MybatisPlusInterceptor
package com.doudou.gulimall.product.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
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.doudou.gulimall.product.dao")
public class MyBatisConfig {
//引入分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(innerInterceptor);
return mybatisPlusInterceptor;
}
}
- 3.4.0 以下版本
应使用 PaginationInterceptor 类来实现分页功能
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Configuration;
@Configuration//配置类
@EnableTransactionManagement//开启事务
@MapperScan("com.doudou.gulimall.product.dao")
public class MyBatisConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
- 3.4.0及以上版本
应使用 MybatisPlusInterceptor 类来实现分页
package com.doudou.gulimall.product.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
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.doudou.gulimall.product.dao")
public class MyBatisConfig {
//引入分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(innerInterceptor);
return mybatisPlusInterceptor;
}
}
模糊查询
doudou.gulimall.product.service.impl
BrandServiceImpl.java
package com.doudou.gulimall.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.doudou.common.utils.PageUtils;
import com.doudou.common.utils.Query;
import com.doudou.gulimall.product.dao.BrandDao;
import com.doudou.gulimall.product.entity.BrandEntity;
import com.doudou.gulimall.product.service.BrandService;
import org.springframework.util.StringUtils;
@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");
QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
if (!StringUtils.isEmpty(key)){
queryWrapper.eq("brand_id",key).or().like("name",key);
}
IPage<BrandEntity> page = this.page(
new Query<BrandEntity>().getPage(params),
queryWrapper
);
return new PageUtils(page);
}
}
新增测试数据
doudou.gulimall.product.controller
CategoryBrandRelationController.java
package com.doudou.gulimall.product.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.doudou.gulimall.product.entity.CategoryBrandRelationEntity;
import com.doudou.gulimall.product.service.CategoryBrandRelationService;
import com.doudou.common.utils.PageUtils;
import com.doudou.common.utils.R;
/**
* 品牌分类关联
*
* @author doudoutj111
*
* @date 2021-06-19 19:40:52
*/
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
@Autowired
private CategoryBrandRelationService categoryBrandRelationService;
/**
* 获取当前品牌关联的所有分类列表
*/
//@RequestMapping(value = "/catelog/list",method = RequestMethod.GET)
@GetMapping("/catelog/list")
//@RequiresPermissions("product:categorybrandrelation:list")
public R cateloglist(@RequestParam("brandId")Long brandId){
List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
return R.ok().put("data", data);
}
/**
* 列表
*/
@RequestMapping("/list")
//@RequiresPermissions("product:categorybrandrelation:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = categoryBrandRelationService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
// @RequiresPermissions("product:categorybrandrelation:info")
public R info(@PathVariable("id") Long id){
CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
return R.ok().put("categoryBrandRelation", categoryBrandRelation);
}
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("product:categorybrandrelation:save")
public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.saveDetail(categoryBrandRelation);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("product:categorybrandrelation:update")
public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.updateById(categoryBrandRelation);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("product:categorybrandrelation:delete")
public R delete(@RequestBody Long[] ids){
categoryBrandRelationService.removeByIds(Arrays.asList(ids));
return R.ok();
}
}
doudou.gulimall.product.service.impl
CategoryBrandRelationServiceImpl.java
package com.doudou.gulimall.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.doudou.common.utils.PageUtils;
import com.doudou.common.utils.Query;
import com.doudou.gulimall.product.dao.CategoryBrandRelationDao;
import com.doudou.gulimall.product.entity.CategoryBrandRelationEntity;
import com.doudou.gulimall.product.service.CategoryBrandRelationService;
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<CategoryBrandRelationEntity> page = this.page(
new Query<CategoryBrandRelationEntity>().getPage(params),
new QueryWrapper<CategoryBrandRelationEntity>()
);
return new PageUtils(page);
}
@Override
public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
}
}
doudou.gulimall.product.service.impl
CategoryBrandRelationServiceImpl.java
package com.doudou.gulimall.product.service.impl;
import com.doudou.gulimall.product.dao.BrandDao;
import com.doudou.gulimall.product.dao.CategoryDao;
import com.doudou.gulimall.product.entity.BrandEntity;
import com.doudou.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;以上是关于第187天学习打卡(项目 谷粒商城29 品牌分类与级联更新 )的主要内容,如果未能解决你的问题,请参考以下文章
第179天学习打卡(项目 谷粒商城21 删除 批量删除小结 品牌管理 效果优化与快速显示开关)
第173天学习打卡(项目 谷粒商城 15 配置网关路由与路径重写 查询树形展示三级分类)
第176天学习打卡(项目 谷粒商城 18 API三级分类 删除效果细化 新增效果)
第195天学习打卡(项目 谷粒商城 37新增商品 获取分类下所有属性及分组)