Fork me on GitHub

五.宜立方商城——前台工程搭建&内容管理模块

一.课程计划

1
2
3
4
5
6
1、前台系统搭建
2、商城首页展示
3、Cms系统的实现
a)内容分类管理
b)内容管理
4、前台内容动态展示

二.商城首页显示

系统架构:
image

2.1工程搭建

可以参考e3-manager-web工程搭建

image

这里注意,看我图片上显示的那个伪静态,就是说,现在大多数网站的搜索引擎它对静态页面的支持比较好,但是我们的jsp页面还是jsp页面啊,难道要改吗?这个时候我就可以使用这种方式

2.2那如何展示首页呢?

我们前面在web.xml中对springmvc的前端控制器是不是做了处理,它只拦截以.html结尾的请求对吧?那我们思考如何写呢?

一般我们展示页面是不是通过Controller来进行的,来我们看一下ControllerHandler是什么呢?

image
好,搞清楚这个之后,我们该如何写呢,我们是不是一般这样写的

一般:http://localhost:8082/

但是现在我们这样写可以吗?

当然不行,因为前端控制器只会拦截以.html结尾的请求,那我们该怎么做呢???
这个时候我们就用到了web.xml

image

image

image

三.首页显示动态分析:

内容信息要从数据库中获得

3.1动态展示分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1、内容需要进行分类
2、分类下有子分类,需要动态管理。
3、分类下有内容列表
4、单点的内容信息
a)有图片
b)有链接
c)有标题
d)有价格
e)包含大文本类型,可以作为公告

需要一个内容分类表和一个内容表。内容分类和内容表是一对多的关系。
内容分类表,需要存储树形结构的数据。
内容分类表:tb_content_category
内容表:tb_content

需要有后台来维护内容信息。Cms系统。

需要创建一个内容服务系统。可以参考e3-manager创建。
E3-content:聚合工程打包方式pom
|--e3-content-interface jar
|--e3-content-Service war

四.内容服务系统创建

4.1工程搭建

可以参考e3-manager工程搭建。

image

4.2 e3_content

pom文件:

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>e3_parent</artifactId>
<groupId>yp.e3mall</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../e3_parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>../e3_content_interface</module>
<module>../e3_content_service</module>
</modules>
<artifactId>e3_content</artifactId>

<dependencies>
<dependency>
<groupId>yp.e3mall</groupId>
<artifactId>e3_common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8083</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>

</project>

搭建的过程是,先把e3_manage的pom文件复制过来,再改改删删,比如common的依赖就不用删,Tomcat插件的端口号改一下就完了

4.3 e3-content-interface

pom文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>e3_content</artifactId>
<groupId>yp.e3mall</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../e3_content/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

<artifactId>e3_content_interface</artifactId>
<dependencies>
<dependency>
<groupId>yp.e3mall</groupId>
<artifactId>e3_manager_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

</project>

依赖了pojo

4.4 e3_content_Service

pom文件:

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>e3_content</artifactId>
<groupId>yp.e3mall</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../e3_content/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>e3_content_service</artifactId>
<packaging>war</packaging>

<name>e3_content_service Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>yp.e3mall</groupId>
<artifactId>e3_manager_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>yp.e3mall</groupId>
<artifactId>e3_content_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>

<build>
<finalName>e3_content_service</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

dao的依赖不需要改动,因为我们还要用到dao中的mapper接口,而interface需要改动一下,interface依赖我们自己的interface,也就是e3_content_interface

我们来看一下Service的配置文件需要改动什么,我们得先把e3_manager_Service的配置文件拷贝过来

image
image
image
image
image
image

五.Cms系统实现

5.1内容分类管理

image

5.1.1展示内容分类

功能分析:

image
image
请求的url:/content/category/list

请求的参数:id,当前节点的id。第一次请求是没有参数,需要给默认值“0”

Json数据:

1
2
3
[{id:1,text:节点名称,state:open(closed)},
{id:2,text:节点名称2,state:open(closed)},
{id:3,text:节点名称3,state:open(closed)}]

业务逻辑

1
2
3
4
1、取查询参数id,parentId
2、根据parentId查询tb_content_category,查询子节点列表。
3、得到List<TbContentCategory>
4、把列表转换成List<EasyUITreeNode>

Service层

接口:

