Basic Hibernate Case 1: Basic hibernate case

Source: Internet
Author: User

Basic Hibernate Case 1: Basic hibernate case

The MySQL database is used.

 

1. Introduce the jar package in the project and add references.

<Dependencies> <dependency> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 4.3 </version> <scope> test </scope> </ dependency> <groupId> anlr </groupId> <artifactId> anlr </artifactId> <version> 2.7.7 </version> </dependency> <groupId> commons -collections </groupId> <artifactId> commons-collections </artifactId> <version> 3.2.1 </version> </dependency> <gr OupId> commons-logging </groupId> <artifactId> commons-logging </artifactId> <version> 1.1.1 </version> </dependency> <groupId> dom4j </ groupId> <artifactId> dom4j </artifactId> <version> 1.6.1 </version> </dependency> <groupId> org. hibernate </groupId> <artifactId> hibernate-core </artifactId> <version> 5.2.10.Final </version> </dependency> <groupId> org. hibernate. javax. persis Tence </groupId> <artifactId> hibernate-jpa-2.0-api </artifactId> <version> 1.0.1.Final </version> </dependency> <groupId> schemsist </groupId> <artifactId> extends sIST </artifactId> <version> 3.12.1.GA </version> </dependency> <groupId> javax. transaction </group ID> <artifactId> jta </artifactId> <version> 1.1 </version> </dependency> <! -- Https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId> log4j </groupId> <artifactId> log4j </artifactId> <version> 1.2.17 </version> </dependency> <groupId> org. slf4j </groupId> <artifactId> slf4j-api </artifactId> <version> 1.6.1 </version> </dependency> <groupId> org. slf4j </groupId> <artifactId> slf4j-log4j12 </artifactId> <version> 1.7.5 </version> <scope> test </sco Pe> </dependency> <! -- Mysql --> <dependency> <groupId> org. wisdom-framework </groupId> <artifactId> mysql-connector-java </artifactId> <version> 5.1.34 _ 1 </version> </dependency> </dependencies>View Code

When referencing a jar package, do not reference the Java ee-api jar first, and an error will be reported.

2. Write the student's entity class

// Student ID private Integer sid; // name private String name; // age private Integer age;View Code

3. Hibernate. cfg. xml configuration (cfg indicates configuration)

1 <? Xml version = '1. 0' encoding = 'utf-8'?> 2 <! DOCTYPE hibernate-configuration PUBLIC 3 "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 4. Student. hbm. xml

1 <? Xml version = '1. 0' encoding = 'utf-8'?> 2 3 <! DOCTYPE hibernate-mapping PUBLIC 4 "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 5 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 6 5. Test class for saving data to the database using Hibernate

1 @ Test 2 public void testhibernate () {3 4 // 01 prepare a Student object 5 Student stu = new Student (); 6 stu. setSid (3); 7 stu. setName ("white"); 8 stu. setAge (44); 9 10 // 02 Hibernate save 11 // read the large Configuration file and obtain the connected database information 12 Configuration cfg = new Configuration (). configure (); 13 14 // 3 create SessionFactory15 SessionFactory factory = cfg. buildSessionFactory (); 16 17 // process session18 Session session = factory. openSession (); 19 20 // start Transaction 21 Transaction tx = session. beginTransaction (); 22 23 // 5Hibernate24 25 session. save (stu); 26 27 tx. commit (); 28 System. out. println ("save OK"); 29}View Code

6. Implementation

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.