码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate框架配置

时间:2016-11-07 22:18:29      阅读:315      评论:0      收藏:0      [点我收藏+]

标签: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连接数据库
	@Test
	public 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框架配置

标签:tools   package   框架   builder   strong   junit   hbm.xml   自动建表   for   

原文地址:http://www.cnblogs.com/liuyanzeng/p/6040762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!