Hibernate configuration and simple use, hibernate Configuration
I. Concepts
Hibernate is an open-source object relationship ing (ORM) framework that encapsulates JDBC objects in a very lightweight manner, so that Java programmers can abandon programming SQL statements in programs, use the object programming thinking to manipulate the database as you like.
2. Preparations
Install the hibernatgatels plug-in Eclipse: Help-> Install New Software. New-> If the Hibernate option is available in the dialog box opened by Other, the installation is successful.
3. Create a project
1. A New Java Project
2. Import the jar package for the Java project:
-
- Hibernate core jar package
- MySQL jdbc driver jar package
- Junit4 jar package
In windows> preferences> java> build path-user libraries, we add the three to three user class libraries for future import:
Right-click Java project-> properties-> Java Build Path-> Library-> add Library-> User Library-> next-> check all the created User class libraries-> finish-> OK.
3. Create the Hibernate configuration document and edit it:
Right-click src under the project-> new-> other-> Hibernate Configuration File (cfg. xml)-> next, next, finish
The configuration file hibernate. cfg. xml has been generated under src. Open
<Hibernate-configuration> <session-factory name = ""> <property name = "connection. driver_class "> com. mysql. jdbc. driver </property> // mysql jdbc Driver <property name = "connection. url "> jdbc: mysql: // hibernate? UseUnicode = true & amp; characterEncoding = UTF-8 </property> // specifies the url of the database <property name = "connection. username "> root </property> // mysql username <property name =" connection. password "> ***** </property> // password <property name =" dialect "> org. hibernate. dialect. mySQLDialect </property> // dialect <property name = "show_ SQL"> true </property> <property name = "format_ SQL"> true </property> <property name = "hbm2ddl. auto "> create </property> <mapping resource =" Students. hbm. xml "/> // ing file </session-factory>
4. Create a persistence class
Suppose we want to store a student table in the database, including the fields such as id, name, gender, birthday, and address. Then we can create a Students class that contains these attributes of Students. We can indirectly manipulate the database by "modifying the attributes of Students objects and passing them to the Database, this avoids the use of SQL statements. The Students class is called persistence class.
Create a class under src and name it Students. The design principles of persistence classes should follow the design principles of JavaBean:
-
- Public class
- Private attributes
- Public constructor without Parameters
- Property setter/getter Encapsulation
As follows:
public class Students { private int sid; private String sname; private String gender; private Date brithday; private String address; public Students() { } public Students(int sid, String sname, String gender, Date brithday, String address) { this.sid = sid; this.sname = sname; this.gender = gender; this.brithday = brithday; this.address = address; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getBrithday() { return brithday; } public void setBrithday(Date brithday) { this.brithday = brithday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", brithday=" + brithday + ", address=" + address + "]"; }}Students Class
5. Create an object-link ing File
Right-click src-> new-> other-> Hibernate-> Hibernater XML Mapping File (hbm. xml)-> next, next-> click the class to be persisted-> finish
After the object-link ing file is created, you can see that the object class ing file is called a table in the database.
Students. hbm. xml
After creating the ing file, remember to add the mapping tag in the hibernate configuration file:
<mapping resource="Students.hbm.xml" />
6. Create a database in Mysql. The name is the name of the database configured in the configuration document.
7. Write the database access code through the Hibernate API,Use Junit for testing.
Three Annotations:
-
- @ Test: Test Method
- @ Before: Initialization Method
- @ After: release resources
Right-click the project name-> new-> source folder-> folder name to test-> finish
Create a new class in the test folder named StudentsTest:
1 import java. util. date; 2 import org. hibernate. session; 3 import org. hibernate. sessionFactory; 4 import org. hibernate. transaction; 5 import org. hibernate. cfg. configuration; 6 import org. hibernate. service. serviceRegistry; 7 import org. hibernate. service. serviceRegistryBuilder; 8 import org. junit. after; 9 import org. junit. before; 10 import org. junit. test; 11 12 // Test class 13 public class StudentsTest {14 15 private SessionFactory sessionFactory; 16 private Session session; 17 private Transaction transaction; 18 19 @ Before20 public void init () {21 // create a Configuration object 22 Configuration config = new Configuration (). configure (); 23 // create the service registration object 24 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder (). applySettings (config. getProperties () 25. buildServiceRegistry (); 26 // create a session factory object 27 sessionFactory = config. buildSessionFactory (serviceRegistry); 28 // session Object 29 session = sessionFactory. openSession (); 30 // start transaction 31 transaction = session. beginTransaction (); 32 33} 34 35 @ After36 public void destroy () {37 transaction. commit (); // submit the transaction 38 session. close (); // close session 39 sessionFactory. close (); // close session factory 40} 41 42 @ Test43 public void testSaveStudents () {44 // generate student object 45 Students s = new Students (1, "John ", "male", new Date (), "Beijing"); 46 // Save the object and enter database 47 session. save (s); 48} 49}
After the Test class is created, right-click the testSaveStudents () method in the directory under the class and Run as Junit Test!
Then we can check in the corresponding database that a students table has been generated: