廢話不多說,直接開始吧!
一、 首先創建數據庫表
我這裏直接創建了一個product類,賦予 id; name; price;三個字段
二、 創建實體類
package ffcs.cn.peam.product.domain;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "product")
public class Product {
int id;
String name;
float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
三、Product.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="ffcs.cn.peam.product.domain">
<class name="Product" table="product">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="name" />
<property name="price" />
</class>
</hibernate-mapping>
四、 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">oracle.jdbc.driver.OracleDriver</property>
<!-- <property name="connection.url">jdbc\:oracle\:thin\:@localhost\:1521\:orcl</property> -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.characterEncoding">utf-8</property>
<property name="connection.username">scott</property>
<property name="connection.password">oracle</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="ffcs/cn/system/Product.hbm.xml" />
</session-factory>
</hibernate-configuration>
五、 代碼測試
@RequestMapping("test")
public String test() {
SessionFactory sessionFactory;
Configuration configuration = new Configuration();
configuration.configure("ffcs/cn/system/hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
Session s = sessionFactory.openSession();
s.beginTransaction();
Criteria c = s.createCriteria(Product.class);
List<Product> ps = c.list();
for (Product p : ps) {
System.out.println(p.getName());
}
s.getTransaction().commit();
s.close();
sessionFactory.close();
return null;
}
附上包結構(tool和function可忽略)
在搭建過程中,主要遇到了兩個問題:
- 第一個就是關於hibernate.cfg.xml路徑的問題
一般情況下,hibernate.cfg.xml默認放在src底下,調用時直接
SessionFactory sessionFactory;
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
但是我們項目的規範一般是不允許放在src底下的,而是要放在相對於的類似system的包中。這時候就需要手動爲hibernate.cfg.xml配置路徑,在這裏加上即可
configuration.configure("ffcs/cn/system/hibernate.cfg.xml");
- 第二個就是關於hibernate版本的問題
我搭建完項目以後,criteria可以正常使用了,但是有一個問題,就是在加載這段代碼塊的時候非常慢
configuration.configure("ffcs/cn/system/hibernate.cfg.xml");
一開始我以爲是這種配置本來就這麼慢,沒怎麼留意,後來測試的時候實在慢的我腦殼疼,所以去網上翻翻翻,後來一篇博客的評論點醒了我,會不會是配置申明的hibernate的版本和自己導入的包有問題,後來一找果然是這樣。
這個配置的申明應該要和hibeinate包的申明一樣,這個申明可以在項目的包配置找到:
Reference libraries>hibernate>org.hibernate》Hibernate-Configuration
同理,hbm.xml文件的配置在
Reference libraries>hibernate>org.hibernate》Hibernate-Mapping
這兩個配置完,在使用criteria蹭蹭蹭的飛起!!!
但是以上的配置較多,所以本人整理了關於criteria的集成配置有需要的可以看下。地址https://blog.csdn.net/weixin_40496191/article/details/105273090