废话不多说,直接开始吧!
一、 首先创建数据库表
我这里直接创建了一个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