mybatis.xml文件中的写法
useGeneratedKeys:是否使用jdbc的getGenereatedKeys方法获取主键并赋值到keyProperty设置的属性中 keyProperty:实体类主键字段 parameterType:参数类型
<insert id="insertOne" useGeneratedKeys="true" keyProperty="id" parameterType="io.renren.modules.shigoto.entity.AppTaskInformationEntity">
insert into app_task_information (task_name, app_file_md5_list, status, message, create_time, update_time)
values(
#{appTaskInformationEntity.taskName},
#{appTaskInformationEntity.appFileMd5List},
#{appTaskInformationEntity.status},
#{appTaskInformationEntity.message},
#{appTaskInformationEntity.createTime},
#{appTaskInformationEntity.updateTime})
</insert>
Mapper层接口的写法
Integer insertOne(@Param("appTaskInformationEntity")AppTaskInformationEntity appTaskInformationEntity);
Service层实现类的写法
@Override
public Integer insert(AppTaskInformationEntity appTask) {
Integer integer = appTaskInformationDao.insertOne(appTask);
System.out.println(integer);
return integer;
}
Controller层的写法
@Transactional
public R insert(@RequestParam("appTaskName") String appTaskName, @RequestParam("ids") List<Integer> ids){
if (StringUtils.isEmpty(appTaskName) || ids.size()<=0 || ids==null) return R.error("参数错误!");
List<String> apkFileMd5s = new ArrayList<>();
for (Integer id : ids) {
AndroidAppInformationEntity android = androidAppInformationService.selectById(id);
apkFileMd5s.add(android.getApkFileMd5());
}
AppTaskInformationEntity appTask = new AppTaskInformationEntity();
String md5List = String.valueOf(apkFileMd5s).replace(" ", "").replace("[", "").replace("]", "");
appTask.setTaskName(appTaskName);
appTask.setAppFileMd5List(md5List);
appTask.setStatus(1);
appTask.setCreateTime(new Date());
appTask.setUpdateTime(new Date());
Integer id = appTaskInformationService.insert(appTask);
Integer task_id = appTask.getId();
System.out.println("task_id "+task_id);
for (Integer android_id : ids) {
AndroidAppInformationEntity android = androidAppInformationService.selectById(android_id);
AppIpv6InformationEntity appIpv6 = new AppIpv6InformationEntity();
appIpv6.setTaskId(task_id);
appIpv6.setAppId(android.getId());
appIpv6.setApkFileMd5(android.getApkFileMd5());
appIpv6.setCreateTime(new Date());
appIpv6.setUpdateTime(new Date());
appIpv6InformationService.insert(appIpv6);
}
return R.ok();
}
|