spring mvc 建立多对一关系
我正在尝试学习一些jsp以及spring框架。
我的应用程序有 2 个 sql 表。 “用户”和“位置”
每个位置都属于一个用户。位置表有一个引用用户 ID 的外键。
我想实现我的应用程序,以便给定一个用户(即“user1”),我可以调用 user1.getLocations() 来检索与该用户关联的位置列表,但我不知道在哪里实现它。 (在 User 类中,在 UserDao 上,在 UserManager 上,在应该列出所有用户及其用户的页面的控制器上,等等?)?
- - 编辑: 在控制器上:
List<User> users = userManager.getUsers();
for(User user:users) {
user.setLocations(locationManager.getLocations(user));
}
myModel.put("users", users);
return new ModelAndView("location", "model", myModel);
}
这是传统的解决方案吗? locationManager.getLocations(user) 返回与用户具有相同 id 的位置列表。
im trying to learn some jsp as well as the spring framework.
My application has 2 sql tables. 'User' and 'Locations'
each Location belongs to a User. The location table has a foreign key referencing the id of the user.
I want to implement my application such that given a user (i.e 'user1') i can call user1.getLocations() to retrieve a list of the locations associated with that user, but i do not know where to implement this. (in the User class, on the UserDao, on the UserManager, on the controller for the page that should list all users and their , etc?)??
----edit:
On the controller:
List<User> users = userManager.getUsers();
for(User user:users) {
user.setLocations(locationManager.getLocations(user));
}
myModel.put("users", users);
return new ModelAndView("location", "model", myModel);
}
is this a conventional solution? locationManager.getLocations(user) returns a List of Locations with the same id as user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最有可能在 UserDao 类中,因为它听起来像数据库操作。此外,该方法应该更像这个签名 userService.getLocations(userId) 而不是 user1.getLocation()、user2.getLocation(). 等。
您可以得到MVC 模式的快速概述,学习有关 Spring MVC 和 教程。
您肯定需要将方法调用从视图链接到 dao。所以传统上是这样完成的:来自视图的请求(MVC中的V)->控制器调度(MVC中的C)->服务类(MVC中M的一部分)->dao类(也是MVC中M的一部分) MVC)并通过翻转方向发送回视图。
Most likely in the UserDao class since it sounds like a DB operation. Also the method should be more like this signature userService.getLocations(userId) as opposed to user1.getLocation(), user2.getLocation()., etc.
You can get a quick overview of the MVC pattern, learn more about the Spring MVC and a tuotrial.
You will definitely need to chain your method calls to the dao from the view. So traditionally it's done this way: a request from the view (V in MVC)->controller dispatches (C in MVC)->service class (part of M in MVC)->dao class (also part of M in MVC) and sent back to view by flipping the directions.
我不会创建一个单独的 DAO 方法来处理这个问题,“user1.getLocations”是正确的方法。休眠等。支持延迟加载和将表关系映射为实体之间的集合(如列表和集合)之类的内容,这些内容适用于此类情况。这是一个简单的示例,它可能无法开箱即用,具体取决于表的命名方式等,但您会明白这个想法:
请注意,您可以从用户端获取位置,即使数据库中的关系仅是从位置到用户(用户的 @OneToMany 中的“mappedBy”属性实际上告诉“引用的另一侧”(位置)上的哪个字段指向该用户)。
我建议查找一些 JPA 和/或 Hibernate with Spring 教程以获取更深入的信息。
I wouldn't create a separate DAO-method for handling this, the "user1.getLocations" is the right approach. Hibernate et al. support stuff like lazy-loading and mapping of table relations as Collections (like Lists and Sets) between entities, which are meant for these kind of situations. Here's a simple example,which might not work out-of-the-box, depending how your tables are named etc., but you'll get the idea:
Note that you can get the locations from the User-side, even though the relation in the database is only from Location to User (the "mappedBy"-attribute in Users' @OneToMany actually tells which field on the "other side of the reference" (Location) points to this User).
I'd suggest looking up some JPA and/or Hibernate with Spring -tutorials for more in-depth information.