81,122
社区成员




package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* <p>
* <pre>
* <b>类描述:</b>
* 根据传过来的id查出所有商品名字
* <b>作者:</b>
* giberson1
* <b>创建时间:</b>
* 2015年8月6日 下午7:12:48
* </pre>
* </p>
*/
public class FindThirdCategory {
public static void main(String[] args) {
}
/**
* <p>
* <pre>
* <b>方法描述:</b>
* 根据categoryIds找到所有三级目录
* <b>作者:</b>
* giberson1
* <b>创建时间:</b>
* 2015年8月6日 下午7:24:50
* </pre>
* </p>
* @param
* @return List<String>
*/
public List<String> getAllCategoryIds(String... categoryIds){
HashMap<String,String> flagMap = new HashMap<String,String>(); //不用来存储,而是当成“去重复数据的过滤器”
List<String> list = new ArrayList<String>(); //存放所有三级目录
for (int i = 0; i < categoryIds.length; i++) {
String categoryId = categoryIds[i];
List<String> thirdList = new ArrayList<String>();
getThindCategoryIds(thirdList,categoryId);
//对thirdList进行过滤后,添加到list。
for (String s : thirdList) {
if(!flagMap.containsKey(s)){
flagMap.put(s, s);
list.add(s);
}
}
}
return list;
}
/**
* <p>
* <pre>
* <b>方法描述:</b>
* 根据categoryId找到对应三级目录--------通过递归来实现
* <b>作者:</b>
* giberson1
* <b>创建时间:</b>
* 2015年8月6日 下午7:25:07
* </pre>
* </p>
* @param
* @return List<String>
*/
public void getThindCategoryIds(List<String> list,String categoryId){
if(isThirdCatagory(categoryId)){ //递归结束
list.add(categoryId);
}else{ //开始递归
List<String> subList = getSubCategoryIds(list,categoryId);
for (String subCategoryId : subList) {
getThindCategoryIds(list,subCategoryId);
}
}
}
/**
* <p>
* <pre>
* <b>方法描述:</b>
* 查询非叶子节点,的所有子节点
* <b>作者:</b>
* giberson1
* <b>创建时间:</b>
* 2015年8月6日 下午7:43:41
* </pre>
* </p>
* @param
* @return List<String>
*/
private List<String> getSubCategoryIds(List<String> list,String categoryId) {
List<String> list = new ArrayList<String>(); //存放categoryId对应的子目录
//TODO :这里不难,这里楼主自己去判断
// 就是找categoryId对应的子目录
return list;
}
/**
* <p>
* <pre>
* <b>方法描述:</b>
* 判断是否第三级目录
* <b>作者:</b>
* giberson1
* <b>创建时间:</b>
* 2015年8月6日 下午7:35:24
* </pre>
* </p>
* @param
* @return boolean
*/
private boolean isThirdCatagory(String categoryId) {
//TODO :这里不难,楼主自己去判断
// 大概思路就是,你根据categoryId查询category这表,
// parentid中没有categoryId的记录,就说明这个这个节点没有子节点,
//说明他是叶子节点,即第三级目录。如果是return true,否则返回false
// ... ... //
return false;
}
}
2》根据1》中三级目录管理product。
这部不难了,楼主自己解决哈。
select id, category_id, name from product where category_id in (
select id from (select id from category where id in (category_ids)) a where parentId = 2 //第3级目录的
union
selecct id from category where parentId in (select id from (select id from category where id in (category_ids)) a where parentId = 1) //2级目录的
union
select id from category where parentId in (selecct id from category where parentId in (select id from (select id from category where id in (category_ids)) a where parentId = 0)) // 1级目录
)
select * from product category_id in
(select id from category where id in (categoryids) )