为我正在处理的每个项目(来自数据库)创建 Spring @Repository 和 @Controller
Creating Spring @Repository and @Controller for every item I'm working with(from database)
在处理涉及从数据库请求多种数据类型的项目时,我遇到了以下问题:
假设我有 2 个 java classes 对应于数据库实体:
路线
public class Route {
public Route(int n, int region, Date fdate, boolean changed, int points,
int length) {
super();
this.n = n;
this.region = region;
this.fdate = fdate;
this.changed = changed;
this.points = points;
this.length = length;
}
}
运营商
public class Carrier {
public Carrier(...) {
this.id = src.getId();
this.name = src.getName();
this.instId = src.getInstId();
this.depotId = src.getDepotId();
}
如果是这样,创建 Dao 接口和 classes 的正确方法是什么?我是这样做的 -
@Repository
public class CarrierDaoImpl implements CarrierDao{
@Autowired
DataSource dataSource;
public List<Carrier> getAllOrgs() { ... }
}
@Repository
public class RoutesDaoImpl implements RoutesDao {
@Autowired
DataSource dataSource;
public ArrayList<AtmRouteItem> getRoutes(AtmRouteFilter filter) { ... }
}
我正在为每个 java class item\db 实体创建一个@Repository DAO,然后为有关承运人和路线的请求创建 2 个单独的控制器。像这样:
@RestController
@RequestMapping(path = "/routes")
public class RoutesController {
@Autowired
RoutesDao routesDao;
@GetMapping(value = {"/getRoutes/", "/getRoutes"})
public ArrayList<Route> getRoutes() { ... } }
控制器运营商也是如此。它是否正确?如果不正确,正确的方法是什么?
抱歉 样式问题,这是我在 Whosebug 上的第一个问题 :)
每个实体都有一个 DAO 是正确的。
使用 JPA 存储库时,您别无选择,只能提供实体。例如:
public interface FooRepository extends JpaRepository<Foo,Long>{}
对于 REST 控制器也是如此,您必须像您一样按对象将功能组合在一起。
您可以改进您的映射,使其更加 RESTful。要检索所有路由,请不要指定路径:
@GetMapping
public ArrayList<RouteResource> getRoutes() { ... }
(我从来没有使用过@GetMapping,但它应该是这样工作的)
如果你想要特定路线:
@GetMapping("/get/{id}")
public RouteResource getRoute() {...}
您应该 return 资源而不是客户端的实体。
我建议创建标有 @Service
注释的服务(即 CarrierService
接口和 CarrierServiceImpl
实现)。比将它们注入控制器。在服务中使用存储库,因为一些数据库操作需要事务,而管理事务的更好地方是服务。服务还可以做更专业的工作,这将需要访问多个存储库,以便您可以注入它们。并且不要忘记使用 @Transactional
注释来标记您的服务。
在处理涉及从数据库请求多种数据类型的项目时,我遇到了以下问题:
假设我有 2 个 java classes 对应于数据库实体:
路线
public class Route {
public Route(int n, int region, Date fdate, boolean changed, int points,
int length) {
super();
this.n = n;
this.region = region;
this.fdate = fdate;
this.changed = changed;
this.points = points;
this.length = length;
}
}
运营商
public class Carrier {
public Carrier(...) {
this.id = src.getId();
this.name = src.getName();
this.instId = src.getInstId();
this.depotId = src.getDepotId();
}
如果是这样,创建 Dao 接口和 classes 的正确方法是什么?我是这样做的 -
@Repository
public class CarrierDaoImpl implements CarrierDao{
@Autowired
DataSource dataSource;
public List<Carrier> getAllOrgs() { ... }
}
@Repository
public class RoutesDaoImpl implements RoutesDao {
@Autowired
DataSource dataSource;
public ArrayList<AtmRouteItem> getRoutes(AtmRouteFilter filter) { ... }
}
我正在为每个 java class item\db 实体创建一个@Repository DAO,然后为有关承运人和路线的请求创建 2 个单独的控制器。像这样:
@RestController
@RequestMapping(path = "/routes")
public class RoutesController {
@Autowired
RoutesDao routesDao;
@GetMapping(value = {"/getRoutes/", "/getRoutes"})
public ArrayList<Route> getRoutes() { ... } }
控制器运营商也是如此。它是否正确?如果不正确,正确的方法是什么?
抱歉 样式问题,这是我在 Whosebug 上的第一个问题 :)
每个实体都有一个 DAO 是正确的。 使用 JPA 存储库时,您别无选择,只能提供实体。例如:
public interface FooRepository extends JpaRepository<Foo,Long>{}
对于 REST 控制器也是如此,您必须像您一样按对象将功能组合在一起。
您可以改进您的映射,使其更加 RESTful。要检索所有路由,请不要指定路径:
@GetMapping
public ArrayList<RouteResource> getRoutes() { ... }
(我从来没有使用过@GetMapping,但它应该是这样工作的)
如果你想要特定路线:
@GetMapping("/get/{id}")
public RouteResource getRoute() {...}
您应该 return 资源而不是客户端的实体。
我建议创建标有 @Service
注释的服务(即 CarrierService
接口和 CarrierServiceImpl
实现)。比将它们注入控制器。在服务中使用存储库,因为一些数据库操作需要事务,而管理事务的更好地方是服务。服务还可以做更专业的工作,这将需要访问多个存储库,以便您可以注入它们。并且不要忘记使用 @Transactional
注释来标记您的服务。