This article source: making here | | GitEE, click here

First, layered strategy

MVC pattern and code layering strategy. The full name of MVC is ModelViewController, which means Model-View-Controller. As a software design paradigm, it organizes code by separating business logic, data and interface display, and aggregates business logic into a component. There is no need to rewrite the business logic. This is a development pattern, but it is not a layering pattern for code in real development. In general, the backend code of the SSM framework is layered as follows:

  • Controller layer: defines the server interface, input and exit parameters, and some input checks;
  • Service business service layer: assemble business logic, business validation, and build parameter models required by the control layer;
  • DAO data interaction layer: provides data query methods required by the service layer and deals with logic related to data interaction conditions;
  • Mapper persistence layer: based on the native support required by MyBatis framework, it is a commonly used persistence layer component at present.

Second, the control layer

1. REST interface style

Different styles of annotations are used based on the logic of resource access and processing. Such as resource new, update, query, delete.

/** * New */ @PostMapping("/insert") public Integer insert (@requestBody BaseInfo BaseInfo){return baseInfoService.insert(baseInfo); } /** * 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) ; } /** * DeleteMapping("/delete/{id}") public String delete(@PathVariable(value = "id") Integer id) { baseInfoService.delete(id) ; return "SUS" ; }

2, interface reuse degree

It is not recommended that the interface be highly multiplexed, such as adding, deleting, changing and checking the respective docking interface. Basic principles, different client-side operations, for independent interfaces.

/** */ @GetMapping("/ List ") public List<BaseInfo> List () {return BaseInfoService. List (new BaseInfoService ()); } /** * @postmapping ("/search") public List<BaseInfo> search (@requestParam ("userName")) String userName, @RequestParam("phone") String phone) { return,phone) ; }

For example, the common list interface usually has a search mechanism that loads according to conditions, and the judgment conditions of search are very complex. It is suggested to divide it into two interfaces. In practical consideration, in most scenarios, only the list interface is used, and search is rarely used.

3, in and out of the parameters

Check the client must conditions, such as certain conditions must be filled in and selected, if there is a problem, quickly block the request link, so that the program entry control layer can intercept and return.

public String update(@PathVariable(value = "id") Integer id,
                     @RequestBody BaseInfo baseInfo) {
    if (id<1){
        return "error";
    return "update="+baseInfoService.update(baseInfo);

Parameters with less than three parameters can be directly displayed in the parameters, and parameters with more than three parameters can be unified encapsulated using entity classes.

public List<BaseInfo> search (@RequestParam("userName") String userName,
                              @RequestParam("phone") String phone) {
    return,phone) ;

4. Parameter processing

The basic principle of processing parameter format. As a public resource, the server should avoid unnecessary operations. For example, the client can judge whether the return value is null or not by itself, or some common format processing, and use the client to share the pressure of the server properly.

Third, the business service layer

1. Service verification

For example, if the order number is passed in and there is no order data after querying in the database layer, it is called an exception of business nature. There is no problem with the code itself, but the business logic cannot be executed normally.

public InfoModel detail(Integer id){ BaseInfo baseInfo = baseInfoDao.selectByPrimaryKey(id) ; if (baseInfo ! = null){ DetailInfoEntity detailInfoEntity = detailInfoDao.getById(id); If (detailInfoEntity == null){ ("id="+id+" detailInfo "); } return buildModel(baseInfo,detailInfoEntity) ; } ("id="+id+" data completely missing "); return null ; }

2. Assemble business logic

In general, the service layer is a complex piece of logic, which is used to join the core steps of the business. The program can be executed step by step through the decision of the business logic, and a large number of possible object creation and demand data query can be avoided at the program entrance.

public int insert (BaseInfo record){
    record.setCreateTime(new Date());
    int insertFlag = baseInfoDao.insert(record);
    if (insertFlag > 0){
        DetailInfoEntity detailInfoEntity = new DetailInfoEntity();
            return insertFlag ;
    return insertFlag;

3. Data model construction

In general, the business layer is more complex. If you want to understand the business layer quickly, you can provide a method of backreference construction for complex business methods, which can handle the parameters that the service layer should pass back to the control layer. This can make the heavy service layer methods clear.

private InfoModel buildModel (BaseInfo baseInfo,DetailInfoEntity detailInfo){
    InfoModel infoModel = new InfoModel() ;
    return infoModel ;

Fourth, data interaction layer

1. Reverse engineering

Use MyBatis framework or MyBatis -plus framework as a reference here. If it is MyBatis framework, it is recommended that the reverse engineering template code does not do custom modification, if the need for custom methods, in the Mapper and XML level and then custom an extension file, used to store custom methods and SQL logic, so as to avoid the table structure changes caused by strong discomfort.

Of course, most now use MyBatis – Plus as a persistence layer component, which can avoid the above problems.

2. Data interaction

According to the needs of the business layer, the corresponding data query method is provided. It only deals with the logic interacting with the database to avoid the occurrence of business logic. Especially in the distributed architecture, the data query and assembly of different services should not appear in the layer.

public interface BaseInfoDao {

    int insert(BaseInfo record);

    List<BaseInfo> selectByExample(BaseInfoExample example);

    int updateByPrimaryKey(BaseInfo record);

    BaseInfo selectByPrimaryKey(Integer id);

    int deleteByPrimaryKey(Integer id);

    BaseInfo getById (Integer id) ;

Five, the source code address

Making address GitEE,,

Recommended reading: Programming system reorganization

The serial number The project name Making the address GitEE address Recommend index
01 Java describes design patterns, algorithms, and data structures Making, click here GitEE, click here Being fostered fostered fostered fostered
02 Java Foundation, Concurrency, Object-oriented, Web Development Making, click here GitEE, click here Being fostered fostered fostered
03 SpringCloud microservice base component case detail Making, click here GitEE, click here Do do do
04 SpringCloud microservice architecture practical comprehensive case Making, click here GitEE, click here Being fostered fostered fostered fostered
05 Getting started with the SpringBoot Framework basics Making, click here GitEE, click here Being fostered fostered fostered
06 SpringBoot framework integrates common middleware development Making, click here GitEE, click here Being fostered fostered fostered fostered
07 Basic cases of data management, distribution, architecture design Making, click here GitEE, click here Being fostered fostered fostered fostered
08 Big data series, storage, components, computing and other frameworks Making, click here GitEE, click here Being fostered fostered fostered fostered