Hibernate connects to the mysql database and automatically creates tables. hibernatemysql
The first step of the Day of the day, the diapers of the birds, the first step of Hibernate, connect to the database.
Hibernate is an open-source object relationship ing framework that encapsulates JDBC very lightweight objects. It establishes ing between POJO and database tables and is a fully automated orm framework, hibernate can automatically generate and execute SQL statements, so that Java programmers can use the object programming thinking to manipulate the database as they wish. So Hibernate simply simplifies the operation with the database, so you have to connect to the database if you want to play with it. (The orm framework corresponding to Hibernate also has Mybatis, but Mybatis is semi-automated, but I personally feel like Mybatis, more free and more scalable ).
Today, we will share with you how Hibernate connects to the mysql database and automatically creates tables. The connection to other databases is actually the same. You will learn the opposite.
Step 1: Create a java project without any problems, right? New, Other..., enter the java Project,
Click Next, enter the project name, select the JDK version to use, and click Finish,
Such a java project has been created.
Step 2: Create a lib directory in the root directory of the project to store the jar package required by the project, here we need to use the core jar package of Hibernate, the jar package of mysql jdbc, And the jar package of Junit. Here we will use Junit for testing (Buddy, I don't need to teach how to get these packages? If you want to, you should give up this industry. As a professional IT hacker, you must learn to get resources and use resources from the grass-roots level ),
Then, introduce these jar packages in the project, right-click the project, and select Properties. The window is displayed:
Click Java Build Path, Libraries, and Add JARs:
Select all the jar packages under the lib directory and click OK,
Step 3: Create a Hibernate configuration file, right-click src and choose New Other... enter Hibernate, pop up the content related to the Hibernate Tools plug-in, select the configuration file, and click Next,
Click Next, Next, and Finish.
Step 4: Create a package according to the package naming rules under the src path, and create an entity class under the entity package. Here, I create a Student class, and then randomly add several fields, encapsulate it with getter/setter,
Step 5: Create a ing file for the object class, New, Other... enter Hibernate to bring up content related to the Hibernate Tools plug-in, select the ing file, and click Next,
In the pop-up window, click Add Class:
Select the object class to map and click OK,
Next, Next, or Finish. Needless to say, the ing file is created successfully. Hibernate will automatically write the ing file,
Step 6: perform detailed configuration of the Hibernate configuration file, including the configuration required to connect to the database, whether to output SQL statements, and import the ing file. (Please understand the meaning of each configuration, cultivate individual learning ability)
Common Hibernate configurations:
Step 7: Create a database. In the configuration file, I use the hibernate database. Remember to open the mysql service and create a hibernate database using a visual tool,
Last step: Use Junit for testing, create a test package, and create a Junit test class under the package,
Then, tap the corresponding code:
1 package com. alanlee. test; 2 3 import java. util. date; 4 5 import org. hibernate. session; 6 import org. hibernate. sessionFactory; 7 import org. hibernate. transaction; 8 import org. hibernate. cfg. configuration; 9 import org. hibernate. service. serviceRegistry; 10 import org. hibernate. service. serviceRegistryBuilder; 11 import org. junit. after; 12 import org. junit. before; 13 import org. junit. test; 14 15 import com. alanlee. entity. student; 16 17/** 18 * test class 19 * @ author AlanLee20 * 21 */22 public class StudentTest23 {24/** 25 * session factory 26 */27 private SessionFactory sessionFactory; 28/** 29 * Session Object 30 */31 private session Session; 32/** 33 * Transaction object 34 */35 private transaction Transaction; 36 37/** 38 * initialize 39 * @ throws Exception40 */41 @ Before42 public void setUp () throws Exception43 {44 // create a Configuration tin 45 Configuration config = new Configuration (). configure (); 46 // create the service registration object 47 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder (). applySettings (config. getProperties () 48. buildServiceRegistry (); 49 // create a session factory object 50 sessionFactory = config. buildSessionFactory (serviceRegistry); 51 // create a session object 52 session = sessionFactory. openSession (); 53 // start transaction 54 transaction = session. beginTransaction (); 55} 56 57/** 58 * release resource 59 * @ throws Exception60 */61 @ After62 public void tearDown () throws Exception63 {64 // submit the transaction 65 transaction. commit (); 66 // close session 67 session. close (); 68 // close session factory 69 sessionFactory. close (); 70} 71 72/** 73 * test method 74 */75 @ Test76 public void testSaveStudent () 77 {78 // generate Student object 79 student Student = new student (); 80 Student. setSid (1); 81 student. setName ("AlanLee"); 82 student. setSex ("male"); 83 student. setBirthday (new Date (); 84 student. setAddress ("Chenzhou, Hunan"); 85 // Save the object to enter database 86 session. save (student); 87} 88 89}
Run this class through Junit for testing. The test is successful,
Then let's take a look at the database and refresh the table,
In this way, it is perfect to connect to the mysql database through Hibernate and automatically create tables.
Alan has written a essay about installing Hibernate Tools in eclipse. If I don't understand Hibernate Tools, I will show it by myself. Alan is trying to use pictures to show it, it's hard to turn technology into something in your mind.
My parents are urging me to have dinner again.