controller层
@Api(tags = "故障模式动态管理控制器")
@Slf4j
@RestController
@RequestMapping("/api/dynamic")
public class DynamicController {
@Autowired
private DynamicService dynamicService;
//分页查询所有
@PostMapping("/findAll")
public ResponseResult findAll(){
return dynamicService.findAll();
}
@PostMapping("/save")
public ResponseResult save(@RequestParam @ApiParam(value = "字段名列表") List<String> fieldList,
@RequestBody @ApiParam(value = "数据列表") DataList dataList){
return dynamicService.save(fieldList,dataList);
}
@PostMapping("/update")
public ResponseResult update(@RequestParam @ApiParam(value = "字段名列表") List<String> fieldList,
@RequestBody @ApiParam(value = "数据列表") DataList dataList){
return dynamicService.update(fieldList,dataList);
}
@DeleteMapping("/delete")
public ResponseResult delete(Integer id){
dynamicService.deleteById(id);
return ResponseResult.okResult();
}
}
数据列表类
@Data
public class DataList {
private List<Map<String,Object>> dataList;
}
service层
public interface DynamicService {
ResponseResult findAll();
ResponseResult save(List<String> fieldList, DataList dataList);
ResponseResult update(List<String> fieldList, DataList dataList);
void deleteById(Integer id);
}
serviceImpl层
@Service
public class DynamicServiceImpl implements DynamicService {
@Autowired
private FaultModesMapper faultModesMapper;
@Override
public ResponseResult findAll() {
return ResponseResult.okResult(faultModesMapper.selectAll()) ;
}
@Override
public ResponseResult save(List<String> fieldList, DataList dataList) {
List<Map<String, Object>> list = dataList.getDataList();
faultModesMapper.save(fieldList,list);
return ResponseResult.okResult();
}
@Override
public ResponseResult update(List<String> fieldList, DataList dataList) {
List<Map<String, Object>> list = dataList.getDataList();
faultModesMapper.updateByList(fieldList,list);
return ResponseResult.okResult();
}
@Override
public void deleteById(Integer id) {
faultModesMapper.deleteById(id);
}
}
mapper层
public interface FaultModesMapper extends BaseMapper<FaultModes> {
List<FaultModes> findList(FaultModesPageDTO dto);
@Select("select * from fault_modes")
List<Map> selectAll();
void save(List<String> fieldList, List<Map<String, Object>> dataList);
void updateByList(List<String> fieldList, List<Map<String, Object>> dataList);
}
mapper.xml层
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imot.mapper.FaultModesMapper">
<insert id="save">
INSERT INTO fault_modes(
<foreach collection="fieldList" item="fieldEach" separator=",">
${fieldEach}
</foreach>
) VALUES
<foreach collection="dataList" item="dataEach" separator=",">
(
<foreach collection="fieldList" item="fieldEach" separator=",">
#{dataEach.${fieldEach}}
</foreach>
)
</foreach>
</insert>
<update id="updateByList">
UPDATE fault_modes set
<foreach collection="fieldList" item="fieldListEach" separator="," >
${fieldListEach} = case
<foreach collection="dataList" item="dataListEach" >
when ( id = #{dataListEach.id} )
then #{dataListEach.${fieldListEach}}
</foreach>
end
</foreach>
<if test="dataList != null and dataList.size() > 0 ">
where
<foreach collection="dataList" item="dataListEach" separator=" or ">
( id = #{dataListEach.id})
</foreach>
</if>
</update>
<select id="findList" resultType="com.imot.pojo.entity.FaultModes">
select * from r01.fault_modes
<where>
<if test="severityLevel!=null and severityLevel!=''">
and severityLevel like concat('%',#{severityLevel},'%')
</if>
</where>
order by create_time DESC
</select>
</mapper>