默认mybatis自动生成的@TableId的主键类型为None
@documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) public @interface TableId { String value() default ""; IdType type() default IdType.NONE; }
将生成的entity改为主键自增类型
@TableId(type = IdType.AUTO )
全部一个一个改太麻烦 , 全部更改为主键自增类型
spring: #配置主键自增 global-config: db-config: id-type: auto
server: port: spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.64.140:3306/gulimall_pms username: root password: root mybatis-plus: mapper-locations: classpath:/mapper*.xml #配置主键自增 global-config: db-config: id-type: auto45.商品三级父子分类递归查询
@Override public ListlistWithTree() { //1.查出所有分类 List entities = categoryDao.selectList(null); //2.组装成父子的树形结构 //2.1)找到所有的一级分类 List level1Menus = entities.stream(). filter(menu -> menu.getParentCid() == 0). map(menu -> { menu.setChildren(getChildrens(menu,entities));p return menu; }).sorted((menu1,menu2)->(menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort())). collect(Collectors.toList()); return level1Menus; }
private List46.网关路由和路径重写 46.1 将renren-fast注册到注册中心中 46.2 修改后台管理基地址getChildrens(CategoryEntity root,List all){ List collect = all.stream(). filter(menu -> menu.getParentCid() == root.getCatId()). map(menu -> { menu.setChildren(getChildrens(menu, all).size()==0?null:getChildrens(menu, all)); return menu; }). sorted((menu1,menu2)->(menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort())). collect(Collectors.toList()); return collect; }
// api接口请求地址 window.SITE_CONFIG["baseUrl"] = "http://localhost:88/api";46.3 修改路由-路径重写
我TM 配置文件filters 写成 filter了 , 也不报错 ,找了半天
- id: renrenfast-route #renrenfast 路由 uri: lb://renrenfast-service predicates: - Path=/api @Slf4j @RestControllerAdvice(basePackages = "com.nanoswarm.gulimall.product.controller") public class GulimallExceptionControllerAdvice { @ExceptionHandler(value = Exception.class) public R handleVaildException(Exception e){ log.error("数据校验出现问题{},异常类型:{}",e.getMessage(),e.getClass()); return R.error(); } }67.2 业务状态码
public enum BizCodeEnume { UNKNOW_EXCEPTION(10000,"系统未知异常"), VAILD_EXCEPTION(10001,"参数格式校验失败"); private Integer code; private String message; BizCodeEnume(Integer code, String message) { this.code = code; this.message = message; } public Integer getCode() { return code; } public String getMessage() { return message; } }71.父子组件传递数据 74.分组修改-级联选择器回显
这里遇到一个坑,包装类型的值相等,若用==判断则一定为fasle,指向了不同的地址
所以
Listcategories = all. stream(). filter(categoryEntity -> categoryEntity.getCatId() == catelogId). collect(Collectors.toList());
这样 filter 中 永远是错的
正确做法 , 判断 包装类型 Long的值,是否相等
List74.1 级联选择器-可搜索的categories = all. stream(). filter(categoryEntity -> categoryEntity.getCatId().longValue() == catelogId.longValue()). collect(Collectors.toList());
75.品牌分类关联与级联更新 75.1 mybatis-plus 分页插件
@Configuration @EnableTransactionManagement @MapperScan("com.nanoswarm.gulimall.product.dao") public class MybatisConfig { @Bean public MybatisPlusInterceptor paginationInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }76.规格参数新增与Vo 76.1 BeanUtils.copyProperties(origin,target)
@Override @Transactional public void saveAttr(AttrVo attrVo) { AttrEntity attrEntity = new AttrEntity(); BeanUtils.copyProperties(attrVo,attrEntity); //保存基本数据 attrDao.insert(attrEntity); //保存关联关系 AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity(); relationEntity.setAttrGroupId(attrVo.getAttrGroupId()); relationEntity.setAttrId(attrEntity.getAttrId()); attrAttrgroupRelationDao.insert(relationEntity); }77.Join 分页
@Override public PageUtils querybaseAttrPage(Map79.常量枚举params, Long catelogId) { QueryWrapper queryWrapper = new QueryWrapper<>(); if(catelogId != 0){ queryWrapper.eq("catelog_id", catelogId); } //select * from `pms_attr` where catelogId = #{catelogId} and //(attr_id = #{key} or attr_name like %attr_name%) String key = (String)params.get("key"); if(!StringUtils.isEmpty(key)){ queryWrapper.and((wrapper)->{ wrapper.eq("attr_id",key ).or().like("attr_name", key); }); } //分页 IPage page = this.page( //Time 1 => dao new Query().getPage(params), queryWrapper ); PageUtils pageUtils = new PageUtils(page); List records = page.getRecords(); List respVos = records.stream().map((attrEntity -> { AttrRespVo attrRespVo = new AttrRespVo(); //此时attrRespVo attr_group_id 为null BeanUtils.copyProperties(attrEntity, attrRespVo); //1.设置分类和分组的名字 AttrRespVo (catelogName groupName) AttrAttrgroupRelationEntity attrId = attrAttrgroupRelationDao.selectOne(new QueryWrapper() .eq("attr_id", attrEntity.getAttrId())); if (attrId != null) { AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId()); attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName()); } CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId()); if (categoryEntity != null) { attrRespVo.setCatelogName(categoryEntity.getName()); } return attrRespVo; })).collect(Collectors.toList()); pageUtils.setList(respVos); return pageUtils; }
public class ProductConstant { public enum AttrEnum{ ATTR_TYPE_base(1,"基本属性"),ATTR_TYPE_SALE(0,"销售属性"); private int code; private String msg; AttrEnum(int code, String msg) { this.code = code; this.msg = msg; } public int getCode() { return code; } public String getMsg() { return msg; } } }84.关于service的复用性和controller的返回值
controller : 处理请求,接受和校验数据
service 接受 controller处理的数据 , 进行业务处理
controller 接受 service 处理完成的数据 , 封装页面指定的vo
@GetMapping("/brands/list") public R relationBrandList(@RequestParam(value = "catId",required = true) Long catId){ List88.分隔字符串集合vos = categoryBrandRelationService.getBrandByCatId(catId); List relations = vos.stream().map(relation -> { BrandVo brandVo = new BrandVo(); brandVo.setBrandId(relation.getBrandId()); brandVo.getBrandName(); return brandVo; }).collect(Collectors.toList()); return R.ok().put("data",relations); }
String.join(CharSequence delimiter, CharSequence... elements)
public class EsPracticeApplication { public static void main(String[] args) { List91.数据库设置当前会话读未提交strList = new ArrayList<>(); strList.add("a"); strList.add("b"); String join = String.join(",", strList); System.out.println(join); //====> a,b } }
在debug模式下 , 事务执行一半的时候 , 数据库无法读取 , 当前会话设置成读未提交就可以读到事务执行一半时的数据了
set session transaction isolation level read uncommitted91.1 设置主键类型@TableId
@documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) public @interface TableId { String value() default ""; IdType type() default IdType.NONE; //默认为NONE }
手动设置主键类型
@Data @TableName("pms_spu_info_desc") public class SpuInfoDescEntity implements Serializable { private static final long serialVersionUID = 1L; @TableId(type = IdType.INPUT) private Long spuId; private String decript; }93.格式化时间
spring: jackson: date-format: yyyy-MM-dd HH:mm:ss99.Mybatis-plus分页插件
@Configuration public class WareMyBatisConfig { @Bean public MybatisPlusInterceptor paginationInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)