关于通用mapper中的的insertList()方法

关于通用mapper中的的insertList()方法

通用mapper中的insertList()方法有两个:

1. tk.mybatis.mapper.common.special.InsertListMapper包下的insertList()方法:
       使用该方法的实体类主键必须是自增的(需要在实体类中指出)。

如果实体的主键名为’id’,同时主键自增。在不修改代码的情况下,使用insertList()方法实现的批量插入数据后通用mapper能自动回写主键值到实体对象中
如以下实体类和对应mapper:

@Data
@Table(name = "user")
public class User {
    @Id
    @KeySql(useGeneratedKeys = true)
    private Integer id;
    private String username;
    private String desc;
}
public interface UserMapper extends InsertListMapper<User> {

}

如果实体类主键名不是id,同时实体类主键是自增的,想要实现实体类主键回写,需要重写insertList()方法,其实就是修改了注解上的值,把@Options注解上的keyProperty值改为自己实体类的主键名
如以下实体类和对应的mapper:

@Data
@Table(name = "user")
public class User {
   @Id
   @KeySql(useGeneratedKeys = true)
   private Integer uid;
   private String username;
   private String desc;
}
public interface UserMapper extends Mapper<User>, InsertListMapper<User> {

    @Options(keyProperty = "uid",useGeneratedKeys = true)
    @InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
    int insertList(List<User> recordList);
}

2. tk.mybatis.mapper.additional.insert.InsertListMapper包下的insertList()方法:
       该方法不支持主键策略,需要在实体类中指定主键。该方法执行后不会回写实体类的主键值

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值