通用mapper批量新增


一、tk.mapper 4.0版本之前

tk.mybatis.mapper.common.MySqlMapper中没有添加@tk.mybatis.mapper.annotation.RegisterMapper 这个注解,需要将该类配置到mappers中:

修改后的通用接口扫描代码:

@Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        
        Properties properties=new Properties();
        properties.put("mappers","tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.MySqlMapper");

        MapperScannerConfigurer msc=new MapperScannerConfigurer();      
        msc.getMapperHelper().setProperties(properties);
        msc.setBasePackage(DataSourceConfig.PACKAGE);
        msc.setSqlSessionFactoryBeanName(DataSourceConfig.SESSION_FACTORY_BEAN_NAME);
        
        return msc;
    }

参考:https://www.cnblogs.com/yql1986/p/9459478.html

然后dao继承该接口就可以了:

public interface UserSystemMessageDao extends Mapper<UserSystemMessagePo>, MySqlMapper<UserSystemMessagePo> {

使用如下:

                // 批量添加用户系统消息
                userSystemMessageDao.insertList(addList);

 

一、tk.mapper 4.0版本之后

tk.mybatis.mapper.common.MySqlMapper中添加了@tk.mybatis.mapper.annotation.RegisterMapper,该注解会自动注入当前mapper,源码如下:

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2017 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package tk.mybatis.mapper.common;

import tk.mybatis.mapper.common.special.InsertListMapper;
import tk.mybatis.mapper.common.special.InsertUseGeneratedKeysMapper;

/**
 * 通用Mapper接口,MySql独有的通用方法
 *
 * @param <T> 不能为空
 * @author liuzh
 */
@tk.mybatis.mapper.annotation.RegisterMapper
public interface MySqlMapper<T> extends
        InsertListMapper<T>,
        InsertUseGeneratedKeysMapper<T> {

}

dao类直接继承MySqlMapper进行批量添加就可以,跟上面一样;

 

 

 

posted @ 2022-04-26 10:12  迷走神经  阅读(1687)  评论(0编辑  收藏  举报