乐优商城的第七天,主要是分类的管理
ajax的跨域问题
nginx
商品分类的删除:
1.删除自己以及后代节点(递归删除)
2.判断有没有兄弟节点,没有兄弟节点,父节点的isparent设置为false(当然还得判断自己是不是根节点)
删除的主方法,在主方法里调用递归
public int deleteCateGory(Long id) { Category category = categoryMapper.selectByPrimaryKey(id); //递归删除儿子以及后辈 this.deleteChild(category); //删除完成之后,需要判断父亲还有没有子节点,如果没有,父亲的isparent置为0 //找出所有的兄弟元素 Category c2 = new Category(); c2.setParentId(category.getParentId()); List<Category> list = categoryMapper.select(c2); //当没有兄弟节点的时候 if (list == null || list.size()<1) { //判断自己是不是根节点 if (category.getParentId() != 0) { //将父节点的isparent置为0 Category parent = new Category(); parent.setId(category.getParentId()); parent.setIsParent(false); categoryMapper.updateByPrimaryKeySelective(parent); } } return 1; }
递归的方法
/** * 递归删除子节点 * * @param category */ public void deleteChild(Category category) { //存在子节点,删除自己以及后辈 if (category.getIsParent()) { List<Category> list = this.queryCategoryByPid(category.getId()); for (Category c : list) { //删除这个节点,递归删除之后的节点 //删除自己 this.categoryMapper.deleteByPrimaryKey(category); //删除后辈 this.deleteChild(c); } } else { //如果不是父亲,直接删除 this.categoryMapper.deleteByPrimaryKey(category); } }
递归删除的时候,逐一list要判断size<1,不能判断null
CORS解决跨域问题
ajax请求为了安全,是不允许跨域访问的,而我们可以在服务端通过CORS的方式解决
我们在zuul中通过一个配置类的方式完美地解决了这个问题
@Configuration public class GlobalCorsConfig { @Bean public CorsFilter corsFilter() { //1.添加CORS配置信息 CorsConfiguration config = new CorsConfiguration(); //1) 允许的域,不要写*,否则cookie就无法使用了 config.addAllowedOrigin("http://manage.leyou.com"); //2) 是否发送Cookie信息 config.setAllowCredentials(true); //3) 允许的请求方式 config.addAllowedMethod("OPTIONS"); config.addAllowedMethod("HEAD"); config.addAllowedMethod("GET"); config.addAllowedMethod("PUT"); config.addAllowedMethod("POST"); config.addAllowedMethod("DELETE"); config.addAllowedMethod("PATCH"); // 4)允许的头信息 config.addAllowedHeader("*"); //2.添加映射路径,我们拦截一切请求 UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource(); configSource.registerCorsConfiguration("/**", config); //3.返回新的CorsFilter. return new CorsFilter(configSource); } }
页面加载的逻辑
create()调用连接,监控数据变化,再调用其他连接