当前位置:网站首页>Coding style: SSM environment in MVC mode, code hierarchical management
Coding style: SSM environment in MVC mode, code hierarchical management
2020-11-10 00:28:00 【Irving the procedural ape】
In this paper, the source code :GitHub· Click here || GitEE· Click here
One 、 Layering strategy
MVC Pattern and code layering strategy ,MVC The full name is ModelViewController The model - View - controller , As a software design paradigm , Using a business logic 、 data 、 The interface displays the separated method organization code , Gather business logic into a component , While improving and personalizing the interface and user interaction , No need to rewrite business logic , It's a development model , But it's not the layered pattern of code in actual development , Usually SSM The framework's back-end code layers are as follows :

- controller Control layer : Define the server interface , In and out , And some input parameters ;
- service Business services layer : Assemble business logic , Business verification , The parameter model needed to build the control layer ;
- dao Data interaction layer : Provide the data query method needed by the service layer , Dealing with logic related to data interaction conditions ;
- mapper Persistence layer : be based on mybatis The framework needs native support , The most commonly used persistence layer component at present ;
Two 、 Control layer
1、Rest The interface style
Based on the logic of resource access and processing , Use different styles of annotations . For example, new resources , to update , Inquire about , Delete .
/** * newly added */@PostMapping("/insert")public Integer insert (@RequestBody BaseInfo baseInfo){ return baseInfoService.insert(baseInfo);}/** * to update */@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);}/** * Primary key query */@GetMapping("/detail/{id}")public InfoModel detail(@PathVariable(value = "id") Integer id) { return baseInfoService.detail(id) ;}/** * Delete primary key */@DeleteMapping("/delete/{id}")public String delete(@PathVariable(value = "id") Integer id) { baseInfoService.delete(id) ; return "SUS" ;}
2、 Interface reuse
High reuse of interfaces is not recommended , For example, add, delete, modify and check all the interfaces , The basic principle of , Different client side operations , For independent interfaces .
/** * List loading */@GetMapping("/list")public List<BaseInfo> list() { return baseInfoService.list(new BaseInfoExample()) ;}/** * List search */@PostMapping("/search")public List<BaseInfo> search (@RequestParam("userName") String userName, @RequestParam("phone") String phone) { return baseInfoService.search(userName,phone) ;}
For example, common list Interface ,list Usually, there will be conditional loading search Mechanism , And the search criteria are complex , It is suggested that there are two interfaces , From a practical point of view , Most of the scenarios are only used list Interface , Rarely used search Search for .
3、 In and out
Verification client must be conditional , For example, a certain condition is required , If there are questions , Quickly block the request link , The program entrance control layer intercepts and returns .
@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);}
The parameters are less than three , It can be displayed directly into the reference , If there are three or more parameters, entity classes can be used to encapsulate them .
@PostMapping("/search")public List<BaseInfo> search (@RequestParam("userName") String userName, @RequestParam("phone") String phone) { return baseInfoService.search(userName,phone) ;}
4、 Processing parameters
The basic principle of the processing degree of the output parameter format , Servers as public resources , Avoid unnecessary operations , For example, the client can judge whether the return value is empty ,null etc. , Or some common format processing , Use the client to share the server pressure properly .
3、 ... and 、 Business services layer
1、 Business verification
For example, pass in the order number , Through the database layer query , No order data , This is called a business nature exception , There's no problem with the code itself , But business logic doesn't work properly .
public InfoModel detail(Integer id){ BaseInfo baseInfo = baseInfoDao.selectByPrimaryKey(id) ; if .........
版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
边栏推荐
- [Python learning manual notes] 001. Preface to Python
- CUDA_ Get the specified device
- 异常:Invalid or unexpected token
- 探访2020 PG技术大会
- Functional guide for temporary users and novices of PL / SQL developer
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- 【CentOS7操作系统安全加固系列】第(2)篇
- 接缝雕刻算法:一种看似不可能的图像大小调整方法
- Unity使用transform.Rotate进行三维旋转角度出现偏差
- mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
猜你喜欢

Python cookbook 3rd note (2.1): using multiple qualifiers to split strings

解决Coursera视频无法观看的三种方法(亲测有效)

How much is the cost of CRM system?

Incomplete Polyfill of proxy

Function calculation advanced IP query tool development

For programmers, those unfamiliar and familiar computer hardware

Aikang Guobin denounced Guoxin Securities report as untrue and sent a lawyer's letter

Python prompt attributeerror or depreciation warning: This module was degraded solution

CUDA_共享内存、访存机制、访问优化

Validation failed for one or more entities. See 'entityvalidationerrors' solution
随机推荐
接缝雕刻算法:一种看似不可能的图像大小调整方法
Fire knowledge online answer activity small program
表单验证,为避免全局污染,少定义全局变量写法
C++ exception implementation mechanism
mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
Simple use of JMeter
proxy 的不完整polyfill
The number of more than half of the array is printed by the sword
JS label syntax jumps out of multiple loops
获取List集合对象中某一列属性值
Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法
CUDA_ Register and local memory
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
一幅图像能顶16x16字!——用于大规模图像缩放识别的变压器(对ICLR 2021年论文的简要回顾)
jt-day10
C / C + + Programming Notes: C language development tank war! In memory of our lost little overlord game
sql 筛选查询重复列
Prometheus installation configuration
利用尾巴作为时间序列进行处理来识别鲸鱼
剑指offer之打印二叉搜索树中第k小的结点