您的位置:首页 > 其它

如何配置hibernate框架

2017-01-03 20:12 435 查看
在做java开发时,经常会与数据库打交道,原始的做法是使用JDBC与数据库连接,大多数人厌倦了写繁杂的sql语句,而且JDBC不符合面向对象的思想,对于开发人员来说,与数据库打交道成为了一件麻烦事。

然而hibernate的使用,简化了我们使用数据库,也符合面向对象思想。因此,hibernate很快便普及到来大多数的项目中来。

因此,我需要更深的巩固一下hibernate的用法,先从它的配置开始。

我们要生成一个周报表,实体类为ReportEntity,包括id,标题title,时间time,内容content这些字段。

首先先了解一下每个文件的大概位置,如下图所示:



配置hibernate大概需要四步:

1、hibernate所需要的jar包,当然,与数据库打交道也必须包括数据库驱动的jar包。

2、创建hibernate的配置文件,主要配置了数据库的驱动,数据库的url,数据库的用户名密码,数据库方言,以及一些数据库表的生成策略,控制台的打印,sql格式化等配置。也包括实体类的映射文件ReportEntity.hbm.xml的关联。

hibernate.cfg.xml

<?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.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>

<!-- 配置hibernate基本信息 -->
<!-- hibernate所使用的数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 执行操作时是否打印sql -->
<property name="hibernate.show_sql">true</property>
<!-- 是否对sql进行格式化 -->
<property name="hibernate.format_sql">true</property>

<!-- 指定自动生成数据库表的策略 -->
<property name="hbm2ddl.auto">update</property>

<!-- 指定关联的.hbm.xml -->
<mapping resource="com/cc/entity/ReportEntity.hbm.xml"/>

</session-factory>
</hibernate-configuration>
3、创建持久化类,这里是ReportEntity.java。

ReportEntity.java

package com.cc.entity;

import java.sql.Timestamp;

public class ReportEntity {
private Integer id;
private String title;
private Timestamp time;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
4、创建映射文件,ReportEntity.hbm.xml。

ReportEntity.hbm.xml

<?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-12-11 16:01:37 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.cc.entity.ReportEntity" table="REPORTENTITY">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键生成方式,native使用数据库本地的方式 -->
<generator class="native" />
</id>

<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="time" type="java.sql.Timestamp">
<column name="TIME" />
</property>
<property name="content" type="java.lang.String">
<column name="CONTENT" />
</property>
</class>
</hibernate-mapping>
5、使用hibernate的API去操作数据库。

虽然我们创建的是Web项目,但是这里我们创建一个main方法去操作数据库。

ReportEntity.java

package com.cc.entity;

import java.sql.Timestamp;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class ReportEntityTest {

public static void main(String[] args) {
//初始化SessionFactory,session工厂,
SessionFactory sessionFactory = null;
//读取创建的配置文件,hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
//服务的注册器,hibernate的配置和服务需要在该对象中注册后才能有效
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
//取得sessionFactory
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//创建session
Session session = sessionFactory.openSession();
//开启事务
Transaction transaction = session.beginTransaction();
//直接创建ReportEntity对象,并对属性赋值
ReportEntity report = new ReportEntity();
Date date = new Date();
Timestamp time = new Timestamp(date.getTime());
report.setTitle("王小杰的报告");
report.setContent("什么都没有");
report.setTime(time);
//执行保存操作
session.save(report);
//提交事务
transaction.commit();
//关闭session
session.close();
//关闭sessionFactory
sessionFactory.close();
}

}
这样hibernate就配置好了,最初的时候,配置hibernate往往会出现很多错误。在这里总结一下我经历过的一些错误。
1、hibernate.cfg.xml文件中hibernate.connection.url属性错误,本文章使用了简写,完整的url应该是 jdbc:mysql://localhost:3306/hibernate,其中localhost表示mysql服务器名称,这里是本地,包括127.0.0.1也表示本地,3306表示mysql使用的端口号,hibernate为数据库名

2、hibernate.cfg.xml文件中未指定.hbm.xml映射文件。该映射文件路径不是使用包名,而是使用了路径的方式。

3、数据库驱动路径也与数据库有关,使用数据库不同或者版本不同,皆有可能导致路径错误。

4、忘记生成实体类中属性的getter,setter方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息