java - 模型关联太多,如何分离做到单一化
天蓬老师
天蓬老师 2017-04-18 09:27:17
0
2
283
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
大家讲道理

Finally I can complain and express my opinions for those who have stopped using ORM for a long time.

I often see people who write Java the backend write a lot of model objects, and various inheritance and combinations are used, as @chanjarster said, one-to-one, one-to-many, many-to-many, single item and two-way , I was immediately confused...

In fact, I personally think ORM 是一个很糟糕的发明,ORM 唯一的好处就是不用考虑 SQL,简直哭晕,SQL 是一个多么好的东西,现在对于大部分的 Java 程序员来说,写个 SQL is a very bad invention. The only advantage of ORM is that you don’t need to consider SQL. I just cry. SQL is such a good thing. Now for everyone For some

programmers, writing SQL shouldn’t be a big problem. It’s much simpler than memorizing various object mapping rules here...

ORMSummarize the shortcomings

  1. The performance is not high. Even with a mature framework, sometimes I only need to access a property and still load the entire object's data. (PS: It may be configurable, but the learning cost is also high)

  2. Not flexible enough. Many business needs require table lookups, function calls, stored procedures...

  3. Abstraction is too cumbersome and I get confused

To sum up, if you agree with it, then give up ORM

Alternatives:

  1. Use MyBaits 直接写 SQL for flexible control

  2. Use NOSQL 数据库,把对象转换为 JSON 数据,可以直接存储,如 MongoDb

大家讲道理

Your question is so big. To put it simply, it is to figure out the relationship between Model and Model, which has three dimensions

  1. "Reference relationship" or "Owning relationship". If A owns B, then when A is deleted, B will also be deleted. If A refers to B, then if A deletes B, it does not need to be deleted. This depends on the business.

  2. Single or two-way. This depends on the business, you can make your own judgment, but remember one thing, under the "reference relationship", try to keep it one-way. Under "owning relationships", bidirectionality is OK.

  3. One-to-one, one-to-many, many-to-one, many-to-many. I won’t go into details about this, but there is one thing to note: try to avoid many-to-many. If you use many-to-many, ask yourself whether you must do this.

If you must ask about the methodology, you can look at the Aggregate Root concept in DDD (Domain Driven Design).

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!