1. Database
1. Create database hibernate01-1514010311
2. Create the table customer
CREATE TABLE customer( id int(11) not null auto_increment PRIMARY KEY, name VARCHAR(20) DEFAULT NULL COMMENT '姓名', age int(11) DEFAULT NULL COMMENT 'age', sex VARCHAR(2) DEFAULT null COMMENT 'sex', city VARCHAR(20) DEFAULT NULL COMMENT 'city' );
2. Create a project and import the jar package
1. Use eclipse to create a web project and import the jar package required by hibernate
2. Create an entity class
package domain; public class Customer { private Integer id; private String name; private Integer age; private String sex; private String city; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", city=" + city + "]"; } }
3. Write the mapping file
Create Customer.hbm.xml under the domain package
<?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> <class name="domain.Customer" table="customer"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name" type="string"></property> <property name="age" column="age" type="integer"></property> <property name="sex" column="sex" type="string"></property> <property name="city" column="city" type="string"></property> </class> </hibernate-mapping>
4. Write the core configuration file hibernate.cfg.xml
Create hibernate.cfg.xml in the src directory
<?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> <!-- Specify dialect--> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- database driver--> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- database url --> <property name="hibernate.connection.url">jdbc:mysql:///hibernate01-1514010311</property> <!-- database connection username--> <property name="hibernate.connection.username">root</property> <!-- database connection password--> <property name="hibernate.connection.password">0x3137</property> <!-- Print the sql statement generated by hibernate to the console --> <property name="hibernate.show_sql">true</property> <!-- Format the sql statement generated by hibernate (syntax indentation) --> <property name="hibernate.format_sql">true</property> <mapping resource="domain/Customer.hbm.xml" /> </session-factory> </hibernate-configuration>
5. Write test classes
Import the JUnit4 runtime environment (details of Baidu JUnit unit testing, niche)
Select the project and click the right mouse button -> select Build Path-> select Configure Build Path-> select as shown, select Add Library-> select JUnit->next->finish->apply
Create the CustomerTest class under the test package
package test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import domain.Customer; public class CustomerTest { @Test public void insertTest() { //1. Load the core configuration file Configuration config = new Configuration().configure(); //2. Get SessionFactory SessionFactory sessionFactory = config.buildSessionFactory(); //3. Get a session Session session = sessionFactory.openSession(); //4. Open the transaction Transaction transaction = session.beginTransaction(); //5. Operation Customer customer = new Customer(); customer.setName("The third brother is innocent"); customer.setAge(21); customer.setSex("男"); customer.setCity("Harbin"); //5.2 Store the data in the table session.save(customer); //6. Commit transaction transaction.commit(); //7. Close the resource session.close(); sessionFactory.close(); } }
Select the method name under @Test->Right click->Run As->JUnit Test
Test add method insertTest()
In this regard, a simple hibernate demo has been written.