Hibernate 配置文件的配置 _rec 连接oracle (笔记二)

Hibernate 的开发环境

documentation  : Hibernate 开发者文档

lib                        : Hibernate 开发包

        required      : Hibernate 开发的必须的依赖包

        option         :Hibernate 开发的可选jar 包

project                :Hibernate 提供的项目

创建一个项目 ,引入jar包:

  1. 数据库驱动包
  2. Hibernate 开发必须jar包
  3. Hibernate 日志记录包

创建表

-- Create table
create table CST_CUSTOMER
(
  cust_id       NUMBER not null,
  cust_name     VARCHAR2(32),
  cust_source   VARCHAR2(32),
  cust_industry VARCHAR2(32),
  cust_level    VARCHAR2(32),
  cust_phone    VARCHAR2(64),
  cust_mobile   VARCHAR2(16)
)
tablespace ZDTBMWSITE
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 8K
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints 
alter table CST_CUSTOMER
  add primary key (CUST_ID)
  using index 
  tablespace ZDTBMWSITE
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

创建实体类

package com.test;
/**
 * 
 * @author Administrator
 *
 */

public class Customer {
	private Long cust_id;
	private String cust_name;
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	private String cust_phone;
	private String cust_mobile;
	
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long cust_id) {
		this.cust_id = cust_id;
	}
	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
	public String getCust_source() {
		return cust_source;
	}
	public void setCust_source(String cust_source) {
		this.cust_source = cust_source;
	}
	public String getCust_industry() {
		return cust_industry;
	}
	public void setCust_industry(String cust_industry) {
		this.cust_industry = cust_industry;
	}
	public String getCust_level() {
		return cust_level;
	}
	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}
	public String getCust_phone() {
		return cust_phone;
	}
	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}
	public String getCust_mobile() {
		return cust_mobile;
	}
	public void setCust_mobile(String cust_mobile) {
		this.cust_mobile = cust_mobile;
	}
	
	
	
}

创建映射

映射通过XML的配置,尽量统一命名规范(类名.hbm.xml)。

<?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="com.test.Customer" table="cst_customer">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="cust_id" column="cust_id" type="java.lang.Long">
			<generator class="sequence" >
			 <param name="sequence_name">cust_id_sequence</param>  
			</generator>
		</id>

		<!-- 建立类中的普通的属性和表的字段的对应 -->
		<property name="cust_name" column="cust_name" type="java.lang.String" length="32"  />
		<property name="cust_source" column="cust_source" type="java.lang.String" length="32" />
		<property name="cust_industry" column="cust_industry" type="java.lang.String" length="32" />
		<property name="cust_level" column="cust_level" type="java.lang.String" length="32" />
		<property name="cust_phone" column="cust_phone" type="java.lang.String" length="64" />
		<property name="cust_mobile" column="cust_mobile" type="java.lang.String" length="16" />
		
	</class>

</hibernate-mapping>

应注意 sequence 由于oracle中没有自增长  所以需要自己创建oracle sequences

-- Create sequence 
create sequence CUST_ID_SEQUENCE
minvalue 1
maxvalue 9999999999999999999999999999
start with 1010
increment by 1
cache 10;

创建hibernate的核心控制文件

这个Hibernate的核心配置文件 必须命名为 :hibernate.cfg.xml

因为hibernate jar包中已经定义了成了常量

<?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>
		<!-- 链接数据库 的基本参数 -->
		<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1522:orcl</property>
		<property name="hibernate.connection.username">hncdsite</property>
		<property name="hibernate.connection.password">power123</property>
		<!-- 配置Hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
		<!-- 可选配置 -->
		<!-- 打印sql -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化sql -->
		<property name="hibernate.format_sql">true</property>
		<mapping resource="com/test/customer.hbm.xml" />
	</session-factory>
</hibernate-configuration>

测试

package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class hibernateDemo {
        @Test
       public void demo() {
        	
        	//1.加载Hibernate的核心配置文件
        		Configuration configuration = new Configuration().configure();
        	// 2.创建一个SessionFactory对象: 类似于JDBC中的连接池
        	SessionFactory sesionFactory = configuration.buildSessionFactory();
        	// 3.通过SessionFactory获取到Session对象: 类似于JDBC中Connection
        	Session seesion = sesionFactory.openSession();
        	// 4.手动开启事务:
        	Transaction transaction = seesion.beginTransaction();
        	// 5.编写代码
        	Customer customer = new Customer();
        	customer.setCust_name("cc");
        	try {
        		seesion.save(customer);
			} catch (Exception e) {
				System.out.println(e);
				// TODO: handle exception
			}
        	
            //6.事务提交
        	transaction.commit();
        	//7.资源释放
        	seesion.close();
        }
}

控制台:

oracle结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值