?表示不确定的数据类型。下面我们来看看怎么使用。
定义模型和子模型
- @Data
- public class Model {
- String id;
- String name;
- String desc;
- }
-
- @Data
- public class SubModel extends Model{
- String subName;
- }
定义List泛型接口和实现
- public interface MultiModelService {
-
- /**
- * 多模型List
- * @param list
- */
- void batchSave(List<? extends Model> list);
- }
-
- @Slf4j
- @Service
- public class MultiModelServiceImpl implements MultiModelService {
-
- @Override
- public void batchSave(List<? extends Model> list) {
- log.info("--MultiModelService batchSave--");
- }
- }
编写测试用例
- @ComponentScan(basePackages = "com.boonya.code.list.service.impl")
- @SpringBootTest
- @SpringBootConfiguration
- public class TestMultiModelService {
-
- @Autowired
- MultiModelService multiModelService;
-
- @Test
- public void test(){
- List<Model> models = new ArrayList<>();
- List<SubModel> subModels = new ArrayList<>();
- multiModelService.batchSave(models);
- multiModelService.batchSave(subModels);
- }
- }
输出效果: