我的配置文件是MyEclipse自动生成的,映射文件也是通过逆向工程生成的。
跪求大神解答
我的配置文件:
<?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">
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/studyhibnerate
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">Hacker</property>
<!-- 是否显示sql语句 -->
<property name="show_sql">true</property>
<mapping resource="Day1/User.hbm.xml" />
</session-factory>
我的映射文件:
<?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 ">
我的测试代码:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class CRUDTTest {
@Test
public void testSave() throws Exception{
User user = new User();
user.setName("王五");
user.setPassword("123456");
//实例化配置对象
Configuration configuration = new Configuration();
//解析配置文件开始;加载默认的hibernate.cfg.xml配置文件
configuration.configure();
//获取SessionFactory对象
SessionFactory sessionfactory = configuration.buildSessionFactory();//过时的方法
//通过会话工厂获取session对象
Session session = sessionfactory.openSession();//测试都用openSession
//开启事务
Transaction transaction = session.beginTransaction();
session.save(user);
//提交事务
transaction.commit();
//关闭资源
session.close();
sessionfactory.close();
}
}
我的目录结构: