criteria完整配置使用经验1(包括hibernate.cfg.xml和hbm.xml)

废话不多说,直接开始吧!

一、 首先创建数据库表
我这里直接创建了一个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可忽略)
在这里插入图片描述
在搭建过程中,主要遇到了两个问题:

  1. 第一个就是关于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");
  1. 第二个就是关于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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值