hibernate study notes

Step 1: Create a new project. Adding the hibernate jar package to the project's WebContent/WEB-INF/lib directory probably requires the following jar packages:






Step 2: 

             Create hibernate.cfg in the src folder of this project .xml configuration file. Since my database is mysql, the following is the specific configuration information:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- After the link database, chengs is the name of the database -->
<property name="hibernate.connection.url">jdbc:mysql://192.168.99.100:3306/chengs</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- Database account password-->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>

<!-- Tool to update data table according to schema-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- show SQL -->

<property name="hibernate.show_sql">true</property>
<!-- Data table mapping configuration file -->
<mapping resource="com/model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Step 3:
    
    Create entity class User.java
public class User {

		 String id;
		 String name;
		 public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		String password;
		public void setCreateTime(Date date) {
			// TODO Auto-generated method stub
			
		}
		public void setExpireTime(Date date) {
			// TODO Auto-generated method stub
			
		}
		

	

}


Step 4:
   Create User.hbm.xml, this file is used to complete the field mapping between objects and database tables. That is to say, those fields of the entity class need to be mapped to the tables of the database.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="hibernate">
<class name="User" table="t_user">

	<id name="id" column="id">
		<generator class="uuid"/>
	</id>
	<property name="name" column="name"/>
	<property name="password"/>
	 
</class>
</hibernate-mapping>


Step 5:

    Create testSave to run the test and save an object
package hibernate;



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class testSave1 {

	public static void main(String[] args) {
		Session session = null;
		Transaction tx = null;
		User user = null;
		
			//Create a Configuration object: basic configuration information and object relationship mapping corresponding to hibernate
		Configuration conf=new Configuration().configure();
		//Create a SessionFactory;
		
		SessionFactory sessionFactory=conf.buildSessionFactory();
		//Create a Session object
		session=sessionFactory.openSession();
		// start the transaction
		tx = session.beginTransaction();
				
			//Transient״̬
			user = new User();
			user.setName("chengs");
			user.setPassword("123");
			
			// save user
			session.save(user);
			// commit the transaction
			tx.commit();
		 //Close the session
		 session.close();
		 //Close the sessionFactory object
		 sessionFactory.close();

	}

}

Then go to check the database, if there are data chengs and 123, it means that hibernate was created successfully

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326323570&siteId=291194637