访问不了Youtube?
1.点击搭建自己的ss并开启bbr快速上网教程轻松访问1080p高清Youtube视频。
2.点击b站视频教程地址观看。

我们已经创建好了 rest 系统,那么接下来这一小节我们来实现商城首页的分类数据展示。

portal 系统中,我们的首页目前是静态的数据,接下来我们就从 rest 系统中取 json 数据,接着将数据展示到首页分类中。

主要思路

  1. 在 portal 系统中获取 rest 服务中的 json 数据;
  2. 使用 freemarker 展示数据

商城分类json数据格式

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
29
30
31
32
{
"categories":[
{
"cat_id":"1",
"name":"父级分类名称",
"sub_cetegories":[
{
"cat_id":"2",
"name":"java se"
},
{
"cat_id":"3",
"name":"java ee"
}
]
},
{
"cat_id":"2",
"name":"分类名称",
"sub_cetegories":[
{
"cat_id":"2",
"name":"java se"
},
{
"cat_id":"3",
"name":"java ee"
}
]
}
]
}

通过json数据转化为实体类

可以使用 idea 的 GsonFormart 插件生成相应的实体类

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class Res_Categories {


private List<CategoriesBean> categories;

public List<CategoriesBean> getCategories() {
return categories;
}

public void setCategories(List<CategoriesBean> categories) {
this.categories = categories;
}

public static class CategoriesBean {
/**
* cat_id : 1
* name : 父级分类名称
* sub_cetegories : [{"cat_id":"2","name":"java se"},{"cat_id":"3","name":"java ee"}]
*/

private String cat_id;
private String name;
private List<SubCetegoriesBean> sub_cetegories;

public String getCat_id() {
return cat_id;
}

public void setCat_id(String cat_id) {
this.cat_id = cat_id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<SubCetegoriesBean> getSub_cetegories() {
return sub_cetegories;
}

public void setSub_cetegories(List<SubCetegoriesBean> sub_cetegories) {
this.sub_cetegories = sub_cetegories;
}

public static class SubCetegoriesBean {
/**
* cat_id : 2
* name : java se
*/

private String cat_id;
private String name;

public String getCat_id() {
return cat_id;
}

public void setCat_id(String cat_id) {
this.cat_id = cat_id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
}

具体实现

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

@Autowired
ProductCategoryMapper productCategoryMapper;

@Override
public Res_Categories getCategories() {

Res_Categories res_categories = new Res_Categories();

ProductCategoryExample productCategoryExample = new ProductCategoryExample();
ProductCategoryExample.Criteria criteria = productCategoryExample.createCriteria();
criteria.andParentIdEqualTo((short) 0);
List<ProductCategory> productCategories = productCategoryMapper.selectByExample(productCategoryExample);

List<Res_Categories.CategoriesBean> categories = new ArrayList<>();

for (int i = 0; i < productCategories.size(); i++) {
Res_Categories.CategoriesBean categoriesBean = new Res_Categories.CategoriesBean();
categoriesBean.setCat_id(productCategories.get(i).getId()+"");
categoriesBean.setName(productCategories.get(i).getName());

//获取子分类
List<Res_Categories.CategoriesBean.SubCetegoriesBean> subCategories = getSubCategories(productCategories
.get(i)
.getId());

categoriesBean.setSub_cetegories(subCategories);
categories.add(categoriesBean);
}

res_categories.setCategories(categories);

return res_categories;
}

private List<Res_Categories.CategoriesBean.SubCetegoriesBean> getSubCategories(Short id) {

List<Res_Categories.CategoriesBean.SubCetegoriesBean> list = new ArrayList<>();

ProductCategoryExample productCategoryExample = new ProductCategoryExample();
ProductCategoryExample.Criteria criteria = productCategoryExample.createCriteria();
criteria.andParentIdEqualTo(id);
List<ProductCategory> productCategories = productCategoryMapper.selectByExample(productCategoryExample);

for (int i = 0; i <productCategories.size() ; i++) {
ProductCategory productCategory = productCategories.get(i);
Res_Categories.CategoriesBean.SubCetegoriesBean subCetegoriesBean = new Res_Categories.CategoriesBean
.SubCetegoriesBean();
subCetegoriesBean.setCat_id(productCategory.getId()+"");
subCetegoriesBean.setName(productCategory.getName());
list.add(subCetegoriesBean);
}

return list;
}
}