简体   繁体   English

在mySQL数据库中存储ArrayList对象数据

[英]Storing ArrayList object data in mySQL Database

I am creating a web application that has a User object and each User object has an ArrayList of objects (Food) that need to be stored into a table in a mySQL database. 我正在创建一个具有User对象的Web应用程序,每个User对象都有一个对象的ArrayList(Food),需要存储到mySQL数据库的表中。 I want to store the entire ArrayList into a single column. 我想将整个ArrayList存储到一个列中。 Would it be better to store it as a Json? 将它存储为Json会更好吗? Or should I just create a table for each User that stores the individual items of the ArrayList? 或者我应该为每个存储ArrayList的各个项的用户创建一个表? The only problem I have is that the data would edited quite frequently. 我唯一的问题是数据会经常编辑。

EDIT: I have tables for both Users and Food. 编辑:我有用户和食物的表。 The idea is that Users add from the Foods from the Food table to their ArrayList and then I want to store that ArrayList in something. 想法是用户从Food表中的Foods添加到他们的ArrayList,然后我想将ArrayList存储在某个东西中。

public class User{
  private int id;
  private String username;
  private List<Food> FoodList = new ArrayList<Food>();
}
public class Food{
  private int id;
  private String name;
  private double protein;
  private int calories;
  ...
}
  1. As @ayrton mentions, creating (at least) two tables is a good idea (one for Users and one for Foods). 正如@ayrton所提到的,创建(至少)两个表是一个好主意(一个用于用户,一个用于Foods)。
  2. Definitely don't create a table per User 绝对不要为每个用户创建一个表
  3. As @NeplatnyUdaj mentioned, a join table (where you map Foods to Users) might be warranted (or it could be overkill, depending on your needs). 正如@NeplatnyUdaj所提到的,可能需要一个连接表(将Foods映射到用户的位置)(或者根据您的需要可能有些过大)。
  4. Given the details you've described, JSON is almost certainly not the best approach (this isn't true in all situations, but if you were in such a situation, I'd probably suggest you consider a different type of database). 鉴于您所描述的细节,JSON几乎肯定不是最好的方法(在所有情况下都不是这样,但如果您遇到这种情况,我可能会建议您考虑使用不同类型的数据库)。

(Welcome to SO, btw!) (欢迎来到SO,顺便问一下!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM