您的位置:首页 > 编程语言

2_Hibernate_HelloWorld 代码实现部分

2017-04-11 16:31 246 查看
准备工作:

1.导入hibernate需要的jar包:改目录下的

hibernate-release-4.2.4.Final\lib\required

2.导入数据库驱动包

Hibernate开发步骤

1. 创建 Hibernate 配置文件()

2. 创建持久化类(pojo)

3. 创建对象-关系映射文件

4. 通过 Hibernate API 编写访问数据库的代码

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>
<!-- 配置链接数据库的基本信息 jdbc\:mysql\://127.0.0.1\:3306/itrusportal-->
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate1</property>

<!-- 配置 hibernate 的基本信息 -->
<!-- hibernate 所使用的数据库方言
用了org.hibernate.dialect.MySQLInnoDBDialect之后,出现无法执行的问题:
org.hibernate.exception.SQLGrammarException: could not execute statement
,改用org.hibernate.dialect.MySQLDialect之后正常了-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property>

<!-- 是否对 SQL 进行格式化 -->
<property name="format_sql">true</property>

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

<!-- 指定关联的 .hbm.xml 文件 :注意取的是路径-->
<mapping resource="com/hgh/hibernate/helloworld/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Person

package com.hgh.hibernate.helloworld;

import java.sql.Date;

public class Person {
private Integer id;
private String name;
private Date birthDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Person(String name, Date birthDate) {
super();
this.name = name;
this.birthDate = birthDate;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", birthDate="
+ birthDate + "]";
}

}


Person.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 2017-4-11 15:06:03 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hgh.hibernate.helloworld.Person" table="PERSON" dynamic-insert="true">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="birthDate" type="java.sql.Date">
<column name="BIRTHDATE" />
</property>
</class>
</hibernate-mapping>


HibernateTest

package com.hgh.hibernate.helloworld;

import java.sql.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;
import org.junit.Test;

public class HibernateTest {

@Test
public void test() {

System.out.println("test...");

//1. 创建一个 SessionFactory 对象
SessionFactory sessionFactory = null;

//1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息,使用了默认值,会去加载默认路径下的默认名称的配置文件
Configuration configuration = new Configuration().configure();

//4.0 之前这样创建
//      sessionFactory = configuration.buildSessionFactory();

//2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象
//hibernate 的任何配置和服务都需要在该对象中注册后才能有效.
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
//3).
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

//2. 创建一个 Session 对象
Session session = sessionFactory.openSession();

//3. 开启事务
Transaction transaction = session.beginTransaction();

//4. 执行保存操作
Person person = new Person("hgh", new Date(new java.util.Date().getTime()));
session.save(person);
//5. 提交事务
transaction.commit();

//6. 关闭 Session
session.close();

//7. 关闭 SessionFactory 对象
sessionFactory.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate