標籤:tools package 架構 builder strong junit hbm.xml 自動建表 for
API
package com.hanqi.test;import static org.junit.Assert.*;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.junit.Test;public class Test01 {//測試Hibernate串連資料庫@Testpublic void test() {//1.擷取設定檔Configuration cfg = new Configuration().configure();//2.註冊配置ServiceRegistry sr =new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();//3. 擷取Session-Factory(相當於JDBCd 串連)SessionFactory sf = cfg.buildSessionFactory(sr);System.out.println(sf);sf.close();}}
設定檔
<?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">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="hibernate.connection.username">test0816</property> <!-- 資料庫方案--> <property name="hibernate.default_schema">TEST0816</property> <!-- 資料庫方言--> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 調試--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 自動建表方式--> <property name="hibernate.hbm2ddl.auto">create</property> <!-- 對應檔--> <mapping resource="com/hanqi/entity/User.hbm.xml"/> </session-factory></hibernate-configuration>
ORM
<?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 2016-11-7 14:39:56 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="com.hanqi.entity.User" table="T_USER"> <id name="userId" type="int"> <column name="USERID" /> <generator class="native" /> </id> <property name="userName" type="java.lang.String"> <column name="USERNAME" length="20" not-null="true" unique="true"/> </property> <property name="birthday" type="java.util.Date"> <column name="BIRTHDAY" sql-type="DATE"/> </property> <property name="money" type="double"> <column name="MONEY" sql-type="NUMBER" default="0" length="8" scale="2"/> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="10"/> </property> </class></hibernate-mapping>
Hibernate架構配置