Thoughts on Additions and Modifications in Mybatis-plus

One finishing touch
When we operate on a database, the additions and modifications are twin brothers, and in most cases the additions are maintained again, so the modifications come in handy.
Additionally, if new and modified are not used well, there will often be duplicate primary keys when inserting, or no data or multiple data when updating, which can lead to exceptions.
Because this scenario is often used, here's a summary.
2. Database Design
We designed our database to look like this, where id is the primary key, and we also specified that username and title uniquely identify a record, which we call (username,title) the business primary key. To improve performance, we can index (username,title).
Name
Record article list
Code
News
Table
news
Memo
Describe news information
Group
Name
Column Name
Data Type
Dictionary
Not Null
Memo
Basic Property Group
Identification
id
Varchar(100)
 
Y
 UUID
Entity Relationship Attribute Group
User name
username
{FK}
 
Y
 
Group of Feature Attributes
Title
title
Varchar(100)
 
Y
 
 
content
content
Varchar(100)
 
Y
 
3. Business Scenario Analysis
1. First of all, of course, add new articles to the data table in new ways.
2. The article can be modified later, but not the title.Modifications will come in handy at this time.
Fourth, you can write code like this in Mybatis-plus
@Override
public void add( NewsDTO newsDTO ) {
    try {
        News news;
        try {
            // Updates
            news = detail(newsDTO.getUsername(), newsDTO.getTitle());
            news.setContent(newsDTO.getContent());
        } catch (Exception e) {
            // What's New
            news = new News();
            news.setTitle(newsDTO.getTitle());
            news.setContent(newsDTO.getContent());
            news.setUsername(newsDTO.getUsername());
        }
        this.saveOrUpdate(news);
    } catch (Exception e) {
        log.info(e.getMessage());
        throw new RequestException(ResponseCode.SQL_FAIL);
    }
}


@Override
public News detail( String username, String title ) {
    try {
        return this.getOne(Wrappers.<News>lambdaQuery().eq(News::getUsername, username).eq(News::getTitle, title));
    } catch (RuntimeException e) {
        throw new RequestException(ResponseCode.TOO_MANY_RESULTS);
    }
}

This allows for additions and updates based on the business primary key after finding the record to be modified.Finally, unified additions or modifications are achieved through IService's saveOrUpdate.But I always find it not elegant to write new records in a catch.It's not good to implement a detail method, is there a better one?

Updates are attempted through Lambda and, if they fail, through insert on the BaseMapper interface.You can solve the two problems mentioned above, and the code is simpler.
// The following is added through insert on the BaseMapper interface, and the update implementation is modified according to the business primary key
@Override
public void add( NewsDTO newsDTO ) {
    try {
        // Update situation: Update one first, if you can update successfully, it means update situation, otherwise it is new situation
        boolean flag = new LambdaUpdateChainWrapper<News>(newsMapper)
                .eq(News::getTitle, newsDTO.getTitle())
                .eq(News::getUsername, newsDTO.getUsername())
                .set(News::getContent, newsDTO.getContent()).update();
        log.info("flag is "+flag);
        // What's new: If you can't update your success
        if (!flag) {
            News news = new News();
            news.setTitle(newsDTO.getTitle());
            news.setContent(newsDTO.getContent());
            news.setUsername(newsDTO.getUsername());
            newsMapper.insert(news);
        }
    } catch (Exception e) {
        throw new RequestException(ResponseCode.SQL_FAIL);
    }
}

 

Tags: Database Attribute Mybatis Lambda

Posted on Thu, 07 May 2020 20:53:12 -0400 by ytse