使用Junit测试Hibernate.hbm.xml报错。
第一句报错:Configuration configuration=new Configuration().configure("/hibernate.cfg.xml");
下面是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.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123123</property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印sql (可选)-->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql(可选) -->
<property name="hibernate.dialect">true</property>
<mapping resource="com/demo1/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
true 删除这一行
默认会加载hibernate.cfg.xml;Configuration config = new Configuration().configure(); 试试
true 删除这一行
true
true
name="hibernate.dialect" true
测试源码:
package com.demo1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateDemo1 {
//保护客户端的案例
@Test
public void demo1(){
try{
//1.加载Hibernate核心配置文件
System.out.println("111");
Configuration configuration=new Configuration().configure("/hibernate.cfg.xml");
//2.创建一个SessionFactory对象,类似于JDBC中的连接池
System.out.println("222");
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3.通过SessionFactory获取到Session对象,类似于JDBCConnection
Session session = sessionFactory.openSession();
//4.手动开启事务
Transaction transaction = session.beginTransaction();
//5.编写代码
Customer customer = new Customer();
customer.setCst_name("张三");
session.save(customer);
//6.事务提交
transaction.commit();
//7.释放资源
session.close();
}catch(Exception e){e.printStackTrace();}
}
}