hibernate 的入门

0:hibernate的开发步骤:

开发步骤
    1)搭建好环境
        引入hibernate最小的jar包
        准备Hibernate.cfg.xml启动配置文件
    2)写实体类(pojo)
    3)为实体类写映射文件"User.hbm.xml"
        在hibernate.cfg.xml添加映射的实体
    4)创建库表
    5)写测试类
        获得Configuration
        创建SessionFactory
        打开Session
        开启事务
        使用session操作数据
        提交事务
        关闭资源

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发行包projectetchibernate.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.jarorghibernatehibernate-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配置文件
}
}

4:hibernate 简介:

hibernate是一个开源框架,它是对象关联关系映射的框架,它对JDBC做了轻量级的封装,而我们java程序员可以使用面向对象的思想来操纵数据库。
hibernate 核心接口
session:负责被持久化对象CRUD操作
sessionFactory:负责初始化hibernate,创建session对象
configuration:负责配置并启动hibernate,创建SessionFactory
Transaction:负责事物相关的操作
Query和Criteria接口:负责执行各种数据库查询

姓名:曹晓阳 联系方式:1076675163@qq.com
原文地址:https://www.cnblogs.com/CAOXIAOYANG/p/8861609.html