当前位置: 代码迷 >> 综合 >> SSM汽车租借系统 layui+SSM (二) 接口规范问题+Json化
  详细解决方案

SSM汽车租借系统 layui+SSM (二) 接口规范问题+Json化

热度:40   发布时间:2023-11-15 22:44:28.0

目录

常量接口规范问题

简单Json处理成标准Json:(动态生成菜单目录)

小结:


常量接口规范问题

编程的时候遇到这么一个情况:

package com.sxt.sys.constast;
/*** 常量接口* @author YDLin* @date 2020年2月16日2020年2月16日* @Description*/
public interface SysConstast {String USER_LOGIN_ERROR_MSG="用户名或密码不正确";/*** 可用状态*/Integer AVAILABLE_TRUE =1;Integer AVAILABLE_FALSE =1;/*** 用户类型*/Integer USER_TYPE_SUPER=1;Integer USER_TYPE_NORMAL=1;/*** 是否展开*/Integer SPREAD_TRUE=1;Integer SPREAD_FALSE=0;
}

虽然尽可能采用接口编程但是接口中定义常亮还是需要注意的:

java中维护常量使用常量类?接口?还是枚举呢?

https://blog.csdn.net/f641385712/article/details/80394402

简单Json处理成标准Json:(动态生成菜单目录)

TreeNode.java

/*** 首页左边导航树的构造器* * @param id* @param pid* @param title* @param icon* @param href* @param spread* @param target*/public TreeNode(Integer id, Integer pid, String title, String icon, String href, Boolean spread, String target) {super();this.id = id;this.pid = pid;this.title = title;this.icon = icon;this.href = href;this.spread = spread;this.target = target;}

TreeNodeBuilder.java

package com.sxt.sys.utils;import java.util.ArrayList;
import java.util.List;public class TreeNodeBuilder {/*** 把简单的集合转成有层级关系的集合* * @param nodes* @param topPid* @return*/public static List<TreeNode> builder(List<TreeNode> nodes, Integer topPid) {List<TreeNode> treeNodes = new ArrayList<>();for (TreeNode n1 : nodes) {if (n1.getPid() == topPid) {treeNodes.add(n1);}for (TreeNode n2 : nodes) {if (n2.getPid() == n1.getId()) {n1.getChildren().add(n2);}}}return treeNodes;}
}

在控制层中:

思路:先存放在ArrayList()中,在TreeNodeBuilder.builder(nodes, 1)遍历化成标准Json进行

对于数据类型尽可能选用包装类

package com.sxt.sys.controller;import java.util.ArrayList;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.sxt.sys.constast.SysConstast;
import com.sxt.sys.domain.Menu;
import com.sxt.sys.domain.User;
import com.sxt.sys.service.MenuService;
import com.sxt.sys.utils.TreeNode;
import com.sxt.sys.utils.TreeNodeBuilder;
import com.sxt.sys.utils.WebUtils;
import com.sxt.sys.vo.MenuVo;/*** 菜单管理控制器* * @author YDLin**/
@RestController
@RequestMapping("menu")
public class MenuController {@Autowiredprivate MenuService menuService;@RequestMapping("loadIndexLeftMenuJson")public List<TreeNode> loadIndexLeftMenuJson(MenuVo menuVo){//得到当前登陆的用户对象User user=(User) WebUtils.getHttpSession().getAttribute("user");List<Menu> list=null;menuVo.setAvailable(SysConstast.AVAILABLE_TRUE);//只查询可用的if(user.getType()==SysConstast.USER_TYPE_SUPER) {list=this.menuService.queryAllMenuForList(menuVo);}else {list=this.menuService.queryMenuByUserIdForList(menuVo, user.getUserid());}List<TreeNode> nodes= new ArrayList<>();//把list里面的数据放到nodesfor (Menu menu : list) {Integer id=menu.getId();Integer pid=menu.getPid();String title=menu.getTitle();String icon=menu.getIcon();String href=menu.getHref();Boolean spread=menu.getSpread()==SysConstast.SPREAD_TRUE?true:false;String target=menu.getTarget();nodes.add(new TreeNode(id, pid, title, icon, href, spread, target));}return TreeNodeBuilder.builder(nodes, 1);}}

效果图:

小结:

第一次接触将菜单设置成数据库字段,然后进行动态生成‘!!这对于后期权限开发可以做到一套前端开发模板的复用

Json 格式处理成标准JSon 需要进行两次for循环

尽肯能使用接口编程,但是将常量定义在接口并不规范,应使用final 常量类或者枚举

  相关解决方案