標籤:
本文將用Maven3、Hibernate3.6、Oracle10g整合,作為例子。
環境清單:
1.Maven3.0.5
2.Hibernate3.6.5 Final
3.JDK1.7.0.11
4.Oracle10g
一.首先建立表BDUSER
create table DBUSER( user_id NUMBER(5) not null, username VARCHAR2(20), created_by VARCHAR2(20), created_date DATE)
二.用Maven3建立一個web項目,項目名稱:maven-hibernate-demo
建立項目參考:Maven3路程(三)用Maven建立第一個web項目(1)
三.添加Hibernate和Oracle依賴,pom.xml如下
Hibernate有些依賴包必須添加
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lei.demo</groupId> <artifactId>maven-hibernate-demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>maven-hibernate-demo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <!-- Oracle Jdbc Driver --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.4.0</version> </dependency> <!-- Hibernate 配置 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.5.Final</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.0.GA</version> </dependency> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.6</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.0.Final</version> </dependency> </dependencies> <build> <finalName>maven-hibernate-demo</finalName> </build></project>
四.建立Hibernate Map檔案和Model類
1.建立Dbuser.hbm.xml對應檔
路徑src/main/resources/com/sulei/demo下
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2013-10-22 15:28:34 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="com.sulei.demo.Dbuser" table="DBUSER" schema="GZIP_BASE"> <id name="userId" type="int"> <column name="USER_ID" precision="5" scale="0" /> <generator class="assigned" /> </id> <property name="username" type="string"> <column name="USERNAME" length="20" /> </property> <property name="createdBy" type="string"> <column name="CREATED_BY" length="20" /> </property> <property name="createdDate" type="date"> <column name="CREATED_DATE" length="7" /> </property> </class></hibernate-mapping>
2. 建立實體類Dbuser
路徑src/main/java/com/sulei/demo下
package com.sulei.demo;// Generated 2013-10-22 15:28:34 by Hibernate Tools 3.4.0.CR1import java.util.Date;/** * Dbuser generated by hbm2java */public class Dbuser implements java.io.Serializable { private int userId; private String username; private String createdBy; private Date createdDate; public Dbuser() { } public Dbuser(int userId) { this.userId = userId; } public Dbuser(int userId, String username, String createdBy, Date createdDate) { this.userId = userId; this.username = username; this.createdBy = createdBy; this.createdDate = createdDate; } public int getUserId() { return this.userId; } public void setUserId(int userId) { this.userId = userId; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getCreatedBy() { return this.createdBy; } public void setCreatedBy(String createdBy) { this.createdBy = createdBy; } public Date getCreatedDate() { return this.createdDate; } public void setCreatedDate(Date createdDate) { this.createdDate = createdDate; }}
可以用Hibernate Tools反向工程自動聲場代碼,見以下另一篇文章:
用Hibernate Tools產生Hibernate Mapping對應檔
五.建立Hibernate.cfg.xml
建立Hibernate設定檔 “hibernate.cfg.xml” 把他放在resources目錄下: “src/main/resources/hibernate.cfg.xml“填寫資料庫相關資訊。
加入Map檔案,“DBUser.hbm.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 name=""> <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@databaseip:1521:gzip</property> <property name="hibernate.connection.username">gzip_base</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="show_sql">true</property> <mapping resource="com/sulei/demo/Dbuser.hbm.xml" /> </session-factory></hibernate-configuration>
六.建立Session工場
建立 “HibernateUtil.java” 類管理Hibernate Session。 路徑:“src/main/java/com/sulei/util/HibernateUtil.java”
package com.sulei.util;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() {
System.out.println("test----1"); return sessionFactory; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); }}
六. 代碼測試
建立App.java向表中插入一條記錄。
package com.sulei.test;import java.util.Date;import org.hibernate.Session;import com.sulei.util.HibernateUtil;import com.sulei.demo.Dbuser;public class App { public static void main(String[] args) { System.out.println("Maven3 + Hibernate + Oracle10g"); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Dbuser user = new Dbuser(); user.setUserId(100); user.setUsername("leioolei"); user.setCreatedBy("system"); user.setCreatedDate(new Date()); session.save(user); session.getTransaction().commit(); }}
項目組織好,目錄結構見
在App.java上右鍵運行,結果
測試成功!!!
Hibernate項目用Maven建立(轉)