要毕业了,什么也不懂,最近想学三大框架,刚学完STUTRS2,昨天开始学hibernate,教程第一讲就弄了两天还停留在下面这个问题,急死人 了。教程上说的是hibernate3.2,但是我现在到官网下了个4.2的版本,实在是整天两天没成功运行 ,后面的教程就一直挡着了,这个是照着教程上看的写的实体类Student,
package com.luhy.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
public Student(){
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试类:
package com.luhy.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import com.luhy.hibernate.model.Student;
public class Test {
public static void main(String[] args) {
Student st = new Student();
st.setId(2);
st.setName("学生");
st.setAge(20);
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.configure().buildSessionFactory(serviceRegistry);
Session session = sf.openSession();
session.beginTransaction(); //事务开启
session.save(st); //保存对象
session.getTransaction().commit();
session.close();
sf.close();
}
}
Student.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="org.hibernate.tutorial.domain">
<class name="Student" table="student">
<id name="id" column="id"/>
<property name="name" column="name"/>
<property name="age" column="age"/>
</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>
<!-- Database connection settings -->
<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">lu5896848</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property>-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>-->