SSMP integration case (7) MybatisPlus fast service business layer development

The above SSMP integration case (6) Business service layer logic writing We created and wrote the service business layer code
, but it is a bit troublesome to write one by one.
It is a repetitive work, but in fact, someone has already considered it for us and MP will do it for us. A quick way to develop services

We create an interface called IBookService under the service package in the project.
In fact, some companies require you to add an I in front of the interface. Of course, this mainly depends on personal habits. In fact, most companies are not so picky.

The reference code of IBookService is as follows.
It is also the same as the BaseMapper of the previous dao layer. Many built-in methods are written for you here.

package com.example.bookconfig.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.bookconfig.domain.book;

public interface IBookService extends IService<book> {
    
    
}

In this way, our temporary business layer interface has been written. Of course, this is just a general method, which is some very basic functions that will be used as long as the project is basically used. It will be encapsulated with you, but you must write more complex functions for your future projects. of

Then the interface is done. The implementation class of the business layer should also be written. Create a class called bookServiceI under the impl package under service.
Many people may say that bookServiceImpl is better or better, but we have created a bookServiceImpl before.
insert image description here
In fact, it is called here. What is not so important is that we can understand it. After all, we don’t need other people to develop this project. Whether other people can understand it is our business, right?

BookServiceI must implement the interface IBookService we just created,
but this brings up a question, do so many abstract methods in IService have to be implemented?
insert image description here
Obviously it is impossible, and the official can't do such an outrageous thing.
We can inherit a ServiceImpl class.
This class needs two generic types.
The first is the implementation interface you want to use. Simply put, it is the dao interface. The tool for data manipulation. The second is the attribute class template.
The reference code is as follows

package com.example.bookconfig.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bookconfig.dao.bookDao;
import com.example.bookconfig.domain.book;
import com.example.bookconfig.service.IBookService;
import org.springframework.stereotype.Service;

@Service
public class bookServiceI extends ServiceImpl<bookDao, book> implements IBookService {
    
    
}

That's it, it's very simple, doesn't it feel dreamy hahaha

So don't just talk about it, let's test it

Then we write the code in the test class as follows

package com.example.bookconfig;

import com.example.bookconfig.service.impl.bookServiceI;
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 bookServiceI bookServiceI;

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

The result of the operation is as follows.
insert image description here
The implementation of the MP-like logic layer provides us with this getById to search for a single item based on the id, and it is successful.

Our test class writes the code as follows

book book = new book();
book.setType(1);
 book.setName("数据管理");
 book.setDescription("整改方案叙述 加系统新增功能描述");
 System.out.println(bookServiceI.save(book));

Declare a book class, then use the set method to assign values ​​to each field, and then call the public add function provided by the save business layer. The
running results are as follows.
insert image description here
It is very thoughtful to change it to return true/false instead of affecting the number of rows.
After all, we actually execute In fact, the business only cares about whether it is successful or not.
insert image description here
Seeing the data in the database, it is obvious that it has also entered

Then we write it like this

book book = new book();
book.setId(10);
book.setType(0);
book.setName("知识图谱");
book.setDescription("管理企业结构和知识体现技术收藏");
System.out.println(bookServiceI.updateById(book));

The function he modified is called updateById.
We will change the data with id 10 just added. The
running results are as follows.
insert image description here
I really want to give a thumbs up to the returned type. When
we see the database
insert image description here
, obviously our data is already OK.

Then let’s delete this piece of data in the end, but the deletion here is no longer called delete, but remove.
Friends who have written js dom with jquery will be more impressed by this word.
We write the code as follows

System.out.println(bookServiceI.removeById(10));

Delete id10.
The result is as follows.
insert image description here
True is returned here. We look at the database and
insert image description here
our data just disappears.

then query all

System.out.println(bookServiceI.list());

It is very simple and rude, just call a list
to run the result as follows,
insert image description here
and then we write the code as follows for page query

IPage<book> pagebook = new Page<>(1,3);
IPage<book> page = bookServiceI.page(pagebook);
System.out.println(page.getRecords());

The running results are as follows.
insert image description here
The changes are actually not big, and it is also done through the IPage object.

Guess you like

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