本文中所使用的Hibernate版本为4.0,数据库使用的是mysql。
新建Maven工程并添加相关依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
创建Hibernate配置文件
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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/zhang/hibernate/entities/News.hbm.xml" />
</session-factory>
</hibernate-configuration>
创建一个实体类和映射文件
News.java
package com.zhang.hibernate.entities;
import java.util.Date;
public class News {
private Integer id;
private String title;
private String athour;
private Date date;
public News() {
}
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 getAthour() {
return athour;
}
public void setAthour(String athour) {
this.athour = athour;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", athour=" + athour + ", date=" + date + "]";
}
public News(String title, String athour, Date date) {
super();
this.title = title;
this.athour = athour;
this.date = date;
}
}
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 2020-3-20 11:20:34 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zhang.hibernate.entities.News" table="NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="athour" type="java.lang.String">
<column name="ATHOUR" />
</property>
<property name="date" type="java.util.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
创建测试类
package com.zhang.hibernate;
import java.util.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.After;
import org.junit.Before;
import org.junit.Test;
import com.zhang.hibernate.entities.News;
public class HibernateTest {
private SessionFactory factory;
private Session session;
private Transaction transaction;
@Before
public void init() {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
session = factory.openSession();
transaction = session.beginTransaction();
}
@After
public void after() {
transaction.commit();
session.close();
factory.close();
}
@Test
public void testConfiguration() {
News news = new News("标题", "作者", new Date());
session.save(news);
}
}
至此,我们已经完成了Hibernate应用的基本配置并成功实现了数据的插入。