新增(save)
Mapper 内置方法
save(T)
:实体类保存save(T,boolean)
:实体类保存(boolean为是否null值也保存)save(Model)
:Model类保存save(Model,boolean)
:Model类保存(boolean为是否null值也保存)saveOrUpdate(T)
:实体类保存或修改saveOrUpdate(T,boolean)
:实体类保存或修改(boolean为是否null值也保存)saveOrUpdate(Model)
:Model类保存或修改saveOrUpdate(Model,boolean)
:Model类保存或修改(boolean为是否null值也保存)save(List<T>)
:多个实体类保存(非批量操作)save(List<T>,boolean)
:多个实体类保存(非批量操作)(boolean为是否null值也保存)saveModel(List<Model>)
:多个Model类保存(非批量操作)saveModel(List<Model>,boolean)
:多个Model类保存(非批量操作)(boolean为是否null值也保存)saveBatch(List<T>)
:多个实体类保存(批量操作)saveBatch(List<T>,saveFields)
:多个实体类,指定列保存(批量操作)saveModelBatch(List<Model>)
:多个Model类保存(批量操作)saveModelBatch(List<Model>,saveFields)
:多个Model类,指定列保存(批量操作)
基于实体类
java
public class Demo {
@Autowired
private SysUserMapper sysUserMapper;
public void save() {
SysUser sysUser = new SysUser();
sysUser.setUserName("demo");
sysUserMapper.save(sysUser);
}
}
基于Model类
Model类为了save时,不用处理太多的字段,只保存部分字段;同时也简单类的转换,方便快捷
Model类
java
@Data
public class SysUserModel implements Model<SysUser> {
private Integer id;
private String userName;
//private String password;
//private Integer roleId;
private LocalDateTime createTime;
}
基于Model类 save
java
public class Demo {
@Autowired
private SysUserMapper sysUserMapper;
public void save() {
SysUserModel sysUser = new SysUserModel();
sysUser.setUserName("demo");
sysUserMapper.save(sysUser);
}
}
批量save
java
public class Demo {
@Autowired
private SysUserMapper sysUserMapper;
public void saveBatch() {
List<SysUser> sysUserList = new ArrayList<>();
//sysUserList.add() ...
sysUserMapper.saveBatch(sysUser);
}
}
动态save
insert into select
java
public class Demo {
@Autowired
private SysUserMapper sysUserMapper;
public void saveBatch() {
InsertChain.of(sysUserMapper)
.insert(SysUser.class)
.fields(SysUser::getUserName, SysUser::getRoleId)
.fromSelect(Query.create()
.select(SysUser2::getUserName, SysUser2::getRoleId)
.from(SysUser2.class)
)
.execute();
}
}
或
java
public class Demo {
@Autowired
private SysUserMapper sysUserMapper;
public void saveBatch() {
InsertChain.of(mapper)
.insert(SysUser.class)
.insertSelect(SysUser::getId, Methods.value(100))
.insertSelect(SysUser::getValue1, GetterFields.of(SysUser2::getValue1, SysUser2::getValue1), cs -> cs[0].concat(cs[1]))
.insertSelect(SysUser::getValue2, SysUser2::getValue2)
.insertSelect(SysUser::getValue3, SysUser2::getValue3)
.insertSelect(SysUser::getCreateTime, SysUser2::getCreateTime)
.insertSelectQuery(query -> query.from(SysUser2.class).eq(SysUser2::getId, 1))
.execute();
}
}
insert into values
java
public class Demo {
@Autowired
private SysUserMapper sysUserMapper;
public void saveBatch() {
InsertChain.of(sysUserMapper)
.insert(SysUser.class)
.fields(SysUser::getUserName, SysUser::getPassword)
.values(Arrays.asList("admin","123"))
.values(Arrays.asList("test","1234"))
.execute();
}
}