①創建持久化 Java 類
import java.sql.Blob;
import java.util.Date;
public class News {
private Integer id; //field
private String title;
private String author;
private String desc;
//使用 title + "," + content 可以來描述當前的 News 記錄.
//即 title + "," + content 可以作爲 News 的 desc 屬性值
private String content;
private Blob picture;
public Blob getPicture() {
return picture;
}
public void setPicture(Blob picture) {
this.picture = picture;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
private Date date;
public Integer getId() { //property
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() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author
+ ", 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">
<hibernate-mapping package="com.atguigu.hibernate.helloworld">
<class name="News" table="NEWS" dynamic-insert="true">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主鍵的生成方式, native: 使用數據庫本地方式 -->
<generator class="native" />
</id>
<property name="title" not-null="true" unique="true"
index="news_index" length="50"
type="java.lang.String" column="TITLE" >
</property>
<property name="author" type="java.lang.String"
index="news_index">
<column name="AUTHOR" />
</property>
<property name="date" type="date">
<column name="DATE" />
</property>
<property name="desc"
formula="(SELECT concat(title, ',', author) FROM NEWS n WHERE n.id = id)"></property>
<property name="content">
<column name="CONTENT" sql-type="text"></column>
</property>
<property name="picture" column="PICTURE" type="blob"></property>
</class>
</hibernate-mapping>
③創建 Hibernate配置文件
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"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate1</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/atguigu/hibernate/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
④Helloworld的測試用例
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() {
System.out.println("test...");
//1. 創建一個 SessionFactory 對象
SessionFactory sessionFactory = null;
//1). 創建 Configuration 對象: 對應 hibernate 的基本配置信息和 對象關係映射信息
Configuration configuration = new Configuration().configure();
//4.0 之前這樣創建
// sessionFactory = configuration.buildSessionFactory();
//2). 創建一個 ServiceRegistry 對象: hibernate 4.x 新添加的對象
//hibernate 的任何配置和服務都需要在該對象中註冊後纔能有效.
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
//3).
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2. 創建一個 Session 對象
Session session = sessionFactory.openSession();
//3. 開啓事務
Transaction transaction = session.beginTransaction();
//4. 執行保存操作
News news = new News("Java12345", "ATGUIGU", new Date(new java.util.Date().getTime()));
session.save(news);
//5. 提交事務
transaction.commit();
//6. 關閉 Session
session.close();
//7. 關閉 SessionFactory 對象
sessionFactory.close();
}
}
⑤查看數據庫是否創建成功