1
2
3
4
5
6
7
8
9
10
public interface ContentCategoryService {

/**
* 查询内容树列表
* @param parentId
* @return
*/
List<EasyUITreeNode> getContentCatList(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
29
30
31
32
33
34
/**
* @author RickYinPeng
* @ClassName ContentCategoryServiceImpl
* @Description 内容分类管理
* @date 2018/10/10/17:39
*/
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

@Autowired
private TbContentCategoryMapper tbContentCategoryMapper;

@Override
public List<EasyUITreeNode> getContentCatList(long parentId) {
//根据parentId查询子节点列表
TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
//设置查询条件
criteria.andParentIdEqualTo(parentId);
//执行查询
List<TbContentCategory> catList= tbContentCategoryMapper.selectByExample(tbContentCategoryExample);

List<EasyUITreeNode> EasyUITreeNode_List = new ArrayList<>();
for (int i = 0;i<catList.size();i++){
EasyUITreeNode node = new EasyUITreeNode();
node.setId(catList.get(i).getId());
node.setText(catList.get(i).getName());
node.setState(catList.get(i).getIsParent()==true?"closed":"open");
EasyUITreeNode_List.add(node);
}

return EasyUITreeNode_List;
}
}

发布服务:

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
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

<!-- 配置Service扫描(可以扫描到子包) -->
<context:component-scan base-package="yp.e3mall.content.service"/>

<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="e3_content" />
<dubbo:registry protocol="zookeeper"
address="192.168.25.128:2181" />
<!-- 用dubbo协议在20881端口暴露服务 -->
<!--端口会冲突,所以改为20881,之前是20880-->
<dubbo:protocol name="dubbo" port="20881" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="yp.e3mall.content.service.ContentCategoryService" ref="contentCategoryServiceImpl"
timeout="600000"/>
</beans>

image
image

不然启动就会报错,如果不这样写的话ref=”contentCategoryServiceImpl”

Web层:

E3-manager-web

依赖e3-content-interface模块

image

Springmvc.xml中添加服务的引用:

image

Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @author RickYinPeng
* @ClassName ContentCatController
* @Description 内容分类管理Controller
* @date 2018/10/10/18:59
*/
@Controller
public class ContentCatController {

@Autowired
private ContentCategoryService contentCategoryService;

@RequestMapping("/content/category/list")
@ResponseBody
public List<EasyUITreeNode> getContentCatList(@RequestParam(name="id",defaultValue = "0") Long parentId){
List<EasyUITreeNode> contentCatList = contentCategoryService.getContentCatList(parentId);
return contentCatList;
}
}

启动测试,我们需要先将e3_content打包到maven仓库,再启动e3_content服务,再启动e3_manager服务,再启动e3_manager_web,这里e3_content和e3_manager都属于服务层

image

5.1.2新增节点

功能分析

image
image
image
image
image
image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
请求的url:/content/category/create
请求的参数:
Long parentId
String name
响应的结果:
json数据,E3Result,其中包含一个对象,对象有id属性,新生产的内容分类id
业务逻辑:
1、接收两个参数:parentId、name
2、向tb_content_category表中插入数据。
a)创建一个TbContentCategory对象
b)补全TbContentCategory对象的属性
c)向tb_content_category表中插入数据
3、判断父节点的isparent是否为true,不是true需要改为true。
4、需要主键返回。
5、返回E3Result,其中包装TbContentCategory对象

Dao层

可以使用逆向工程。

需要添加主键返回:

image

注意:修改完代码后,需要向本地仓库安装e3-manager-dao包

Service层

参数:parentId、name

返回值:返回E3Result,其中包装TbContentCategory对象
接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public interface ContentCategoryService {

/**
* 查询内容树列表
* @param parentId
* @return
*/
List<EasyUITreeNode> getContentCatList(long parentId);

/**
* 添加内容
* @param parentId
* @param name
* @return
*/
E3Result addContentCategory(long parentId,String 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @author RickYinPeng
* @ClassName ContentCategoryServiceImpl
* @Description 内容分类管理
* @date 2018/10/10/17:39
*/
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

@Autowired
private TbContentCategoryMapper tbContentCategoryMapper;

/**
* 显示分类树
* @param parentId
* @return
*/
@Override
public List<EasyUITreeNode> getContentCatList(long parentId) {
//根据parentId查询子节点列表
TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
//设置查询条件
criteria.andParentIdEqualTo(parentId);
//执行查询
List<TbContentCategory> catList= tbContentCategoryMapper.selectByExample(tbContentCategoryExample);

List<EasyUITreeNode> EasyUITreeNode_List = new ArrayList<>();
for (int i = 0;i<catList.size();i++){
EasyUITreeNode node = new EasyUITreeNode();
node.setId(catList.get(i).getId());
node.setText(catList.get(i).getName());
node.setState(catList.get(i).getIsParent()==true?"closed":"open");
EasyUITreeNode_List.add(node);
}

return EasyUITreeNode_List;
}

/**
* 添加内容分类
* @param parentId
* @param name
* @return
*/
@Override
public E3Result addContentCategory(long parentId, String name) {
//创建一个tb_content_category表对应的pojo
TbContentCategory tbContentCategory = new TbContentCategory();

//设置pojo的属性
tbContentCategory.setParentId(parentId);
tbContentCategory.setName(name);
//1:正常 2:删除
tbContentCategory.setStatus(1);
//默认排序是1
tbContentCategory.setSortOrder(1);
//新添加的节点一定是叶子节点,所以是false
tbContentCategory.setIsParent(false);
tbContentCategory.setCreated(new Date());
tbContentCategory.setUpdated(new Date());

//插入到数据库,插入完了之后,
//这个对象tbContentCategory就已经包含了id属性,因为我们设置了在mapper.xml中
tbContentCategoryMapper.insert(tbContentCategory);

//判断父节点的isParent属性。如果不是true改为true
//根据parentid查询父节点
TbContentCategory Parent = tbContentCategoryMapper.selectByPrimaryKey(parentId);
if(!Parent.getIsParent()){
Parent.setIsParent(true);
//更新到数据库中
tbContentCategoryMapper.updateByPrimaryKey(Parent);
}

//返回结果,返回e3Result,其中Dat属性是上面的pojo,id已经有了
return E3Result.ok(tbContentCategory);
}
}

表现层

1
2
3
4
5
6
请求的url:/content/category/create
请求的参数:
Long parentId
String name
响应的结果:
json数据,E3Result
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
/**
* @author RickYinPeng
* @ClassName ContentCatController
* @Description 内容分类管理Controller
* @date 2018/10/10/18:59
*/
@Controller
public class ContentCatController {

@Autowired
private ContentCategoryService contentCategoryService;

/**
* 显示内容管理分类
* @param parentId
* @return
*/
@RequestMapping("/content/category/list")
@ResponseBody
public List<EasyUITreeNode> getContentCatList(@RequestParam(name="id",defaultValue = "0") Long parentId){
List<EasyUITreeNode> contentCatList = contentCategoryService.getContentCatList(parentId);
return contentCatList;
}

@RequestMapping(value="/content/category/create",method = RequestMethod.POST)
@ResponseBody
public E3Result creatContentCategory(long parentId,String name){
E3Result e3Result = contentCategoryService.addContentCategory(parentId, name);
return e3Result;
}
}

注意:我复制的时候是和类全部复制的,防止大家看不懂。

5.2内容管理

5.2.1功能点分析

1
2
3
4
1、内容列表查询(作业)
2、新增内容
3、编辑内容(作业)
4、删除内容(作业)

5.2.2内容列表查询(已做)

1
2
3
4
5
6
7
8
9
请求的url:/content/query/list
参数:categoryId 分类id
响应的数据:json数据
{total:查询结果总数量,rows[{id:1,title:aaa,subtitle:bb,...}]}
EasyUIDataGridResult
描述商品数据List<TbContent>
查询的表:tb_content
业务逻辑:
根据内容分类id查询内容列表。要进行分页处理。

5.2.3新增内容

功能分析

image

image

image

image

image

image

1
2
3
4
5
6
7
提交表单请求的url:/content/save
参数:表单的数据。使用pojo接收TbContent
返回值:E3Result(json数据)
业务逻辑:
1、把TbContent对象属性补全。
2、向tb_content表中插入数据。
3、返回E3Result

Dao

逆向工程

Service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface ContentService {

/**
* 内容分页列表
* @param id
* @param page
* @param rows
* @return
*/
EasyUIDataGridResult getItemList(long id, int page, int rows);

/**
* 添加内容
* @param tbContent
* @return
*/
E3Result addContent(TbContent tbContent);


}
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
package yp.e3mall.content.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yp.e3mall.common.pojo.EasyUIDataGridResult;
import yp.e3mall.common.utils.E3Result;
import yp.e3mall.content.service.ContentService;
import yp.e3mall.mapper.TbContentMapper;
import yp.e3mall.pojo.TbContent;
import yp.e3mall.pojo.TbContentExample;

import java.util.Date;
import java.util.List;

/**
* @author RickYinPeng
* @ClassName ContentServiceImpl
* @Description
* @date 2018/10/13/21:09
*/
@Service
public class ContentServiceImpl implements ContentService{

@Autowired
private TbContentMapper tbContentMapper;

@Override
public EasyUIDataGridResult getItemList(long id, int page, int rows) {
//设置分页信息
//page:页码
//rows:每页显示数量
PageHelper.startPage(page,rows);
//执行查询
TbContentExample tbContentExample = new TbContentExample();
TbContentExample.Criteria criteria = tbContentExample.createCriteria();
criteria.andCategoryIdEqualTo(id);
List<TbContent> itemList = tbContentMapper.selectByExample(tbContentExample);
//创建一个返回值对象
EasyUIDataGridResult result = new EasyUIDataGridResult();
result.setRows(itemList);
//取分页结果
PageInfo pageInfo = new PageInfo(itemList);
result.setTotal(pageInfo.getTotal());
return result;
}

@Override
public E3Result addContent(TbContent tbContent) {
//补全内容,因为数据库中有update和creatDate
tbContent.setCreated(new Date());
tbContent.setUpdated(new Date());
//将内容插入内容表
tbContentMapper.insert(tbContent);

return E3Result.ok();
}
}
发布服务

image

引用服务

image

Controller
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
/**
* @author RickYinPeng
* @ClassName ContentController
* @Description
* @date 2018/10/13/21:14
*/
@Controller
public class ContentController {
@Autowired
private ContentService contentService;

@RequestMapping("/content/query/list")
@ResponseBody
public EasyUIDataGridResult getItemList(@RequestParam("categoryId") long id, Integer page, Integer rows){
EasyUIDataGridResult itemList = contentService.getItemList(id, page, rows);
return itemList;
}

@RequestMapping(value = "/content/save",method = RequestMethod.POST)
@ResponseBody
public E3Result addContent(TbContent tbContent){
E3Result e3Result = contentService.addContent(tbContent);
return e3Result;
}
}

image

六.商城首页轮播图动态展示

分析:

image

image

image

image

image

image

image

image

image

Service:

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
package yp.e3mall.content.service;

import yp.e3mall.common.pojo.EasyUIDataGridResult;
import yp.e3mall.common.utils.E3Result;
import yp.e3mall.pojo.TbContent;

import java.util.List;

public interface ContentService {

/**
* 内容分页列表
* @param id
* @param page
* @param rows
* @return
*/
EasyUIDataGridResult getItemList(long id, int page, int rows);

/**
* 添加内容
* @param tbContent
* @return
*/
E3Result addContent(TbContent tbContent);

/**
* 根据内容分类id查询内容,就展示轮播图那块
* @param cid
* @return
*/
List<TbContent> getContentListByCid(long cid);

}

只看最后一个方法

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
/**
* @author RickYinPeng
* @ClassName ContentServiceImpl
* @Description
* @date 2018/10/13/21:09
*/
@Service
public class ContentServiceImpl implements ContentService{

@Autowired
private TbContentMapper tbContentMapper;

@Override
public EasyUIDataGridResult getItemList(long id, int page, int rows) {
//设置分页信息
//page:页码
//rows:每页显示数量
PageHelper.startPage(page,rows);
//执行查询
TbContentExample tbContentExample = new TbContentExample();
TbContentExample.Criteria criteria = tbContentExample.createCriteria();
criteria.andCategoryIdEqualTo(id);
List<TbContent> itemList = tbContentMapper.selectByExample(tbContentExample);
//创建一个返回值对象
EasyUIDataGridResult result = new EasyUIDataGridResult();
result.setRows(itemList);
//取分页结果
PageInfo pageInfo = new PageInfo(itemList);
result.setTotal(pageInfo.getTotal());
return result;
}

@Override
public E3Result addContent(TbContent tbContent) {
//补全内容,因为数据库中有update和creatDate
tbContent.setCreated(new Date());
tbContent.setUpdated(new Date());
//将内容插入内容表
tbContentMapper.insert(tbContent);

return E3Result.ok();
}

@Override
public List<TbContent> getContentListByCid(long cid) {
TbContentExample tbContentExample = new TbContentExample();
TbContentExample.Criteria criteria = tbContentExample.createCriteria();
criteria.andCategoryIdEqualTo(cid);
List<TbContent> tbContents = tbContentMapper.selectByExampleWithBLOBs(tbContentExample);
return tbContents;
}
}

只看最后一个方法

依赖添加

这次我们的web层应该写在e3_portal_web中了,所以我们得在这一层依赖interface接口,再引用dubbo服务

添加e3_content_interfac依赖:

image

image

Controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @author RickYinPeng
* @ClassName IndexController
* @Description 首頁展示
* @date 2018/10/7/13:34
*/
@Controller
public class IndexController {

@Autowired
private ContentService contentService;

@RequestMapping("/index")
public String showIndex(){
//查询内容列表
contentService.getContentListByCid(89);
return "index";
}
}

注意:写在了我们之前写的那个方法当中,我们想一想这个89我们不能写死吧!所以我们可以写在配置文件中

image

image

image

你想啊,我们查询出来是不是要传送到页面去
image

image

1
2
3
4
5
6
7
8
9
@RequestMapping("/index")
public String showIndex(Model model){
//查询内容列表1
List<TbContent> ad1List = contentService.getContentListByCid(CONTENT_LUNBO_ID);
//把结果传递给页面
model.addAttribute("ad1List",ad1List);

return "index";
}

image

image