Dao
逆向工程已经实现了Mapper,不需要再做修改。
Service
接收parentId参数。
调用dao查询Tree节点,返回一个EasyUITreeNode支持的数据格式的Tree节点。
pojo:
把它放到izheyi-manager-common里,代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23public class EasyUiTreeNode {
public long id;
public String text;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String state;
}
接口类:1
2
3
4
5public interface ItemCategoryService {
List<EasyUiTreeNode> getItemCategoryNode(long parentId);
}
实现类:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {
@Autowired
private TbItemCatMapper itemCategoryMapper;
@Override
public List<EasyUiTreeNode> getItemCategoryNode(long parentId) {
// Search
TbItemCatExample example = new TbItemCatExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
//返回结果列表
List<TbItemCat> list = itemCategoryMapper.selectByExample(example);
//转换成EasyUiTree
List<EasyUiTreeNode> result = new ArrayList<EasyUiTreeNode>();
for (TbItemCat tbItemCat : list) {
EasyUiTreeNode node = new EasyUiTreeNode();
node.setId(tbItemCat.getId());
node.setText(tbItemCat.getName());
node.setState(tbItemCat.getIsParent()?"closed":"open");
result.add(node);
}
return result;
}
}
Controller
接收页面传递过来的参数id。1
2
3
4
5
6
7
8
9
10
11
12
13@Controller
public class ItemCategoryController {
@Autowired
private ItemCategoryService itemCategoryService;
@RequestMapping("/item/cat/list")
@ResponseBody
public List<EasyUiTreeNode> getItemCategoryNode(@RequestParam(value="id", defaultValue="0") long parentId){
List<EasyUiTreeNode> result = itemCategoryService.getItemCategoryNode(parentId);
return result;
}
}