Step 1: Introduce the required jar package
Step 2: Create an entity class and configure the mapping relationship between the entity class and the data table
Create entity class
User.java
package cn.hao.entity; public class User { /* hibernate requires entity classes to have a uniquely identified property */ private int uid; private String username; private String password; private String address; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Configure entity class and data table mapping relationship
Way 1: Done using xml configuration file
User.hbm.xml, there are no fixed requirements for the location and name of this file.
<?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 > <!-- 1. Configure the mapping between classes and tables class tag name attribute: the full path of the entity class table attribute: data table name --> < class name ="cn.itcast.entity.User" table ="t_user" > <!-- 2. Configure the corresponding entity class id and table id hibernate requires entity class to have a property unique value hibernate requires table to have field as unique value --> <!-- id tag name attribute: the name of the id attribute in the entity class column property: generated table field name --> < id name ="uid" column ="uid" > <!-- Set the data table id growth strategy native: The generated table id value is the automatic growth of the primary key --> < generator class ="native" ></ generator > </ id > <!-- 3. Configure other attributes and field correspondences name attribute: entity class attribute name column property: Generate table field name --> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="address" column="address"></property> </class> </hibernate-mapping>
Step 3: Create Hibernate Core Configuration File
The location and name of the core configuration file must be: hibernate.cfg.xml under src
<?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> <!-- 第一部分:配置数据库信息(必须的) --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">mysqladmin</property> <!-- Part II: Configure hibernate information (not required) --> <!-- Output the underlying sql statement in the console --> < property name ="hibernate.show_sql" > true </ property > <! --Output the sql statement format in the console -- > < property name ="hibernate.format_sql" > true </ property > <!-- hibernate automatically creates a table update: if the table already exists, update it; if the table does not exist, create the table --> < property name ="hibernate.hbm2ddl.auto" > update </ property > <!-- Configuration database dialect Implement paging keyword limit in mysql, which can only be used in mysql And use rownum key in Oracle database to implement paging operation Let the hibernate framework recognize statements from different databases --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- The third part: put the mapping file in the core configuration file (required) --> < mapping resource ="cn/itcast/entity/User.hbm.xml" /> </ session-factory > </ hibernate-configuration >
The above completes the construction of the basic use environment of the Hibernate framework.
test code
package cn.hao.hibernatetest; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import cn.hao.entity.User; public class HibernateDemo { @Test public void testAdd() { // The first step is to load the hibernate core configuration file // Go to src to find the name hibernate.cfg.xml // Encapsulate the object in hibernate Configuration cfg = new Configuration(); cfg.configure(); // The second step is to create a SessionFactory object // Read the content of the hibernate core configuration file and create a SessionFactory // In this process, according to the mapping relationship, create a data table SessionFactory sessionFactory = cfg.buildSessionFactory(); // The third step uses SessionFactory to create Session // similar to connecting Session session = sessionFactory.openSession(); // The fourth step is to open the transaction Transaction tx = session.beginTransaction(); // The fifth step is to write the specific crud operation User user = new User(); user.setUsername("小明"); user.setPassword("131415"); user.setAddress( "Shanghai" ); // The method of calling session realizes adding session.save(user); // The sixth step commits the transaction tx.commit(); //The seventh step closes the resource session.close(); sessionFactory.close(); } }