1.新建java项目bibernate_001,引入jar包,本例使用的是hibernate-3.3.2,包括hibernate的jar包,slf4j包和jdbc包
其中hibernate的jar包包括核心包以及必须的jar包
2.在数据库创建表student
-- Create table create table STUDENT ( id NUMBER not null, name VARCHAR2(20), age NUMBER ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table STUDENT add constraint PK primary key (ID) using index tablespace USERS pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited );3.创建hibernate配置文件hibernate.cfg.xml,位于src目录下
<?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">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <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> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/baosight/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>4.新建实体类Student
package com.baosight.model; /** * <p>Title: </p> * <p>Description:Student </p> * <p>Company: </p> * @author yuan * @date 2016-4-10 下午12:32:46*/ 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; } }5.新建实体类对数据库表的映射文件Student.hbm.xml
<?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.baosight.model"> <class name="Student"> <id name="id" ></id> <property name="name"></property> <property name="age"></property> </class> </hibernate-mapping>6.新建测试类TestStu对数据新增进行测试
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.baosight.model.Student; /** * <p>Title: </p> * <p>Description:TestStu </p> * <p>Company: </p> * @author yuan * @date 2016-4-10 下午3:16:01*/ public class TestStu { /** * @Title: main * @Description: TODO * @param args * @return void * @throws */ public static void main(String[] args) { // TODO Auto-generated method stub //学生测试类 Student s = new Student(); s.setId(1); s.setName("s1"); s.setAge(20); //读取配置文件 Configuration cfg = new Configuration(); //得到session工厂 SessionFactory sf = cfg.configure().buildSessionFactory(); //得到session Session session = sf.openSession(); //开启事务 session.beginTransaction(); //session执行save session.save(s); //事务提交 session.getTransaction().commit(); //关闭session session.close(); //关闭session工厂 sf.close(); } }7.查询数据库,显示数据已经成功插入
查询结果:1 1 s1 20
以上即为一个最简单的使用hibernate进行插入操作的demo,hibernate最大的优点在于对操作数据库的封装,不用出现sql语句,相关配置均在实体类和数据库表的映射文件中,后面复杂的操作可能配置会相应的复杂,不过,hibernate提供了annotation,可以极大地简化这一过程。