1:加入Hibernate的相关的jar包
antlr-2.7.7.jar
classmate-1.3.0.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.2.12.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-2.0.3.Final.jar
javassist-3.20.0-GA.jar
jboss-logging-3.3.0.Final.jar
jboss-transaction-api_1.2_spec-1.0.1.Final.jar
2,写好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.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置hibernate的基本信息 -->
<!-- -配置hibernate所用的方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</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/yidu/hibernate/news.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.创建一个实体类
package com.yidu.hibernate;
import java.sql.Date;
public class news {
private Integer id;
private String title;
private String author;
private Date date;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public news() {
}
@Override
public String toString() {
return "news [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
}
public news(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}
}
4.创建news.hbm.xml自动生成对象关系映射,并把这个文件引入hibernate.cfg.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-12-4 2:20:08 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.yidu.hibernate.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.sql.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
5.测试
package com.yidu.hibernate;
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.junit.Test;
public class HibernateTest {
@Test
public void test() {
//1.创建一个sessionfactory的对象
SessionFactory sessionFactory=null;
//1)创建configuration对象:对应hibernate的基本信息和对象信息
Configuration configuration=new Configuration().configure();
//4.0之前这样创建
sessionFactory=configuration.buildSessionFactory();
//2)创建一个ServiceRegistry对象,hibernate4.x添加的新对象
//hibernate的任何配置和服务都需要在该对象中注册后才能有效
//ServiceRegistry serviceRegistry=new Serviceregistry
//2.创建一个session的对象
Session session = sessionFactory.openSession();
//3.开启事物
Transaction bt = session.beginTransaction();
//4.执行保存操作
news ne=new news("java","yidu",new Date(new java.util.Date().getTime()));
session.save(ne);
//5.提交事物
bt.commit();
//6.关闭session
session.close();
//7.关闭sessionfactory的对象
sessionFactory.close();
}
}