安装eclispe hibernate插件
创建一个工程导入jar包
hibernate链接-密码:bi6o
导入hibernate-release-5.2.10.Final\hibernate-release-5.2.10.Final\lib\required下面的jar包到工程并添加到BuildPath
加入mysql驱动-点击下载
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">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://xxxx.xxxx.xxxx.xxxx:3306/hibernate</property>
<!-- 配置hibernate的基本信息 -->
<!-- hibernate所使用的数据库方言 -->
<!-- <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> -->
<!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="dialect">org.hibernate.dialect.MySQLMyISAMDialect</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/hgaong/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
新建bean类
package com.hgaong.helloworld;
import java.sql.Date;
public class News {
private int nId;
private String nTitle;
private String nAuthor;
private Date createDate;
public int getnId() {
return nId;
}
public void setnId(int nId) {
this.nId = nId;
}
public String getnTitle() {
return nTitle;
}
public void setnTitle(String nTitle) {
this.nTitle = nTitle;
}
public String getnAuthor() {
return nAuthor;
}
public void setnAuthor(String nAuthor) {
this.nAuthor = nAuthor;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Override
public String toString() {
return "News [nId=" + nId + ", nTitle=" + nTitle + ", nAuthor=" + nAuthor + ", createDate=" + createDate + "]";
}
public News(String nTitle, String nAuthor, Date createDate) {
super();
this.nTitle = nTitle;
this.nAuthor = nAuthor;
this.createDate = createDate;
}
public News() {
super();
}
}
生成 bean.hbm.xml
右键点击上面建的News.java使用插件快速建立 对象-数据表 映射文件
<?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-29 0:47:34 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.hgaong.helloworld.News" table="NEWS">
<id name="nId" type="int" access="field">
<column name="NID" />
<!-- 改成naive -->
<generator class="native" />
</id>
<property name="nTitle" type="java.lang.String" access="field">
<column name="NTITLE" />
</property>
<property name="nAuthor" type="java.lang.String" access="field">
<column name="NAUTHOR" />
</property>
<property name="createDate" type="java.sql.Date">
<column name="CREATEDATE" />
</property>
</class>
</hibernate-mapping>
测试类
package com.hgaong.helloworld;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateTest {
@Test
public void test() {
// 1.创建一个SessionFactory对象
SessionFactory sessionFactory = null;
// 1).创建Configuration对象:对应hibernate的基本配置信息和对象关系信息
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
// 2.创建一个Session对象
Session session = sessionFactory.openSession();
// 3.开启事务
Transaction transaction = session.beginTransaction();
// 4.执行保存操作
News news = new News("Java", "Hgaong", new Date(new java.util.Date().getTime()));
session.save(news);
// 5.提交事务
transaction.commit();
// 6.关闭Session
session.close();
// 7.关闭SessionFactory对象
sessionFactory.close();
}
}
结果
<!-- 若表不存在会自动建立对应的表,前提是hibernate.cfg.xml需这样配置 -->
<!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
控制台输出
Hibernate:
insert
into
NEWS
(NTITLE, NAUTHOR, CREATEDATE)
values
(?, ?, ?)