前段时间项目中有个需求:实现某业务的运维功能,主要是对10张数据库表的增删改查,没有复杂的业务逻辑,只是满足运维人员的基本需要,方便他们快速分析定位问题。这里简单记录分享下实现方案,仅供参考。
一、项目方案
根据不同的表定义各自的Vo,然后继承公共的BaseDataVo。设计接口IDataProcessor和抽象处理类AbstractDataProcessor,这个AbstractDataProcessor定义增删改查模板方法。根据不同策略执行不同的processor,比如NodeProcessorService、NodeAutoProcessorService等。
二、代码实现
这里只实现了查询方法,其他增加、删除、修改可以依次实现!
1、定义Vo
BaseDataVo
@Getter
@Setter
public class BaseDataVo implements Serializable {private Integer id;private String creator;private String last_update_by;private Date create_time;private Date last_update_time;
}
NodeVo
@Getter
@Setter
public class NodeVo extends BaseDataVo{String esn;String name;
}
2、设计表名及对应服务枚举类TableNameServiceEnum
@Getter
@AllArgsConstructor
public enum TableNameServiceEnum {NODE("nodeVo", "nodeProcessorService"),NODE_AUTO("nodeAutoVo", "nodeAutoProcessorService");private String tableName;private String serviceName;public static Map<String, String> map = new HashMap<>();static {Arrays.stream(values()).forEach(v->map.put(v.getTableName(),v.getServiceName()));}public static String getServiceName(String tableName){return map.get(tableName);}
}
3、IDataProcessor用于定义AbstractDataProcessor所需的接口
public interface IDataProcessor {// 这里只实现了查询方法,其他增加、删除、修改可以依次实现List<? extends BaseDataVo> findList(SearchVo searchVo, String tableName);
}
IDataOperationService定义增删改查模板方法
public interface IDataOperationServices {List<? extends BaseDataVo> findList(SearchVo searchVo, String tableName);
}
4、抽象处理类及各自业务实现
AbstractDataProcessor
public abstract class AbstractDataProcessor implements IDataProcessor {@Overridepublic List<? extends BaseDataVo> findList(SearchVo searchVo, String tableName) {return findPagedList(searchVo,tableName);}// 这里定义模板方法,执行具体的processorprotected abstract List<? extends BaseDataVo> findPagedList(SearchVo searchVo, String tableName);
}
DataOperationService业务处理service层
@Service
public class DataOperationService implements IDataOperationServices {@Autowiredprivate Map<String, IDataProcessor> dataProcessMap;// 这里根据tableName获取执行不同的processor@Overridepublic List<? extends BaseDataVo> findList(SearchVo searchVo, String tableName) {return Optional.ofNullable(tableName).filter(name -> !name.isEmpty()).map(this::getDataProcessor).map(processor -> processor.findList(searchVo, tableName)).orElse(null);}// 这里根据tableName获取获取不同的processorprivate IDataProcessor getDataProcessor(String tableName) {String serviceName = TableNameServiceEnum.getServiceName(tableName);return dataProcessMap.get(serviceName);}
}
node_auto业务处理类
@Service
public class NodeAutoProcessorService extends AbstractDataProcessor{@Autowiredprivate NodeAutoMapper nodeAutoMapper;@Overrideprotected List<? extends BaseDataVo> findPagedList(SearchVo searchVo, String tableName) {QueryWrapper<NodeAutoVo> nodeAutoVoQueryWrapper = new QueryWrapper<>();nodeAutoVoQueryWrapper.eq("esn",searchVo.getEsn());nodeAutoVoQueryWrapper.eq("name",searchVo.getName());return nodeAutoMapper.selectList(nodeAutoVoQueryWrapper);}
}
node业务处理类
@Service
public class NodeProcessorService extends AbstractDataProcessor{@Autowiredprivate NodeMapper nodeMapper;@Overrideprotected List<? extends BaseDataVo> findPagedList(SearchVo searchVo, String tableName) {QueryWrapper<NodeVo> nodeVoQueryWrapper = new QueryWrapper<>();nodeVoQueryWrapper.eq("esn",searchVo.getEsn());nodeVoQueryWrapper.eq("name",searchVo.getName());return nodeMapper.selectList(nodeVoQueryWrapper);}
}
5、各自对应Mapper
这里是设置在多数源场景下,使用mysql1配置
NodeMapper
@DS("mysql1")
@Repository("NodeMapper")
public interface NodeMapper extends BaseMapper<NodeVo> {}
NodeAutoMapper
@DS("mysql1")
@Repository("NodeAutoMapper")
public interface NodeAutoMapper extends BaseMapper<NodeAutoVo> {}
6、DataOperationController控制层
@RestController
public class DataOperationController {@Autowiredprivate DataOperationService dataOperationService;@RequestMapping("findList")public List<? extends BaseDataVo> findList(@RequestBody SearchVo searchVo, @RequestParam String tableName) {return dataOperationService.findList(searchVo, tableName);}
}
三、测试方案
1、根据不同的tableName,查询结果有区别
2、项目结构及源码地址
源码下载:点我下载 欢迎Star