一:单条插入返回id
useGeneratedKeys为true,持自动生成主键,keyProperty和keyColumn分别代表数据库记录主键字段和java对象成员属性名
dao层 修饰符 为 void,不需要返回任何
<insert id="insertEntity" parameterType="**" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
实现类正确写法,id会自动赋予到实体类中
public Long insertEntity(SattionAlarmEntity entity) {
dao.insertEntity(entity);
return entity.getId();
}
二:批量插入
dao层 修饰符 为 void,不需要返回任何
dao层
void batchInsert(List<TAlarmChannelEntity> list);
实现层
@Override
public List<Long> batchInsert(List<TAlarmChannelEntity> list) {
dao.batchInsert(list);
List<Long> ids=list.stream().map(TAlarmChannelEntity::getId).collect(Collectors.toList());
return ids;
}
<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" >
INSERT INTO t_alarm_channel
(channel, silent_cycle, aggregation_mode,threshold,effective_start_time,effective_end_time,inform_obj,selected,create_time)
VALUES
<foreach collection="list" index="index" item="bo" separator=",">
(
#{bo.channel}, #{bo.silentCycle}, #{bo.aggregationMode},#{bo.threshold},#{bo.effectiveStartTime},#{bo.effectiveEndTime},#{bo.informObj},#{bo.selected},now()
)
</foreach>
</insert>