我的第一个hibernate小程序
今天学写了第一个hibernate小程序。
1 导入 hibernate需要的jar包, hibernate3.jar ,lib/required下面所有jar包,还有slf4j-nop-1.5.8.jar
2 在数据库里创建表 create table student(id int primary key,name varchar(20),age int);
3 创建要保存的类Student
package com.archer.model;
public class Student {
int id;
String name;
int age;
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;
}
}
4 创建student.hbm.xml
<hibernate-mapping package="com.archer.model">
<class name="Student"
table="student"
discriminator-value="S">
<id name="id"></id>
<property name="name"> </property>
<property name="age"> </property>
</class>
</hibernate-mapping>
5 创建hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/bbs</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping resource="com/archer/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6 运行之后检查数据库和后台打印