例子一
1. 查询 100001下的子节点
2. mapper
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from category where id = #{id,jdbcType=INTEGER} </select>
<select id="selectCategoryChildrenByParentId" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from category where parent_id= #{parent_id} </select>
//Category实体类equals和hashCode方法进行重写 只需比较id即可 //递归算法,算出子节点 public Set<Category> findChildCatgory(Set<Category> categorySet,Integer categoryId){ Category category = categoryMapper.selectByPrimaryKey(categoryId); if (category != null){ categorySet.add(category); } //查找子节点 eg: 100001 ------ 100006 100007 100008 100009 100010 List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId); for (Category categoryitem : categoryList){ findChildCatgory(categorySet,categoryitem.getId()); } //递归结束条件 return categorySet; }
@RunWith(SpringRunner.class) @SpringBootTest public class ICategoryServiceImplTest { @Autowired private ICategoryService iCartService; @Test public void findChildCatgory() { Set<Category> category = iCartService.findChildCatgory(Sets.newHashSet(),100001); for (Category str : category ) { System.out.println(str.getId()); } } }
打印结果
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where id = ?
==> Parameters: 100001(Integer)
<== Columns: id, parent_id, name, status, sort_order, create_time, update_time
<== Row: 100001, 0, 家用电器, 1, null, 2017-03-25 16:46:00, 2017-03-25 16:46:00
<== Total: 1
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where parent_id= ?
==> Parameters: 100001(Integer)
<== Columns: id, parent_id, name, status, sort_order, create_time, update_time
<== Row: 100006, 100001, 冰箱, 1, null, 2017-03-25 16:52:15, 2017-03-25 16:52:15
<== Row: 100007, 100001, 电视, 1, null, 2017-03-25 16:52:26, 2017-03-25 16:52:26
<== Row: 100008, 100001, 洗衣机, 1, null, 2017-03-25 16:52:39, 2017-03-25 16:52:39
<== Row: 100009, 100001, 空调, 1, null, 2017-03-25 16:52:45, 2017-03-25 16:52:45
<== Row: 100010, 100001, 电热水器, 1, null, 2017-03-25 16:52:54, 2017-03-25 16:52:54
<== Total: 5
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where id = ?
==> Parameters: 100006(Integer)
<== Columns: id, parent_id, name, status, sort_order, create_time, update_time
<== Row: 100006, 100001, 冰箱, 1, null, 2017-03-25 16:52:15, 2017-03-25 16:52:15
<== Total: 1
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where parent_id= ?
==> Parameters: 100006(Integer)
<== Total: 0
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where id = ?
==> Parameters: 100007(Integer)
<== Columns: id, parent_id, name, status, sort_order, create_time, update_time
<== Row: 100007, 100001, 电视, 1, null, 2017-03-25 16:52:26, 2017-03-25 16:52:26
<== Total: 1
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where parent_id= ?
==> Parameters: 100007(Integer)
<== Total: 0
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where id = ?
==> Parameters: 100008(Integer)
<== Columns: id, parent_id, name, status, sort_order, create_time, update_time
<== Row: 100008, 100001, 洗衣机, 1, null, 2017-03-25 16:52:39, 2017-03-25 16:52:39
<== Total: 1
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where parent_id= ?
==> Parameters: 100008(Integer)
<== Total: 0
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where id = ?
==> Parameters: 100009(Integer)
<== Columns: id, parent_id, name, status, sort_order, create_time, update_time
<== Row: 100009, 100001, 空调, 1, null, 2017-03-25 16:52:45, 2017-03-25 16:52:45
<== Total: 1
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where parent_id= ?
==> Parameters: 100009(Integer)
<== Total: 0
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where id = ?
==> Parameters: 100010(Integer)
<== Columns: id, parent_id, name, status, sort_order, create_time, update_time
<== Row: 100010, 100001, 电热水器, 1, null, 2017-03-25 16:52:54, 2017-03-25 16:52:54
<== Total: 1
==> Preparing: select id, parent_id, name, status, sort_order, create_time, update_time from mmall_category where parent_id= ?
==> Parameters: 100010(Integer)
<== Total: 0
100001
100006
100008
100007
100010
100009