一个helloword hibernate配置以及查询

搭建一个Hibernate环境,开发步骤:

1. 下载源码

版本:hibernate-distribution-3.6.0.Final

2. 引入jar文件

hibernate3.jar核心  +  required 必须引入的(6) +  jpa 目录  + 数据库驱动包

3. 写对象以及对象的映射

Employee.java            对象

Employee.hbm.xml        对象的映射 (映射文件)

4. src/hibernate.cfg.xml  主配置文件

-à 数据库连接配置

-à 加载所用的映射(*.hbm.xml)

5. App.java  测试

Employee.java     对象

//一、 对象

public class Employee {

private int empId;

private String empName;

private Date workDate;

}

Employee.hbm.xml  对象的映射

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC 

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.a_hello">

<class name="Employee" table="employee">

<!-- 主键 ,映射-->

<id name="empId" column="id">

<generator class="native"/>

</id>

<!-- 非主键,映射 -->

<property name="empName" column="empName"></property>

<property name="workDate" column="workDate"></property>

</class>

</hibernate-mapping>

hibernate.cfg.xml    主配置文件

<!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">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<property name="hibernate.show_sql">true</property>

<!-- 加载所有映射 -->

<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>

</session-factory>

</hibernate-configuration>

App.java   测试类

public class App {

@Test

public void testHello() throws Exception {

// 对象

Employee emp = new Employee();

emp.setEmpName("班长");

emp.setWorkDate(new Date());

// 获取加载配置文件的管理类对象

Configuration config = new Configuration();

config.configure();  // 默认加载src/hibenrate.cfg.xml文件

// 创建session的工厂对象

SessionFactory sf = config.buildSessionFactory();

// 创建session (代表一个会话,与数据库连接的会话)

Session session = sf.openSession();

// 开启事务

Transaction tx = session.beginTransaction();

//保存-数据库

session.save(emp);

// 提交事务

tx.commit();

// 关闭

session.close();

sf.close();

}

}

Hibernate  Api

|-- Configuration       配置管理类对象

config.configure();    加载主配置文件的方法(hibernate.cfg.xml)

默认加载src/hibernate.cfg.xml

config.configure(“cn/config/hibernate.cfg.xml”);   加载指定路径下指定名称的主配置文件

config.buildSessionFactory();   创建session的工厂对象

|-- SessionFactory     session的工厂(或者说代表了这个hibernate.cfg.xml配置文件)

sf.openSession();   创建一个sesison对象

sf.getCurrentSession();  创建session或取出session对象

|--Session       session对象维护了一个连接(Connection), 代表了与数据库连接的会话。

   Hibernate最重要的对象: 只用使用hibernate与数据库操作,都用到这个对象

session.beginTransaction(); 开启一个事务; hibernate要求所有的与数据库的操作必须有事务的环境,否则报错!

更新:

session.save(obj);   保存一个对象

session.update(emp);  更新一个对象

session.saveOrUpdate(emp);  保存或者更新的方法:

à没有设置主键,执行保存;

à有设置主键,执行更新操作;

à如果设置主键不存在报错!

主键查询:

session.get(Employee.class, 1);    主键查询

session.load(Employee.class, 1);   主键查询 (支持懒加载)

HQL查询:

HQL查询与SQL查询区别:

SQL: (结构化查询语句)查询的是表以及字段;  不区分大小写。

HQL: hibernate  query  language hibernate提供的面向对象的查询语言

查询的是对象以及对象的属性。

区分大小写。

Criteria查询:

 完全面向对象的查询。

本地SQL查询:

复杂的查询,就要使用原生态的sql查询,也可以,就是本地sql查询的支持!

(缺点: 不能跨数据库平台!)

|-- Transaction    hibernate事务对象

 
原文地址:https://www.cnblogs.com/hello-liyb/p/7802784.html