How to implement batch insertion in Mybatis-Plus
This article will explain in detail how to achieve batch insertion of Mybatis-Plus. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The method of bulk insertion is provided in Mybatis Plus's IService interface, but its internal implementation logic looks like this:
Unexpectedly, it is a circular single insert?! It's funny. All right, do it yourself and have plenty of food and clothing.
one。 Add dependency com.baomidou mybatis-plus-extension 3.4.0 II. Inherit default method injection
Create a new EasySqlInjector.java under injector package
EasySqlInjector.java
Import com.baomidou.mybatisplus.core.injector.AbstractMethod;import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;import java.util.List;/** * @ author: jichunyang * @ description: custom data method injection * @ date: 14:15 on 2020-12-18 * * / public class EasySqlInjector extends DefaultSqlInjector {@ Override public List getMethodList (Class mapperClass) {List methodList = super.getMethodList (mapperClass) MethodList.add (new InsertBatchSomeColumn ()); return methodList;}} three. Inject Bean// into the MybatisPlusConfig configuration file to open the transaction @ EnableTransactionManagement@Configuration@MapperScan ("scanned mapper packet path") public class MybatisPlusConfig {/ / Bean @ Bean public EasySqlInjector easySqlInjector () {return new EasySqlInjector ();} @ Bean ("sqlSessionFactory") public SqlSessionFactory sqlSessionFactory () throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean () / / other configuration items. SqlSessionFactory.setGlobalConfig (globalConfiguration ()); return sqlSessionFactory.getObject ();} @ Bean public GlobalConfig globalConfiguration () {GlobalConfig conf = new GlobalConfig (); / / custom injection needs to be configured here for conf.setSqlInjector (easySqlInjector ()); return conf;}} four. Extended built-in BaseMapper
Create a new EasyBaseMapper API under the mapper package, and extend it with BaseMapper
Import com.baomidou.mybatisplus.core.mapper.BaseMapper;import java.util.List;public interface EasyBaseMapper extends BaseMapper {/ * bulk insert is only applicable to mysql * @ param entityList entity list * @ return affected rows * / Integer insertBatchSomeColumn (List entityList);} five. Business-level implementation
Modify the business mapper interface UserMapper to inherit the EasyBaseMapper just extended
Import org.apache.ibatis.annotations.Mapper;/** * @ author jichunyang * @ description user Mapper * / @ Mapperpublic interface UserMapper extends EasyBaseMapper {}
The use of service implementation layer
Import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import org.springframework.stereotype.Service;@Servicepublic class UserServiceImpl extends ServiceImpl implements IUserService {@ Override @ Transactional public void insertUsers (List users) {/ / Custom batch inserts are used here, and baseMapper can be used directly without declaring baseMapper.insertBatchSomeColumn (users);}}
IUserService is a defined business logic interface, independent of bulk insert configuration
This is the end of the article on "how to insert Mybatis-Plus in bulk". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.