hibernate 的入门

1:hibernate.cfg.xml 配置文件

<?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>
<!-- 属性的name可以在 Hibernate发行包project\etc\hibernate.properties中找到。 参数名称的hibernate前缀可以省略 -->
<!-- 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 数据库连接池配置:使用Hibernate内置数据源 -->
<property name="connection.pool_size">10</property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 二级缓存配置:暂时关闭 -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- 控制台打印Hibernate执行的SQL语句 -->
<property name="show_sql">true</property>
<!-- 以方便阅读的形式显示sql语句 -->
<property name="hibernate.format_sql">false</property>
<!-- 自动生成DDL:Data Definition Language定义表结构等 -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/anrongtec/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

2:Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- dtd文件在:hibernate3.jar\org\hibernate\hibernate-mapping-3.0.dtd -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.anrongtec.domain.Customer" table="CUSTOMERS">
<!-- 映射主键 -->
<id name="id" column="ID">
<!-- 主键生成策略:目前暂时记住用native -->
<generator class="native"></generator>
</id>
<!-- 映射类中的属性和数据库表字段的关系 -->
<property name="name" column="NAME" type="string" length="100"></property>
<property name="gender" column="GENDER" type="boolean"></property>
<property name="birthday" column="BIRTHDAY"></property>
</class>
</hibernate-mapping>

3:测试类。

package com.anrongtec.test;

import java.util.Date;

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

import com.anrongtec.domain.Customer;

public class CustomerTest {
//加载配置文件的方式。
@Test
public void saveCustomer() {
Customer cs = new Customer(01, "曹肖扬", true, new Date());
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session openSession = sessionFactory.openSession();
Transaction beginTransaction = openSession.beginTransaction();
openSession.save(cs);
beginTransaction.commit();
openSession.close();
sessionFactory.close();
// insert into CUSTOMERS (NAME, GENDER, BIRTHDAY) values (?, ?, ?)
}
//Configuration加载配置文件,
public void saveTest() {
// 创建一个对象
Customer cs = new Customer(01, "caoxiaoyang", true, new Date());
//Customer c = new Customer("caoxiaoyang", false, new Date());
// 初始化Hibernate的配置文件
Configuration cfg = new Configuration();
cfg.configure();// 加载hibernate.cfg.xml配置文件
// 创建SessionFactory工厂
SessionFactory sessionFactory = cfg.buildSessionFactory();
// 得到Session对象:核心.Hibernate的操作都是基于session
Session session = sessionFactory.openSession();
// 开启事务
Transaction tx = session.beginTransaction();// start transaction
session.save(cs);// 保存实体对象
tx.commit();// 提交事务
// 释放占用的资源
session.close();
sessionFactory.close();
}
public void save10(){
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// 加载hibernate.cfg.xml配置文件
}
}

猜你喜欢

转载自www.cnblogs.com/CAOXIAOYANG/p/8861609.html