All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.mybatisplus.service.MyBatisPlusServiceImpl Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.mybatisplus.service;

import java.util.Collection;

import org.springframework.transaction.annotation.Transactional;
import org.zodiac.mybatisplus.base.BaseEntity;
import org.zodiac.mybatisplus.base.SecurityBaseServiceImpl;
import org.zodiac.mybatisplus.injector.MyBatisPlusMethod;
import org.zodiac.mybatisplus.mapper.MyBatisPlusMapper;
import org.zodiac.security.SecurityAuthOperations2;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;

public class MyBatisPlusServiceImpl, T extends BaseEntity> extends SecurityBaseServiceImpl implements MyBatisPlusService {

    public MyBatisPlusServiceImpl(SecurityAuthOperations2 securityAuthOperations) {
        super(securityAuthOperations);
    }

    @Override
    public boolean saveIgnore(T entity) {
        return SqlHelper.retBool(baseMapper.insertIgnore(entity));
    }

    @Override
    public boolean saveReplace(T entity) {
        return SqlHelper.retBool(baseMapper.replace(entity));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveIgnoreBatch(Collection entityList, int batchSize) {
        return saveBatch(entityList, batchSize, MyBatisPlusMethod.INSERT_IGNORE_ONE);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveReplaceBatch(Collection entityList, int batchSize) {
        return saveBatch(entityList, batchSize, MyBatisPlusMethod.REPLACE_ONE);
    }

    /**
     * 获取 SQL语句。
     *
     * @param sqlMethod ignore
     * @return sql
     */
    protected String sqlStatement(MyBatisPlusMethod sqlMethod) {
        return SqlHelper.table(currentModelClass()).getSqlStatement(sqlMethod.getMethod());
    }

    protected final boolean saveBatch(Collection entityList, int batchSize, MyBatisPlusMethod sqlMethod) {
        String sqlStatement = sqlStatement(sqlMethod);
        executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy