一.准备工作
1.在eclipse安装好hibernate tools以便框架的开发
2.下载hibernate后,创建一个名为hibernate_001的java项目,导入hibernate中lib/required的所有jar包,同时导入junit和mysql的jar包
3.准备工作完成后可以开始编写hibernate
二.编写hibernate例子
1.新建一个hibernate配置文件(cfg.xml)
2.编写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>
<!-- hibernate.connection.driver_class : 连接数据库的驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- hibernate.connection.username : 连接数据库的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- hibernate.connection.password : 连接数据库的密码 -->
<property name="hibernate.connection.password">123</property>
<!-- hibernate.connection.url : 连接数据库的地址,路径 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
<!-- 数据库方言配置 org.hibernate.dialect.MySQLDialect (选择最短的)-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->
<property name="show_sql">true</property>
<!-- format_sql: 打印sql语句前,会将sql语句先格式化 -->
<property name="format_sql">true</property>
<!-- 指定hibernate启动的时候自动创建表结构creat update -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
3.创建一个持久化类Students.java
import java.util.Date;
public class Students {
private int sid;
private String sname;
private String gendar;
private Date birthday;
private String address;
public Students(){
super();
}
publ