Code analysis of accessing JSON data with hibernate

1、 Scene

Some preferential information will be stored in the order to facilitate the display of the page, such as:

1. 50 minus 100

2. You only need to pay 200.00 RMB to participate in the "old member's true feedback - quality course experience activity"

3. The [rush purchase] activity of the [Oracle + PL / SQL actual combat] package course, with a discount of 120.00 academic coins... And so on

As shown in the figure, we show users the preferential information they participate in on the page:

2、 Analysis

The above preferential information has the following characteristics:

1. It is only for display and will not involve modification;

2. Once the order is paid successfully, it will not change;

3. The amount of data will not be large.

3、 Solution

1. The simplest solution is to associate tables:

However, this solution needs to query with tables. It feels unnecessary. After all, it just displays data. Using associated tables is a bit like killing chickens with a bull's knife.

2. JSON solution:

Through the above ideas, we can solve many similar problems.

3. Code example:

1. Model class:

Java code

① We use our custom hibernate type for conversion, and there is only part of the above code

2. Custom jsontype

Java code

The JSON framework uses JSON lib 2.1.

3. Custom jsonlist

Java code

That's it. Welcome to discuss it.

Someone said there was a performance problem. I wrote a test case:

Test machine: CPU: P8700 (dual core @ 2.53GHz) memory: 2G

1、 Insert

1. Insert 10W entries in JSON mode

2. Insert 10W entries in association table

JSON performance is much better than associated tables, which need to insert two tables.

2、 Inquiry

1. Query 10W items in JSON paging (100 items per page)

select 100000 elapsed time(millis):146047

2. Associated table paging (100 entries per page) query 10W entries

select 100000 elapsed time(millis):275375

JSON performance is much better than that of the associated table, which needs to join the linked table query.

Disadvantages of JSON: queries such as analysis and statistics are chicken ribs, and large amount of data is chicken ribs (the amount of data stored in a column cannot be too large).

My application scenario: preferential information, shopping cart persistence (up to 50 shopping carts per user).

summary

The above is the code analysis of Hibernate accessing JSON data introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>