First, I build a new project. I use the latest version 5.3.2. It will take a while to download the jar package. Then I choose my own project name. I'll call it demo here.
I chose to build it in maven mode, right-click on the project, and select Add Framework Support.
Choose Maven laterThen pop up a prompt box in the lower right corner of the project and select Auto Import.Then add it under the pom.xml file
<dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.3</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.activation/activation --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0.1</version> </dependency> </dependencies>
Then in resources, create a new xml file called hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database Connection Configuration --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">1</property> <!-- dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Console display sql --> <property name="show_sql">true</property> <!-- Automatic update of table structure --> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
Then in the upper right corner of the interfaceClick on the pop-up prompt box to make the file hibernate configuration file. Click on the project structure button to see the configuration file you just added.
Or under the resources folder, create a new xml file called Student.hbn.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="mode"> <class name="mode.Student" table="t_student"> <id name="id" column="stuId" > <generator class="native" /> </id> <property name="name" /> </class> </hibernate-mapping>
First go to the database and create a new hibernate database. I'll call it hibernate directly. First, I'll create two new packages, one called model and the other called util.
Then create a new entity class named Student under the model package, and generate get, set, and toString methods
public class Student { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
Create a new tool class under the util package called HibernateUtil
public class HibernateUtil { private static final SessionFactory SESSION_FACTORY = buildSessionFactory(); private static SessionFactory buildSessionFactory(){ //Creating Service Registration Objects ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); //Creating Session Factory Objects return new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory(); } public static SessionFactory getSessionFactory(){ return SESSION_FACTORY; } }
Under any class, press Alt+Enter and select Create Test
Then select TestNG to test in the pop-up dialog box
Then you can create a new Test class with the default name and add the following code
package model; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); private Session session; @BeforeMethod public void setUp() { session = sessionFactory.openSession(); session.beginTransaction(); } @AfterMethod public void tearDown() { session.getTransaction().commit(); session.close(); } @Test public void add(){ Student student = new Student(); student.setName("Zhang San"); session.save(student); } }
Then click on the run button on the left to run and you can see that there are tables in the database.