第一个Hibernate小程序
Hibernate配置文件:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/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://127.0.0.1:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">123</property> <!-- JDBC connection pool (use the built-in) --><!-- 很少使用hibernate自带的连接池 --> <!--<property name="connection.pool_size">1</property>--> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management 使用SessionFactory.getCurrentSession()方法时使用 --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- hbm:hibernate mapping To ddl:datebase definition language--> <property name="hbm2ddl.auto">update</property> <!-- 注册:告诉hibernate Model文件在哪 --> <mapping resource="com/ibm/hibernate/model/Student.hbm.xml"/> <!-- 配饰AnnotationConfiguration时使用的 --> <!-- <mapping class="com.ibm.hibernate.model.Teacher"/> --> </session-factory> </hibernate-configuration>
模型类:
package com.ibm.hibernate.model;
public class Student {
private int id;
private String name;
private 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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "id:"+id+"\tname:"+name+"\tage:"+age;
}
}
模型类配置文件:
<?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="com.ibm.hibernate.model"> <class name="Student" table="STUDENT"> <id name="id" column="ID"></id> <property name="name" column="NAME"></property> <property name="age" column="AGE"></property> </class> </hibernate-mapping>
测试文件:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.ibm.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(001);
s.setName("Tom");
s.setAge(10);
Session session = null;
SessionFactory sf = null;
Transaction t = null;
//1、读取配置文件
Configuration cfg = new Configuration();
//2、通过SessionFactory 一个数据库对应一个SessionFactory
sf = cfg.configure().buildSessionFactory();
//3、创建session
//此处的session并不是web中的session
//session只有在用时,才建立concation,session还管理缓存。
//session用完后,必须关闭。
//session是非线程安全,一般是一个请求一个session.
session = sf.openSession();
//4、手动开启事务(可以在hibernate.cfg.xml配置文件中配置自动开启事务)
t = session.beginTransaction();
try {
//5、保存数据,此处的数据是保存对象,这就是hibernate操作对象的好处
//简单就一句话
session.save(s);
//6、事务成功提交/失败则回滚
t.commit();
} catch (HibernateException e) {
t.rollback();
e.printStackTrace();
}
//7、关闭session
session.close();
//8、关闭 sessionfactory
sf.close();
}
}
结果显示:
结果 写道
Hibernate: insert into STUDENT (NAME, AGE, ID) values (?, ?, ?)
数据库字段和模型字段相同,省略。