0%

复制代码

/** * */
package com.xdw.dao; import java.util.List; import com.xdw.model.Category; /** * @author xiadewang
*2018年4月16日 */
public interface CategoryDao {
List getCategoryList();
}

复制代码

复制代码

<mapper namespace=“com.xdw.dao.CategoryDao”>





<resultMap type=“Category” id=“categoryTree”>
<result column=“cid” property=“cid” javaType=“java.lang.String” />
<result column=“cname” property=“cname” javaType=“java.lang.String” />
<result column=“pid” property=“pid” javaType=“java.lang.String” />
<collection column=“cid” property=“childrenList” ofType=“Category” javaType=“java.util.ArrayList” select=“selectCategoryChildrenByCid”/>
</resultMap>

<!-- 先查询菜单根级目录 \-->
<!-- 这里的返回结果必须为resultMap,并且值为上面构建的resultMap的id的值 \-->
<select id\="getCategoryList" resultMap\="categoryTree"\> select \* from category where pid = 'root' </select\>

<!-- 再利用上次查询结果colliection中column的值cid做递归查询,查出所有子菜单 \-->
<!-- 这里的返回结果必须为resultMap,并且值为上面构建的resultMap的id的值 \-->
<select id\="selectCategoryChildrenByCid" resultMap\="categoryTree" parameterType\="String"\> select \* from category where pid = #{cid} </select\>

</mapper>

复制代码

复制代码

/** * */
package com.xdw.service; import java.util.List; import com.xdw.model.Category; /** * @author xiadewang
*2018年4月16日 */
public interface CategoryService {
List getCategoryList();
} /** * */
package com.xdw.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.xdw.dao.CategoryDao; import com.xdw.model.Category; import com.xdw.service.CategoryService; /** * @author xiadewang
*2018年4月16日 */ @Service public class CategoryServiceImpl implements CategoryService {
@Autowired private CategoryDao categoryDao; /* (non-Javadoc)
* @see com.xdw.service.CategoryService#getCategoryList() */ @Override public List getCategoryList() { // TODO Auto-generated method stub
return categoryDao.getCategoryList();
}

}

复制代码

@RequestMapping(“/getCategoryTree”)
@ResponseBody public List getCategoryTree() { return categoryService.getCategoryList();
}

此时基于SSM的操作已经完毕,核心就是在写mybatis的xml,可以在浏览器中查看运行结果,返回的是json数据。运行结果如下