我使用AngularJS + Spring-data-mongodb + MongoDb创建了一个项目。我想用一个根据对象继承的概念动态改变的模型来管理CRUD操作。
前端: 如果用户选择“车辆”,那么表格将仅包含车辆的属性。 如果用户选择车辆类型“汽车”,那么表格将包含“车辆”的属性加上“汽车”的属性。 如果用户选择车辆类型“摩托车”,那么表格将包含“车辆”的属性加上“摩托车”的属性。
AngularJS表单模型:
<div ng-controller="vehicleFormCtrl">
<input type="text" class="form-control" id="InputFieldA" ng-model="vehicle.name">
<select class="form-control" ng-model="vehicleType" ng-change="selectVehicleType()" ng-options="k as v for (k, v) in vehicleTypes">
</select>
...
<input type="text" class="form-control" id="InputFieldCar" ng-model="car.name">
...
<input type="text" class="form-control" id="InputFieldMotorcyle" ng-model="motorcycle.name">
...
</div>
Java对象:
@Document
public class Vehicle {
@Id
private String id;
...
private String type;
private String name;
}
@Document
public class Car extends Vehicle {
@Id
private String id;
...
private String carColor;
}
@Document
public class Motorcycle extends Vehicle {
@Id
private String id;
...
private String motorcycleColor;
}
这是Spring Controller方法:
@RequestMapping(method = RequestMethod.POST, value = "/save", consumes = "application/json",produces = "application/json")
@ResponseBody
public ResponseEntity<Void> saveVehicle(@RequestBody VehicleDto vehicle) {
...
}
这是Spring Repository方法:
MongoTemplate mongoTemplate;
Vehicle vehicle = new Vehicle();
...
mongoTemplate.insert(vehicle, "vehicle"); // Vehicle.class or Car.class or Motorcycle.class ?
mongodb文件示例(“车辆”集合):
{"_id":"1", "type":"Vehicle", "name":"Tesla"}
{"_id":"2", "type":"Car", "name":"LandRover", "carColor":"red"}
{"_id":"3", "type":"Motorcycle", "name":"Ducati", "motorcycleColor":"yellow"}
如何管理@Document Pojo,CRUD方法和我的AngularJs项目以管理对象继承? 这种方法是一个很好的选择吗?哪些是替代品?
我阅读了mongodb文档和其他相关问题,例如Spring-data mongodb repository and inheritance和inheritance in document database?。
我正在使用AngularJS 1.2,Spring 4.1和spring-data-mongodb 1.8。 感谢。