当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Self writing performance testing tool (2)
- iNeuOS工业互联平台,WEB组态(iNeuView)增加工程视图导入、导出功能,及优化和修复,发布:v3.2.1版本
- Prometheus installation configuration
- Can't find other people's problem to solve
- Seam engraving algorithm: a seemingly impossible image size adjustment method
- 自己上手写性能测试工具(二)
- Make a home page
- leetcode之最后一个单词的长度
- The length of the last word in leetcode
- Python cookbook 3rd note (2.1): using multiple qualifiers to split strings
猜你喜欢

iNeuOS工业互联平台,WEB组态(iNeuView)增加工程视图导入、导出功能,及优化和修复,发布:v3.2.1版本

Error running app:Default Activity not found 解决方法

嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王健

Three ways to solve coursera video unable to watch

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

Centos7 operating system security hardening series (2)

害怕重构?都怪我太晚和你介绍该如何重构,现在我来了

How SSL certificate and public IP address affect SEO

快来学习!个性化推荐系统开发指南(附网盘链接)

sql 筛选查询重复列
随机推荐
The length of the last word in leetcode
JS label syntax jumps out of multiple loops
sql 截取数据中 ‘.’的前后数据
C++ exception implementation mechanism
Must see! RDS database all in one
Brief analysis of LinkedList source code
PL/SQL Developer临时用户和新手的功能指南
Day85: Luffy: shopping cart switching price according to different validity period & shopping cart deletion operation & price settlement & foreplay of order page
分布式文档存储数据库之MongoDB索引管理
No space left on device
【CentOS7操作系统安全加固系列】第(2)篇
那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!
Assign the corresponding key and value in the map to the object
Guest interview: Wang Jian
eleven point nine
proxy 的不完整polyfill
How SSL certificate and public IP address affect SEO
Can public IP address and SSL certificate improve SEO?
lodash.js源码-flatten
《Python Cookbook 3rd》笔记(2.1):使用多个界定符分割字符串