不依賴 Web 容器、Ant/Maven、IDE 插件,純 Eclipse + JDK + MySQL + Hibernate JARs 即可跑通第一個持久化程式。
test)mysql-connector-java)CREATE TABLE news (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(400),
content TEXT,
time DATETIME
);
package cn.com.nick.hbm;
import java.util.Date;
public class News {
private int id;
private String title;
private String content;
private Date date;
// 標準 getter/setter
}
News.hbm.xml 放在同包目錄:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"https://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.com.nick.hbm.News" table="news">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="title" column="title"/>
<property name="content" column="content"/>
<property name="date" column="time" type="timestamp"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml 放在 src 根目錄:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="show_sql">true</property>
<mapping resource="cn/com/nick/hbm/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() { return sessionFactory; }
}
public class Test {
public static void main(String[] args) {
Test t = new Test();
// t.save(new News("測試標題", "測試內容", new Date()));
t.select();
// t.update();
// t.delete();
}
public void save(News n) {
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
s.save(n);
s.getTransaction().commit();
}
public void select() {
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
List<News> list = s.createQuery("FROM News").list(); // HQL 查物件非表
for (News n : list) {
System.out.println(n.getId() + " | " + n.getTitle() + " | " + n.getDate());
}
s.getTransaction().commit();
}
public void update() {
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
News n = (News) s.load(News.class, 1);
n.setTitle("更新後標題");
s.update(n);
s.getTransaction().commit();
}
public void delete() {
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
News n = (News) s.load(News.class, 6);
s.delete(n);
s.getTransaction().commit();
}
}
FROM News 非 FROM news)getCurrentSession() 需配置 hibernate.current_session_context_class=thread (Hibernate 3+ 預設)load() 回傳 Proxy,不存在拋異常;get() 即時查庫,不存在回 null