SSMP integration case (6) Business service layer logic writing

Before, we still set up the structure of the data layer
, then the next step is the business layer.
Some developers may have some misunderstandings and confuse the function naming of the business layer and the data layer.

For example, if we have a users table
, we need to create a login function
. The interface of the business layer is undoubtedly called login. It receives two parameters: userName and userPassword,
and then the data layer is more particular. Maybe some people still call it login. What
needs to be emphasized here is The business layer focuses on business, so the name of the method used to log in should be linked to login. There is nothing wrong with login,
but the data layer should focus on its operations in the database. Login cannot obtain user information based on user name and password, so the most reasonable name for the data layer should be It is queryUsers that receives two parameters userName userPassword
Of course, you can also follow your personal preferences. This is just a specification, not mandatory. If you don’t follow the same code, it can run well. This is only a matter of later maintenance.

We create a package called
service in the same directory as the java
startup class, and just put the business layer logic here.
insert image description here
Create an interface called bookService under it
, which is the interface that our book corresponds to in the business layer.

If the later function business layer is business-oriented, then name it after the business function. If it is just an ordinary addition, deletion, modification and query, then just use the most basic addition, deletion, modification and query to name it.

The bookService reference code is as follows

package com.example.bookconfig.service;

import com.example.bookconfig.domain.book;
import java.util.List;

public interface bookService {
    
    
    //获取整个表的数据
    List<book> queryList();
    //通过id获取单条数据
    book getById(Integer id);
    //新增数据
    Boolean addBook(book book);
    //修改数据
    Boolean editBook(book book);
    //删除数据
    Boolean deleteBook(Integer id);
}

Here we define the basic functions of adding, deleting, checking, and modifying.
In fact, we will add page-by-page checking and checking by conditions later, but let’s do this first and then add it later.

We create a package called impl in the service directory to store the implementation class of our logic layer interface
insert image description here
, and then create a bookServiceImpl class below to be the implementation class of the bookService interface.
The reference code is as follows

package com.example.bookconfig.service.impl;

import com.example.bookconfig.dao.bookDao;
import com.example.bookconfig.domain.book;
import com.example.bookconfig.service.bookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class bookServiceImpl implements bookService {
    
    

    @Autowired
    private bookDao bookDao;

    @Override
    public List<book> queryList() {
    
    
        return bookDao.selectList(null);
    }

    @Override
    public book getById(Integer id) {
    
    
        return bookDao.selectById(id);
    }

    @Override
    public Boolean addBook(book book) {
    
    
        return bookDao.insert(book) > 0;
    }

    @Override
    public Boolean editBook(book book) {
    
    
        return bookDao.updateById(book) > 0;
    }

    @Override
    public Boolean deleteBook(Integer id) {
    
    
        return bookDao.deleteById(id) > 0;
    }
}

The composition of this class is relatively complicated. First,
we use the @Service annotation to declare that this is a business layer management.
Then the class is used to implement the bookService interface. Since it is implemented, all its abstract methods must be rewritten.
The first two have nothing to say. The database operation obtained by calling bookDao by inheriting BaseMapper, but here involves a conversion type of operation is the BaseMapper operation database. After adding, modifying and deleting, its Api returns the number of rows affected by this sql, so we don’t care how many he returns directly. Judging whether it is 0 because as long as it is not 0, it means that the result of his execution is successful

Then don't talk so much, let's test it directly

Our test class writes the code as follows

package com.example.bookconfig;

import com.example.bookconfig.service.bookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BookConfigApplicationTests {
    
    

    @Autowired
    private bookService bookService;

    @Test
    void contextLoads() {
    
    
        System.out.println(bookService.getById(1));
    }
}

The running results are as follows
insert image description here
Here, what our test class introduces is directly
the logic of the service layer used by bookService. Our query based on id is no problem.

Then I will not test one by one here

But don’t be lazy because the things in the business layer are to write test cases. I have already executed them in private, but I don’t want to waste everyone’s time.

Then we add a paging query function to the bookService interface

//分页查询
IPage<book> getPageBook(int page,int pageSize);

This function is used for pagination query.
Our bookServiceImpl writes the corresponding implementation method

@Override
public IPage<book> getPageBook(int page, int pageSize) {
    
    
    IPage IPage = new Page(page,pageSize);
    return bookDao.selectPage(IPage,null);
}

Here we directly convert page and pageSize into Ipage format and then use it to operate selectPage

We write the code in the test class as follows

IPage<book> page = bookService.getPageBook(1,5);
System.out.println(page.getRecords());

We call getPageBook to check 5 entries from the first page,
and the results are as follows
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/131371234