您的位置:首页 > 其它

hibernate框架基本搭建及使用

2016-05-17 08:33 465 查看

简介

hibernate是一款重量级的ORM框架。ORM(Object/Relational Mapping)即对象/关系映射,是一种数据持久化技术。它使得对象模型与关系型数据库建立起对应关系,并且提供了一种机制,通过JavaBean对象去操作数据库表中的数据。

jar包

下载地址

使用的是 hibernate-release-5.0.6.Final

导入 hibernate-release-5.0.6.Final\lib\required 下的所有jar包,以及一些必须依赖jar包,如下:



mysql



xml文件配置

hibernate框架的xml文件分为两种,hibernate.cfg.xml:主配置文件,xxx.hbm.xml:映射配置文件。

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>
<!-- 数据库连接配置 -->
<property name="connection.url">jdbc:mysql://localhost:3306/exam2</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<!-- hibernate相关属性配置 -->
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否打印sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否自动生成表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 是否格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 是否自动提交 -->
<property name="hibernate.connection.autocommit">true</property>

<!-- 映射文件路径配置 -->
<mapping resource="cn/xiedacon/domain/Person.hbm.xml" />

</session-factory>

</hibernate-configuration>


例子

Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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="cn.xiedacon.domain.Person" table="person">
<id name="id">
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>

<property name="name"></property>
<property name="birthday"></property>

</class>
</hibernate-mapping>


Person.java

package cn.xiedacon.domain;

import java.util.Date;

public class Person {

private Integer id;
private String name;
private Date birthday;
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 getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", birthday=" + birthday + "]";
}
}


Test.java

package cn.xiedacon.domain;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Test {

private static SessionFactory sessionFactory = new Configuration()//
.configure()//
.buildSessionFactory();

/**
* 保存Person
*/
@org.junit.Test
public void test1(){
Person person = new Person();
person.setName("aaa");
person.setBirthday(new Date());

Session session = sessionFactory.openSession();
session.save(person);
session.close();
}

/**
* 获取Person
*/
@org.junit.Test
public void test2(){
Session session = sessionFactory.openSession();
Person person = (Person) session.get(Person.class, 1);

System.out.println(person);

session.close();
}
}


执行test1( )后:



执行test2( )后:

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