目錄結構如圖,java
1.用MyEclipse創建一個Maven-Java項目,而後給出pom配置,mysql
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xuebaosoft.hibernate4</groupId> <artifactId>hibernate4-maven-conf</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hibernate4-maven-conf</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.8.Final</version> </dependency> <!-- Hibernate uses jboss-logging for logging, for the tutorials we will use the sl4fj-simple backend --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies> </project>
2.環境準備好之後寫一個pojo-UserModel.java,sql
package modelTest; public class UserModel { private String uuid; private int userId; private String name; private int age; public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
3.配置hibernate數據源環境xml--hibernate.cfg.xml,數據庫
<?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.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://192.168.191.1:3306/mysql</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="UserModel.hbm.xml"/> </session-factory> </hibernate-configuration>
解釋下:apache
show_sql爲true表示在執行數據庫操做的時候sql語句會以log的形式打印在console;session
hbm2ddl.auto爲create表示先刪除指定的實體-關係表(無論存在不存在都先執行刪除操做),而後再進行操做,這個也算是hibernate的主要實用優點之一,不用你寫sql建表了。app
mapping這裏對應了實體-關係表的配置文件框架
4.實體-關係表的配置--UserModel.hbm.xmlmaven
<?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> <class name="modelTest.UserModel" table="tbl_user"> <id name="userId"> <generator class="native" /> </id> <property name="uuid"></property> <property name="name"></property> <property name="age"></property> </class> </hibernate-mapping>
解釋下,測試
name爲modelTest.UserModel對應的是那個POJO,table爲tbl_user表示對應數據庫的那個表
id標識userId字段爲主鍵,其他的都不是主鍵,generator爲native或者是increment表示自增,這裏有篇帖子但願瞭解區別的能夠參考下:**(帖子我忘了,找個機會補),總之native是本地化的方案,可能會使用increment進行代替,能夠理解爲native是一個抽象父類,increment是一個實現它的具體類,但具體類不止increment一個,這裏自增主鍵用native和increment都是能夠的(官方demo用的就是increment)
這樣配置好之後就能夠進行測試了。
5.使用JUnit進行測試,
package foo; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import modelTest.UserModel; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class AppTest extends TestCase { private SessionFactory sessionFactory; public AppTest(String testName) { super(testName); } protected void setUp() throws Exception { sessionFactory = new Configuration().configure().buildSessionFactory(); } protected void tearDown() throws Exception { if (sessionFactory != null) { sessionFactory.close(); } } public static Test suite() { return new TestSuite(AppTest.class); } public void testApp() { UserModel um = new UserModel(); um.setUuid("1"); um.setName("name1"); um.setAge(1); Session s = sessionFactory.openSession(); Transaction t = s.beginTransaction(); s.save(um); t.commit(); } }
6.分析一下,
首先數據庫mysql會刪除tbl_user這張表,而後重建,以後就是插入一條記錄
關鍵代碼也就是
sessionFactory = new Configuration().configure().buildSessionFactory();
而後用這個sessionFactory去open一個session,
Session s = sessionFactory.openSession();
再以後就用這個session利用hibernate操做數據庫,就是這麼一個過程,
Transaction t = s.beginTransaction();
s.save(um);
t.commit();
其中這個POJO就是讓這個session的save使用的,這樣就會在ORM的框架下插入一條記錄。
文章的意義也就是記錄下Hibernate4用的哪些Jar包,還有基本的配置過程。