e3mall项目:内容分类管理
一、web层(ContentCategoryController),位置在e3-manager-web中
package cn.e3mall.controller; import cn.e3mall.common.entity.E3Result; import cn.e3mall.common.entity.EasyUITreeNodeResult; import cn.e3mall.content.service.ContentCategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * 内容分类控制层 * Author: xushuai * Date: 2018/5/17 * Time: 23:19 * Description: */ @Controller public class ContentCategoryController { @Autowired private ContentCategoryService contentCategoryService; /* * 加载内容分类列表 */ @RequestMapping("/content/category/list") @ResponseBody public List<EasyUITreeNodeResult> list(@RequestParam(defaultValue = "0") Long id){ return contentCategoryService.list(id); } /* * 新建内容分类 */ @RequestMapping("/content/category/create") @ResponseBody public E3Result createNode(Long parentId ,String name){ return contentCategoryService.add(parentId,name); } /* * 更新内容分类 */ @RequestMapping("/content/category/update") public void updateNode(Long id ,String name){ contentCategoryService.update(id,name); } @RequestMapping("/content/category/delete") @ResponseBody public String deleteNode(Long id){ contentCategoryService.delete(id); //返回一个成功标识 return "OK"; } }
二、新建一个聚合工程(e3-content),在e3-content中再新建两个子工程,分别为:e3-content-interface、e3-content-service,其中e3-content-service为web工程。
(1)pom.xml
e3-content中的pom.xml
<?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>cn.e3mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>e3-content</artifactId> <packaging>pom</packaging> <modules> <module>e3-content-interface</module> <module>e3-content-service</module> </modules> <dependencies> <dependency> <groupId>cn.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-content-interface中pom.xml
<?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>cn.e3mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>e3-content-interface</artifactId> <name>e3-content-interface</name> <dependencies> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-manager-entity</artifactId> <version>1.0-SNAPSHOT</version> </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> </project>
e3-content-service中的pom.xml
<?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>cn.e3mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>e3-content-service</artifactId> <packaging>war</packaging> <name>e3-content-service</name> <dependencies> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-content-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-manager-dao</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> </dependencies> </project>
(2)e3-content-service中相关配置文件(其中太部分配置文件与e3-manager-service中相同,下方列出不同的地方)
applicationContext-service.xml
注意:修改暴露服务的端口,避免端口冲突!!!!
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!--开启注解支持--> <context:component-scan base-package="cn.e3mall.content.service" /> <!-- 使用dubbo发布服务 --> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="e3-manager" /> <dubbo:registry protocol="zookeeper" address="192.168.25.130:2181" /> <!-- 用dubbo协议在20881端口暴露服务 --> <dubbo:protocol name="dubbo" port="20881" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="cn.e3mall.content.service.ContentCategoryService" ref="contentCategoryServiceImpl" timeout="300000"/> </beans>
applicationContext-tx.xml(注意:只需要修改切面补分的扫描表达式)
<!-- 切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.e3mall.portal.service..*.*(..))" /> </aop:config>
(3)包结构
三、e3-content-interface、e3-content-service代码
package cn.e3mall.content.service; import cn.e3mall.common.entity.E3Result; import cn.e3mall.common.entity.EasyUITreeNodeResult; import java.util.List; /** * 内容分类业务逻辑层 * @Auther: xushuai * @Date: 2018/5/17 23:27 * @Description: */ public interface ContentCategoryService { /** * 加载内容分类列表 * @auther: xushuai * @date: 2018/5/17 23:28 * @return: 返回为树所需要的数据格式 */ List<EasyUITreeNodeResult> list(Long parentId); /** * 新增内容分类 * @auther: xushuai * @date: 2018/5/18 10:55 */ E3Result add(Long parentId, String name); /** * 更新内容分类 * @auther: xushuai * @date: 2018/5/18 21:25 */ void update(Long id, String name); /** * 删除内容分类 * @auther: xushuai * @date: 2018/5/18 21:32 * @return: * @throws: */ void delete(Long id); }
package cn.e3mall.content.service.impl; import cn.e3mall.common.entity.E3Result; import cn.e3mall.common.entity.EasyUITreeNodeResult; import cn.e3mall.content.service.ContentCategoryService; import cn.e3mall.dao.TbContentCategoryMapper; import cn.e3mall.entity.TbContentCategory; import cn.e3mall.entity.TbContentCategoryExample; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 内容分类业务逻辑层实现 * Author: xushuai * Date: 2018/5/17 * Time: 23:30 * Description: */ @Service public class ContentCategoryServiceImpl implements ContentCategoryService { @Autowired private TbContentCategoryMapper contentCategoryMapper; @Override public List<EasyUITreeNodeResult> list(Long parentId) { //创建查询对象 TbContentCategoryExample example = new TbContentCategoryExample(); //封装查询条件 example.createCriteria().andParentIdEqualTo(parentId); //执行查询 List<TbContentCategory> categories = contentCategoryMapper.selectByExample(example); //创建返回结果集合 List<EasyUITreeNodeResult> results = new ArrayList<>(); for(TbContentCategory contentCategory : categories){ //封装查询出的条件为指定格式 EasyUITreeNodeResult node = new EasyUITreeNodeResult(); node.setId(contentCategory.getId()); node.setText(contentCategory.getName()); node.setState(contentCategory.getIsParent()?"closed":"open"); //添加到结果集合中 results.add(node); } return results; } @Override public E3Result add(Long parentId, String name) { //封装内容分类对象 TbContentCategory contentCategory = new TbContentCategory(); contentCategory.setName(name); contentCategory.setParentId(parentId); contentCategory.setSortOrder(1); contentCategory.setStatus(TbContentCategory.STATUS_NORMAL); contentCategory.setIsParent(false); contentCategory.setCreated(new Date()); contentCategory.setUpdated(new Date()); //保存内容分类对象 contentCategoryMapper.insert(contentCategory); //修改父节点isParent TbContentCategory parent = contentCategoryMapper.selectByPrimaryKey(parentId); //如果当前parentId对应的内容分类为子节点,将其设置为父节点 if(!parent.getIsParent()){ //设置parent为父节点 parent.setIsParent(true); //保存更新后的数据 contentCategoryMapper.updateByPrimaryKeySelective(parent); } //返回成功 return E3Result.ok(contentCategory); } @Override public void update(Long id, String name) { //封装接收到的数据 TbContentCategory contentCategory = new TbContentCategory(); contentCategory.setId(id); contentCategory.setName(name); //保存更新 contentCategoryMapper.updateByPrimaryKeySelective(contentCategory); } @Override public void delete(Long id) { //使用ID查询出内容分类对象 TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id); /* * 判断当前要删除的节点是否为父节点,如果不是父节点直接删除,否则不作任何处理 */ if(contentCategory != null && !contentCategory.getIsParent()){ //执行删除 contentCategoryMapper.deleteByPrimaryKey(id); /* * 查看删除节点的父节点还有没有其他子节点,如果没有子节点,将其设置为子节点 */ //获取父节点id Long parentId = contentCategory.getParentId(); //查询父节点对象 TbContentCategory parent = contentCategoryMapper.selectByPrimaryKey(parentId); //判断是否为父节点 if(!isParent(parent)){ //修改其isPrent属性为false parent.setIsParent(false); //保存修改 contentCategoryMapper.updateByPrimaryKeySelective(parent); } } } /* * 判断内容分类是否为父节点(即是否含有子节点) */ private boolean isParent(TbContentCategory parent) { //封装查询条件 TbContentCategoryExample example = new TbContentCategoryExample(); example.createCriteria().andParentIdEqualTo(parent.getId()); //执行查询 List<TbContentCategory> categories = contentCategoryMapper.selectByExample(example); //判断是否为空且长度大于0,如果为空或不大于0,则表示不是父节点 if(categories != null && categories.size() > 0){ return true; } return false; } }
四、dao层仍然使用逆向工程生成的代码,无任何改动
注意:
(1)在e3-manager-web中的pom文件中依赖e3-content-interface
(2)在e3-manager-web中的springmvc.xml中引用ContentCategoryService服务