Build a simple Hibernate project through myclipse (PS: Implement data insertion into the table in unit tests)
Let's first look at the composition of the project (PS: In fact, when building Hibernate, we need to write 3 things: ①Entity class ②Entity class.hbm.xml③hibernate.cfg.xml):
step
Ⅰ. Create a new web project;
Ⅱ. Right-click the project and select myeclipse->Project Facets->install hibernate facet (PS: click finish if there is no necessary requirement for jdk), and Jiangzi will configure the basic packages required by hibernate;
Ⅲ. Copy the tool class HibernateUtil into your project
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.zp.hibernate.domain; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 import org.hibernate.service.ServiceRegistry; 7 import org.hibernate.service.ServiceRegistryBuilder; 8 9 public class HibernateUtil { // 涓撻棬璐熻矗鑾峰彇session骞跺皢鍏跺叧闂� 10 11 private static SessionFactory sessionFactory; 12 private static ThreadLocal session = new ThreadLocal(); 13 14 private HibernateUtil() { 15 } 16 17 static { 18 Configuration configuration = new Configuration().configure(); 19 ServiceRegistryBuilder regbuilder = new ServiceRegistryBuilder() 20 .applySettings(configuration.getProperties()); //姣旇緝鏃╁寘鐢ㄧ殑鏄疭erviceRegistryBuilder 21 // StandardServiceRegistryBuilder 鍦ㄤ互鍚庡氨娌$敤浜� 22 ServiceRegistry serviceRegistry = regbuilder.buildServiceRegistry(); 23 sessionFactory = configuration.buildSessionFactory(serviceRegistry); 24 } 25 public static Session getThreadLocalSession() { 26 Session s = (Session) session.get(); 27 if (s == null) { 28 s = sessionFactory.openSession(); 29 session.set(s); 30 } 31 return s; 32 } 33 public static void closeSession() { 34 Session s = (Session) session.get(); 35 if (s != null) { 36 s.close(); 37 session.set(null); 38 } 39 } 40 }
Ⅳ. Complete the writing of the entity class, and fill in the attributes and the corresponding get and set methods (student.java)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.zp.hibernate.domain; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 @Entity // Entity class, which is mapped to the database here is Table 9 @Table 10 public class student { 11 private Integer id; 12 private String name; 13 @Id // Set the variable id as the primary key 14 @GeneratedValue // 15 public Integer getId() { 16 return id ; 17 } 18 public void setId(Integer id) { 19 this .id = id; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this .name = name; 26 } 27 }
Ⅴ. Create a new XXX.hbm.xml (student.hbm.xml) (PS: XXX must be consistent with the class name of the entity class)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate- mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping- 3.0.dtd"> 5 <!-- package : Specify the full package name of the persistent class corresponding to the current mapping file --> 6 <hibernate-mapping package ="com.zp.hibernate.domain"> 7 <!-- class The name in the name is the class name of the corresponding entity class --> 8 < class name="student"> 9 <!-- The name in the id is the variable in the corresponding entity class --> 10 <id name="id"> 11 <!-- The name in the column is the column name in the generated database --> 12 <column name="student_id"></column> 13 <!-- The class of the generator specifies native, indicating that Hibernate uses one of identity, hilo, and sequence as the primary key generation method based on the underlying database's own judgment --> 14 <generator class = "native" /> 15 </id> 16 <!-- property is used to get the name of a variable that is not a primary key, and should be consistent with the entity class --> 17 <property name="name"> 18 <column name=" student_name"></column> 19 </property> 20 </ class > 21 22 </hibernate-mapping>
Ⅵ, modify hibernate.cfg.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 <!-- Generated by MyEclipse Hibernate Tools. --> 6 <hibernate-configuration> 7 8 <session-factory> 9 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 10 <!-- 所用数据库 --> 11 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 12 <!-- Database url --> 13 <property name="connection.url">jdbc:mysql: // localhost:3306/hibernate</property> 14 <!-- Automatic table creation --> 15 <property name ="hibernate.hbm2ddl.auto">create</property> 16 <!-- hibernate.connection.username : username to connect to the database --> 17 <property name="hibernate.connection.username">root</property > 18 <!-- hibernate.connection.password : Password for connecting to the database --> 19 <property name="hibernate.connection.password">****</property> 20 <!-- Mapping the entity class file is loaded --> 21 <mapping resource="com/zp/hibernate/domain/student.hbm.xml"/> 22 </session-factory> 23 24 </hibernate-configuration>
VII. Writing test class (test.java)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.zp.hibernate.test; 2 3 import static org.junit.Assert.*; 4 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 import org.junit.Test; 8 9 import com.zp.hibernate.domain.HibernateUtil; 10 import com.zp.hibernate.domain.student; 11 12 public class test { 13 14 @Test 15 public void test() { 16 student stu = new student(); 17 stu.setId(1); 18 stu.setName("getcharzp"); 19 20 Session session = HibernateUtil.getThreadLocalSession(); 21 Transaction tx = session.beginTransaction(); 22 session.save(stu); 23 tx.commit(); 24 HibernateUtil.closeSession(); 25 } 26 27 }
Summarize:
The basic configuration of hibernate can be imported directly through the install.. of myeclipse, and the rest only need us to modify or add the corresponding ①, entity class ②, XXX.hbm.xml ③, hibernate.cfg.xml