hibernate 環境的配置步驟:
- 加入 hibernate 所需的 jar 包,并将這些 jar 添加到 project 中,如圖:
- hibernate.cfg.xml 的建立。hibernate 的 hibernate.cfg.xml 配置檔案預設在 project/src 目錄下,如圖:
- hibernate.cfg.xml 中的内容如下(當然,這是從 hibernate 的參考文檔中拷貝過來的,還未修改):
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
至此,hibernate 環境配置完成。
hibernate 與 mysql 內建:
- 添加支援 mysql 驅動的 jar 包,如下圖:
2. 修改 hibernate.cfg.xml
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
修改為 mysql 的連接配接方式:
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
至此,hibernate + mysql 環境搭建完成(當然,要在 mysql 資料庫下建立一個名為 hibernate 的資料庫)。
下面我們測試一下:
- 建立以下包和類 ,并修改 hibernate.cfg.xml
- 貼下代碼:
2.1 Student.java
package com.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
public int getAge() {
return age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
2.2 Student.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.hibernate.model">
<class name="Student">
<id name="id"/>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
2.3 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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) hibernate連接配接池-->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/hibernate/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.4 StudentTest
package com.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(18);
//cfd.configure(),configure()不寫參數預設查找src目錄下的hibernate.cfg.xml
Configuration cfd = new Configuration();
//buildSessionFactory()産生一個SessionFactory工廠
SessionFactory sf = cfd.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
運作下 StudentTest 類中的 main 方法,則在名為 hibernate 資料庫中新建立一個名為 student 的表并添加資料,如下圖:
---------------------------------------------------------------------------------------------------------------------
注:
hibernate 使用操作資料庫的步驟:
- 通過 Configuration 類的 configure() 查找配置檔案 hibernate.cfg.xml 。
- 擷取配置檔案後通過 buildSessionFactory() 來建立 SessionFactory 。
- 通過 SessionFactory 的 openSession() 建立一個 Session 。
- 通過 Session 的 beginTransaction() 開啟一個事物 。
- 調用 Session 的 save() 、update() 、delete() 等方法執行資料庫操作 。
- 通過 Session 的 getTransaction() 擷取目前正在操作的事物,再通過把這個事物的 commit() 将資料更新至資料庫 。
- 最後關掉 Session 、 SessionFactory 。
Configuration cfd = new Configuration();
SessionFactory sf = cfd.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
//session.save(s); 調用session的增删改查方法
session.getTransaction().commit();
session.close();
sf.close();
源碼下載下傳
end。
版權聲明:本文為CSDN部落客「weixin_33859231」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:https://blog.csdn.net/weixin_33859231/article/details/92131185