当前位置:网站首页>编码风格:Mvc模式下SSM环境,代码分层管理
编码风格:Mvc模式下SSM环境,代码分层管理
2020-11-10 00:28:00 【程序猿欧文】
本文源码:GitHub·点这里 || GitEE·点这里
一、分层策略
MVC模式与代码分层策略,MVC全名是ModelViewController即模型-视图-控制器,作为一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,这是一种开发模式,但并不是实际开发中代码的分层模式,通常SSM框架的后端代码分层如下:

- controller控制层:定义服务端接口,入参出参,和一些入参校验;
- service业务服务层:组装业务逻辑,业务校验,构建控制层需要的参数模型;
- dao数据交互层:提供服务层需要的数据查询方法,处理数据交互条件相关的逻辑;
- mapper持久层:基于mybatis框架需要的原生支持,目前很常用的持久层组件;
二、控制层
1、Rest接口风格
基于资源访问和处理的逻辑,使用不同风格的注解。例如资源新增,更新,查询,删除。
/** * 新增 */@PostMapping("/insert")public Integer insert (@RequestBody BaseInfo baseInfo){ return baseInfoService.insert(baseInfo);}/** * 更新 */@PutMapping("/update/{id}")public String update(@PathVariable(value = "id") Integer id, @RequestBody BaseInfo baseInfo) { if (id<1){ return "error"; } baseInfo.setId(id); return "update="+baseInfoService.update(baseInfo);}/** * 主键查询 */@GetMapping("/detail/{id}")public InfoModel detail(@PathVariable(value = "id") Integer id) { return baseInfoService.detail(id) ;}/** * 主键删除 */@DeleteMapping("/delete/{id}")public String delete(@PathVariable(value = "id") Integer id) { baseInfoService.delete(id) ; return "SUS" ;}
2、接口复用度
不建议接口高度复用,例如增删改查都各自对接接口即可,基本原则,不同的客户端端操作,对于独立的接口。
/** * 列表加载 */@GetMapping("/list")public List<BaseInfo> list() { return baseInfoService.list(new BaseInfoExample()) ;}/** * 列表搜索 */@PostMapping("/search")public List<BaseInfo> search (@RequestParam("userName") String userName, @RequestParam("phone") String phone) { return baseInfoService.search(userName,phone) ;}
例如常见的list接口,list通常都有会按条件加载的search机制,而且搜索的判断条件很复杂,建议分为两个接口,从实际考虑,大部分场景下都是只使用list接口,很少使用search搜索。
3、入参出参
校验客户端必须条件,例如某某条件必填必选等,如果有问题,快速阻断请求链路,做到程序入口控制层拦截返回。
@PutMapping("/update/{id}")public String update(@PathVariable(value = "id") Integer id, @RequestBody BaseInfo baseInfo) { if (id<1){ return "error"; } baseInfo.setId(id); return "update="+baseInfoService.update(baseInfo);}
参数在三个以下,可以直接陈列入参,参数在三个或三个以上可以使用实体类统一封装。
@PostMapping("/search")public List<BaseInfo> search (@RequestParam("userName") String userName, @RequestParam("phone") String phone) { return baseInfoService.search(userName,phone) ;}
4、参数处理
出参格式处理度基本原则,服务器作为公共资源,避免非必要操作,例如客户端可自行判断返回值是否为空,null等,或者一些常见格式处理,利用客户端适当分担服务器压力。
三、业务服务层
1、业务校验
例如传入订单号,经过数据库层查询,没有订单数据,这里称为业务性质的异常,代码本身没有问题,但是业务逻辑无法正常执行。
public InfoModel detail(Integer id){ BaseInfo baseInfo = baseInfoDao.selectByPrimaryKey(id) ; if .........
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4710400
边栏推荐
- What can CRM system help enterprises do?
- 《Python Cookbook 3rd》笔记(2.2):字符串开头或结尾匹配
- jt-day10
- SRM系统是什么系统?SRM供应商管理系统功能
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- JMeter的简单使用
- [leetcode] 92 integer inversion
- CUDA_存储器模型
- 编码风格:Mvc模式下SSM环境,代码分层管理
- Gets the property value of a column in the list collection object
猜你喜欢

Fire knowledge online answer activity small program

那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!

推动中国制造升级,汽车装配车间生产流水线3D可视化

sql 截取数据中 ‘.’的前后数据

Software engineering in code -- source code analysis of menu project

JS label syntax jumps out of multiple loops

Coding style: SSM environment in MVC mode, code hierarchical management

CUDA_ Memory model

Experiment 2

Prometheus installation configuration
随机推荐
【LeetCode】 92 整数反转
【CentOS7操作系统安全加固系列】第(2)篇
So what should investors do with the current market? Now a new investment outlet is coming!
Coding style: SSM environment in MVC mode, code hierarchical management
ES6, ES7, es8 Learning Guide
Coding style: SSM environment in MVC mode, code hierarchical management
What is the architecture of a live broadcast system worth more than $1 billion?
Usage of [:] and [::] in Python
C + + game development
将Map中对应的key和value赋值到对象中
The kth smallest node in the print binary search tree of offer
One image can hold 16x16 words! ——Transformers for large scale image scaling recognition (a brief review of ICLR 2021 papers)
价值超10亿美元的直播系统架构图是什么样子的?
CUDA_ Get the specified device
消防知识线上答题活动小程序复盘
YouTube subscription: solve the problem of incomplete height display of YouTube subscription button in pop-up window
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王健
Centos7 operating system security hardening series (2)
Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法
《Python Cookbook 3rd》笔记(2.2):字符串开头或结尾匹配