(初學Hibernate)Hibernate對資料庫的增刪改查)
阿新 • • 發佈:2018-12-27
(初學Hibernate)Hibernate對資料庫的增刪改查)Hibernate所需jar包可以在Hibernate官網下載
最後倒計時結束後下載框才會提示出來
現在開始放程式碼了
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="connection.useUnicode">true</property> <property name="connection.characterEncoding">UTF-8</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 設定地址 --> <property name="connection.url">jdbc:mysql://localhost:3306/task</property> <!-- 使用者名稱 --> <property name="connection.username">root</property> <!-- 密碼 --> <property name="connection.password">root</property> <!-- 驅動 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 顯示sql語句 --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <!-- 關聯對映檔案 --> <mapping resource="com/entity/News.hbm.xml" /> </session-factory> </hibernate-configuration>
這裡需要注意關聯對映檔案以及設定的地址中對應的資料庫名稱
建立實體類News.java
package com.entity; public class News { private int id; private String title; private String content; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
配置對映檔案News.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.entity.News" table="news"> <id name="id" column="id" type="integer"> <generator class="native"></generator> </id> <property name="title" column="title" type="string" /> <property name="content" column="content" type="string" /> </class> </hibernate-mapping>
類測試NewsDao
package com.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.entity.News;
public class NewsDao {
static Configuration cfg = null; // 讀取配置檔案的物件
static SessionFactory sf = null; // 建立會話工廠的物件
static Session session = null;// 開啟session
static Transaction t = null; // 開啟事務
static {
cfg = new Configuration().configure();
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sf = cfg.buildSessionFactory(ssr);
session = sf.openSession();
t = session.beginTransaction();
}
// 新增資料
public static void insert(News n) {
try {
session.save(n);
System.out.println("insert datas:success!");
} catch (Exception e) {
// TODO: handle exception
System.out.println("insert datas:error!");
}
end();
}
// 修改資料
public static void update(News n){
try {
session.update(n);
System.out.println("update datas:success!");
} catch (Exception e) {
// TODO: handle exception
System.out.println("update datas:error!");
}
end();
}
//根據id刪除資料
public static void delete(News n){
try {
session.delete(n);
System.out.println("delete datas:success!");
} catch (Exception e) {
// TODO: handle exception
System.out.println("delete datas:error!");
}
end();
}
//根據id查詢資料
public static News select(int id){
News n;
try {
n = (News) session.get(News.class, id);
return n;
} catch (Exception e) {
System.out.println("delete datas:error!");
return n = null;
} finally {
end();
}
}
// 關閉資源
public static void end(){
t.commit();
sf.close();
}
}
新人第一次釋出,不好的地方希望諒解