您的位置:首页 > 其它

Hibernate_Notes_1_helloworld

2015-07-12 16:27 295 查看
持久化

加载:根据特定的OID,把一个对象从数据库加载到内存中。

ORM 概念就不叙述太多了...

主要对象-关系的映射、这个有点666阿!!

也就是之前对数据库的操作变成是对对象的操作。

主要记录helloworld吧、毕竟新手(主要四个步骤:创建Hibernate配置文件、创建持久化类、创建对象-关系映射文件、通过H api编写)

1、环境myeclipse

2、安装hibernate插件 版本:hibernatetools-Update-4.1.1.Final_2013-12-08_01-06-33-B605

3、jar包、自行下载阿。找不到你就别学了...这里版本:hibernate-release-4.2.4.Final

4、用的是mysql jar包版本:mysql-connector-java-5.1.33-bin

5、导入jar包 build path

6、创建Hibernate配置文件(hibernatge.cfg.xml)

具体看代码以及注释

<!-- 配置连接数据库的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">7362346</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate5</property>

<!-- 配置hibernate的基本信息 -->
<!-- hibernate 所使用的数据库方言-->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property><span style="white-space:pre">	</span><!--直接查官方文档project/etc/-->

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

<!-- 是否对SQL进行格式化 指输出到控制台的sql-->
<property name="format_sql">true</property>

<!-- 制定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定关联的 .hbm.xml文件  --> 
<mapping resource="com/test/hibernate/helloword/News.hbm.xml"/>
7、创建持久化类

package com.test.hibernate.helloword;

import java.sql.Date;

public class News {

private Integer id;
private String title;
private String author;

private Date date;

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 String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}

public News(){

}

@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author
+ ", date=" + date + "]";
}
}
8、创建对象-关系映射文件(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 2015-7-12 12:21:14 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>

<class name="com.test.hibernate.helloword.News" table="NEWS">

<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 制定主键的生成方式,native:使用数据库本地方式 -->
<generator class="native" />
</id>

<property name="title" type="java.lang.String"><span style="white-space:pre">		</span>//name类里的属性名
<column name="TITLE" /><span style="white-space:pre">		</span>//column数据表列名
</property>

<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>

<property name="date" type="java.sql.Date">
<column name="DATE" />
</property>

</class>
</hibernate-mapping>
9、具体使用api

package com.test.hibernate.helloword;

import static org.junit.Assert.*;

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() {
//1.创建一个sessionFactory对象

4000
SessionFactory sessionFactory = null;
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2.创建一个Session 对象
Session session = sessionFactory.openSession();
//3.开启事务
Transaction transaction=session.beginTransaction();
//4.执行保存执行
News news = new News("Java","sun",new Date(new java.util.Date().getTime()));
session.save(news);
//5.提交事务
transaction.commit();
//6.关闭session
session.close();
//7.关闭SessionFactory对象
sessionFactory.close();
}

}
运行情况


使用神器Navicat
for mysql      

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