Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between Hibernate and MyBatis

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the differences between Hibernate and MyBatis". In daily operation, I believe many people have doubts about the differences between Hibernate and MyBatis. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the differences between Hibernate and MyBatis?" Next, please follow the editor to study!

Test target

The following tests need to determine a few things:

Scenarios with different performance

Performance difference ratio in the same scene

Find out the advantages and disadvantages of each frame, the performance in various situations, and the applicable scene.

Test idea

The overall test is divided into: single table insert, association insert, single table query, multi-table query.

The test is divided into two rounds, one round of default parameters in the same scenario, one round of tuning and strength, and a comparative analysis of horizontal and vertical.

Try to ensure the consistency of input and output in the test.

The sample size is as large as possible, reaching more than 100000 levels to reduce statistical errors.

Test outline

Under the specific situation

Insert test 1: 100000 records are inserted.

In the query test 1: 1 million data, a single table was queried 100000 times through id, and there were no associated fields.

Query test 2: 1 million data in a single table through id query 100000 times, output associated object fields.

Query test 3: 1 million * 500000 query 100000 times in the associated data, both output the same field.

Prepare for

Database: mysql 5.6

Table design:

Twitter: Twitter

CREATE TABLE `twitter` (`id` bigint (20) NOT NULL AUTO_INCREMENT, `add_ date` datetime DEFAULT NULL, `modify_ date` datetime DEFAULT NULL, `ctx` varchar (255) NOT NULL, `add_user_ id` bigint (20) DEFAULT NULL, `modify_user_ id` bigint (20) DEFAULT NULL, PRIMARY KEY (`id`), KEY `UPDATE_USER_ FORI` (`modify_user_ id`), KEY `ADD_USER_ FORI` (`add_user_ id`) CONSTRAINT `ADD_USER_ FORI` FOREIGN KEY (`add_user_ id`) REFERENCES `user` (`id`) ON DELETE SET NULL, CONSTRAINT `UPDATE_USER_ FORI` FOREIGN KEY (`modify_user_ id`) REFERENCES `user` (`id`) ON DELETE SET NULL) ENGINE=InnoDB AUTO_INCREMENT=1048561 DEFAULT CHARSET=utf8

User: user

CREATE TABLE `user` (`id` bigint (20) NOT NULL AUTO_INCREMENT, `name` varchar (255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=524281 DEFAULT CHARSET=utf8

Test data preparation:

Table 1: twitter

No data.

Table 2: user

500000 random user names.

Random content Twitter table (material_twitter)

No id, only random string content, a total of 100000.

It is used to insert the Twitter table.

Generate data code that associates 100 users:

Insert into twitter (ctx,add_user_id,modify_user_id,add_date,modify_date) SELECT name,ROUND (RAND () * 100) + 1 from MATERIAL (RAND () * 100) + 1 from MATERIAL

Generate data code that associates 500000 users:

Insert into twitter (ctx,add_user_id,modify_user_id,add_date,modify_date) SELECT name,ROUND (RAND () * 500000) + 1 from MATERIAL (RAND () * 500000) + 1 from MATERIAL

Entity code

Entity @ Table (name = "twitter") public class Twitter implements java.io.Serializable {private Long id; private Date add_date; private Date modify_date; private String ctx; private User add_user; private User modify_user; private String createUserName; @ Id @ GeneratedValue (strategy = IDENTITY) @ Column (name = "id", unique = true, nullable = false) public Long getId () {return id } public void setId (Long id) {this.id = id;} @ Temporal (TemporalType.DATE) @ Column (name = "add_date") public Date getAddDate () {return add_date;} public void setAddDate (Date add_date) {this.add_date = add_date } @ Temporal (TemporalType.DATE) @ Column (name = "modify_date") public Date getModifyDate () {return modify_date;} public void setModifyDate (Date modify_date) {this.modify_date = modify_date;} @ Column (name = "ctx") public String getCtx () {return ctx;} public void setCtx (String ctx) {this.ctx = ctx } @ ManyToOne (fetch = FetchType.LAZY) @ JoinColumn (name = "add_user_id") public User getAddUser () {return add_user;} public void setAddUser (User add_user) {this.add_user = add_user;} @ ManyToOne (fetch = FetchType.LAZY) @ JoinColumn (name = "modify_user_id") public User getModifyUser () {return modify_user } public void setModifyUser (User modify_user) {this.modify_user = modify_user;} @ Transient public String getCreateUserName () {return createUserName;} public void setCreateUserName (String createUserName) {this.createUserName = createUserName;}}

Start

Insert Test 1

Code operation:

Load the data of the random content Twitter table into memory, and then add a total of 100000 items to the Twitter table.

Key code:

Hibernate:

Session session = factory.openSession (); session.beginTransaction (); Twitter t = null; Date now = new Date (); for (String materialTwitter: materialTwitters) {/ / System.out.println ("materialTwitter=" + materialTwitter); t = new Twitter (); t.setCtx (materialTwitter); t.setAddDate (now); t.setModifyDate (now); t.setAddUser (null) T.setModifyUser (null); session.save (t);} session.getTransaction (). Commit ()

Mybatis:

Twitter t = null; Date now = new Date (); for (String materialTwitter: materialTwitters) {/ / System.out.println ("materialTwitter=" + materialTwitter); t = new Twitter (); t.setCtx (materialTwitter); t.setAddDate (now); t.setModifyDate (now); t.setAddUser (null); t.setModifyUser (null) Msession.insert ("insertTwitter", t)} msession.commit ()

TwitterMapper.xml, insert code snippet:

Insert into twitter (ctx, add_date,modify_date) values (# {ctx}, # {add_date}, # {modify_date})

Query Test 1

Query Twitter content from 1 to 100000 through id, and output only Weibo content.

Key code:

Hibernate:

Long cnt = 1000000; for (long I = 1; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report