標籤:j2ee 單元測試 oracle 11g hibernate xml
巴巴運動網-整合hibernate4(1)1、項目圖解
2、首先我們引入相應的jar包
這裡用的是oracle 11g,所以我們使用的資料庫連接jar包是ojdbc5
3、我們配置一下資料庫中相應的實體物件
ProductType.java
/** * 功能:這是產品類別的 * 檔案:ProductType.java * 時間:2015年5月12日10:16:21 * cutter_point */package com.cutter_point.bean.product; publicclassProductType{ private Integer typeid; public Integer getTypeid() { returntypeid; } publicvoid setTypeid(Integertypeid) { this.typeid = typeid; }}
Product.hbm.xml
<?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 package="com.cutter_point.bean.product"> <class name="ProductType" table="ProductType"> <id name="typeid"column="typeid"> <generator class="sequence" /> </id> </class></hibernate-mapping>
4、我們配置一下hibernate的串連檔案
<?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> <!-- Database connectionsettings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1522:ORCL</property> <property name="connection.username">這裡填帳號</property> <property name="connection.password">你的密碼</property> <!-- JDBC connection pool (usethe built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- Enable Hibernate'sautomatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-levelcache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create thedatabase schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/cutter_point/bean/product/Product.hbm.xml"/> <!--這個是你剛剛陪的java類的路徑對象 --> </session-factory> </hibernate-configuration>
5、接下來我們測試一下hibernate是否配置成功
/** * 功能:這是產品類別的單元測試 * 檔案:ProductTest.java * 時間:2015年5月12日10:27:24 * cutter_point */package junit.test; import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.BeforeClass;import org.junit.Test; import com.cutter_point.bean.product.ProductType; publicclassProductTest{ @BeforeClass publicstaticvoid setUpBeforeClass() throws Exception { } @SuppressWarnings("deprecation") @Test publicvoid test() { ProductTypept = newProductType(); //new一個對象 pt.setTypeid(78); //設定id號碼 Configurationcfg = newConfiguration(); //得到Configuration SessionFactorysf = cfg.configure("/config/hibernate/hibernate.cfg.xml").buildSessionFactory(); //取得session工廠 Sessionsession = sf.openSession(); session.beginTransaction(); session.save(pt); session.getTransaction().commit(); session.close(); sf.close(); } }
6、總結
這個配置hibernate還是很簡單的,但是這裡注意一點,如果你的資料庫裡面沒有建立相應的表的話,又或者你sql語句不太會寫,又或者你覺得自己寫的sql語句不準,那麼這裡還有一個方法,吧hibernate設定檔hibernate.cfg.xml裡面的
<property name="hbm2ddl.auto">update</property>中的update改為create,那麼hibernate就會自動幫你建立相應的表
【j2ee spring】26、巴巴運動網-整合hibernate4(1)