具体代码就是以下三个主要配置文件,求大佬瞄一眼哪里出错了。
错误提示只有上面几句话
Mytest.java
package com.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class Mytest {
@Test
public void demo() {
try {
Configuration cfg = new Configuration().configure();
System.out.println("t1");
SessionFactory sessionFactory= cfg.buildSessionFactory(); // 根据配置生成Session工厂
System.out.println("t2");
Session session = sessionFactory.openSession(); // 打开一个新的Session
Transaction tx = session.beginTransaction(); // 开启事务
User user=new User();
user.setid(2);
user.setName("王一一");
session.save(user);
tx.commit();//提交事务
session.close();//资源释放
}
catch(Exception e)
{System.out.println("t3");}
}
}
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate">
<class name="User" table="stu">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<!-- 其他属性映射 -->
<property name="name" type="string" column="name" />
</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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="myConfig">
<!-- 1.方言(连接的数据库类型) -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- 2. 配置数据库信息 -->
<property name="connection.url">jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=666</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver"</property>
<property name="connection.username">sa</property>
<property name="connection.password">123</property>
<!-- 3. 其他配置-->
<!-- 显示生成的SQL语句 以日志的形式跟踪数据sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 4. 导入映射文件 -->
<mapping resource="com/hibernate/User.hbm.xml" />
</session-factory>
</hibernate-configuration>