运行环境:eclipse数据库MySQL
一、首先说以下eclipse中集成hibernateTool工具这样在有关hibernate的开发中会有只能提示
1、下载hibernatetools-Update-4.1.1.Final_2013-12-08_01-06-33-B605工具。下载地址:http://pan.baidu.com/s/1o82hrdC
下载后点击hellp->>install new software按照下图步骤进行操作hibernatetools工具不要放在中文路径下边
然后下一步下一步就行了,集成完后会重启eclipse,然后打开select a wizard如果出现hibernate文件夹表示集成成功!
二、新建一个web项目(建java也行)
1、在src目录下创建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="connection.username">root</property>
<property name="connection.password">orcl</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test?characterEncoding=utf8 </property>
<!-- 配置hibernate的基本信息 -->
<!-- hibernate的方言配置 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 是否显示及格式化 SQL hibernate.show_sql 可以省略hibernate,但是在集成Struts2、spring的时候没有hibernate会报错-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 生成数据表的策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 指定关联的.hbm.xml文件-->
<mapping resource="com/lyd/hibernate/helloword/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2、创建域模型(实体类)
package com.lyd.hibernate.helloword;
import java.util.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() {
}
public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
}
}
3、创建映射文件News.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-21 15:02:25 by Hibernate Tools 3.4.0.CR1 -->
<
hibernate-mapping
>
<
class
name
=
"com.lyd.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"
>
<
column
name
=
"TITLE"
/>
</
property
>
<
property
name
=
"author"
type
=
"
java.lang.String
"
>
<
column
name
=
"AUTHOR"
/>
</
property
>
<
property
name
=
"date"
type
=
"java.util.Date"
>
<
column
name
=
"DATE"
/>
</
property
>
</
class
>
</
hibernate-mapping
>
|
因为我们已经集成了hibernate开发工具所以直接新建xml文件即可如下图,选中点击下一步生成hibernate.hbm.xml文件
生成的文件中需要修改该id主键的生成方式如下图修改成native
测试代码HibernateTest.java
|
package com.lyd.hibernate.helloword;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void Test() {
// 1、创建sessionFactory
SessionFactory sessionFactory = null;
// 2、创建session
Configuration configuration = new Configuration().configure();
/*在4.0以后的版本中用该部分代码
ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();*/
//4.0版本之前用的代码
sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
News news = new News(1, "UpdateTest", "zhagnsan", new Date(new java.util.Date().getTime()));
// session.save(news);
//保存或者更新数据库数据,如果id已经存在则更新数据如果不存在则新增一条数据
session.saveOrUpdate(news);
session.get(News.class, 1);
transaction.commit();
session.close();
sessionFactory.close();
}
public static void main(String[] args) {
Test();
}
}
*本文解析的是简单的hibernate框架配置,是最基本最基础的