Development tool: MyEclipse 2013, database: MySql
1. First, create a database in the database. The database tool I use is sqllog
Create the following database:
Open MyEclispe after database creation
2. Create Web Project
2.1: Item 1: Guide Package
The following packages need to be imported:(these bags can be found on the Internet, and I will share them in my resources)
2.2: write configuration file: hibernate.cfg.xml
- <span style="white-space:pre;"> </span><hibernate-configuration>
- <!-- Configuration database parameters -->
- <session-factory>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/hibertest?characterEncoding=utf-8</property>
- <property name="connection.username">root</property>
- <property name="connection.password">sasa</property>
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!-- display sql Sentence -->
- <property name="show_sql">true</property>
- <!-- formatting code -->
- <property name="format_sql">true</property>
- <mapping resource="com/entity/Dept.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
2.3: create an entity class and give the get/set method with or without parameters
- package com.entity;
- import java.io.Serializable;
- public class Dept implements Serializable {
- private int deptno;
- private String deptname;
- private String loc;
- public int getDeptno() {
- return deptno;
- }
- public void setDeptno(int deptno) {
- this.deptno = deptno;
- }
- public String getDeptname() {
- return deptname;
- }
- public void setDeptname(String deptname) {
- this.deptname = deptname;
- }
- public String getLoc() {
- return loc;
- }
- public void setLoc(String loc) {
- this.loc = loc;
- }
- public Dept(int deptno, String deptname, String loc) {
- super();
- this.deptno = deptno;
- this.deptname = deptname;
- this.loc = loc;
- }
- public Dept() {
- super();
- // TODO Auto-generated constructor stub
- }
- }
The file Dept.hbm.xml should be created in the entity package;
The code is as follows:
- <?xml version="1.0"?>
- <!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="com.entity.Dept" table="dept">
- <!-- Primary key mapping -->
- <id name="deptno" column="deptno">
- <!-- The strategy of generating primary key -->
- <generator class="native"></generator>
- </id>
- <!-- Here name Value corresponds to attribute in entity class -->
- <property name="deptname" column="deptname"></property>
- <property name="loc" column="loc"></property>
- </class>
- </hibernate-m
Write the test class as follows:
The code is as follows:
- package com.test;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
- import com.entity.Dept;
- import junit.framework.TestCase;
- public class Test extends TestCase {
- //Add Department
- public void testAdd() {
- //Read hibernate configuration file
- Configuration config = new Configuration()
- .configure("hibernate.cfg.xml");
- //All configurations are registered with a class
- ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
- config.getProperties()).buildServiceRegistry();
- //Get sessionfactory
- SessionFactory sf = config.buildSessionFactory(sr);
- //Get session, interface to operate database
- Session session = sf.openSession();
- //Create a department object
- Dept dept = new Dept();
- dept.setDeptname("Development Department");
- dept.setLoc("Wuhan");
- //Open things
- Transaction tran = session.beginTransaction();
- //Perform insert operation
- //try catch manually - right click - surround with
- try {
- session.save(dept);
- tran.commit();
- } catch (Exception e) {
- tran.rollback();
- e.printStackTrace();
- } finally {
- session.close();
- }
- }
- }
- The overall figure is as follows:
- Run the testAdd() method in the test class:
- The < span style = "white space: pre;" > console will print out the sql statement as follows:
- Hibernate:
- insert
- into
- dept
- (deptname, loc)
- values
- (?, ?)
- Database data addition result: