搭建环境:
jdk1.6 + hibernate 3
hibernate3 的下载地址:http://nchc.dl.sourceforge.net/project/hibernate/hibernate3/3.5.5-Final/hibernate-distribution-3.5.5-Final-dist.zip
附件中书包含所必需的jar包
1.导入hibernate相应的jar包:
我当时下载的是hibernate-distribution-3.5.5-Final-dist,但只选用里面的必选required包之后会报错,因为少了一个slf4j-nop-1.5.2.jar这个包,所以单独下载了这个包
2,写hibernate配置文件 hibernate.cfg.xml(此文件直接放在src下面):
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="test">
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test1</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping resource="mapping/Member.hbm.xml"/>
</session-factory>
</hibernate-configuration>
说明:
hibernate.hbm2ddl.auto:自动创建表,当数据库不存在要操作的表时,将会自动进行创建,所以在执行完第一次hibernate操作后便可以注释这个属性
show_sql:打印执行的hql语句
format_sql:将show_sql打印出来的sql语句进行格式化
connection.useUnicode:使用编码
connection.characterEncoding:设置编码格式
(connection.useUnicode,connection.characterEncoding当自己在乱码中挣扎时,这两句相当重要)
hibernate.dialect:设置hibernate方言
mapping:对象与数据库表的映射文件
3.编写一个Member类:
public class Member implements Serializable{
private Integer mid;
private String name;
private Character sex;
public Member() {
}
public Member(Integer mid, String name, Character sex) {
this.mid = mid;
this.name = name;
this.sex = sex;
}
public Integer getMid() {
return mid;
}
public void setMid(Integer mid) {
this.mid = mid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Character getSex() {
return sex;
}
public void setSex(Character sex) {
this.sex = sex;
}
}
3.编写Member类与数据库表的映射文件 Member.hbm.xml(此文件的地址与hibernate.cfg.xml中mapping路径要相同):
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="mapping">
<class name="mapping.Member" table="member">
<id name="mid">
<generator class="identity"></generator>
</id>
<property name="name"></property>
<property name="sex"></property>
</class>
</hibernate-mapping>
4.创建Mysql数据库,数据库名为test1(仅仅只创建数据库即可)
5.编写测试类 HibernateTest
public class HibernateTest {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
Member member = new Member();
member.setName("潘明志2");
member.setSex('男');
session.save(member);
t.commit();
session.close();
}
}
运行后便会看到控制台会打印出下面的语句
Hibernate:
insert
into
member
(name, sex)
values
(?, ?)
运行成功后便可以注释hibernate.cfg.xml中hibernate.hbm2ddl.auto这个属性了,如果不注释,那数据库的的member表中永远就只有一条数据.