1、Hibernate简介
· Hibernate是一种ORM框架技术,而ORM就是关系对象映射,即Object-Relationship-Mapping。使得程序员可以使用面向对象的思想来处理数据,尽量减少了底层与数据库进行交互。
· Hibernate对Java的JDBC进行了轻量级的封装,即底层是通过Java的JDBC实现的。
·传统的JDBC,使得项目中在操作数据库的同时出现大量的代码冗余,所以采用各种框架技术可以降低程序的代码冗余度,便于维护和修改。
2、Hiberante配置步骤
·导入所需要的jar包,hibernate版本的不同,所需要的核心jar包也是不同的。一般在下载下来的re'quried文件夹中即为所必需的jar包。
·配置hibernate.cfg.xml
首先是导入所需要的模板文件,这个文件在hibernate的核心家jar包中可以找到(org.hibernate)
然后根据模板提供的标签定义 数据库连接信息、实体类映射信息以及其他的一些配置信息三个部分,
<?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>
<property name="connection.username">hibernate</property>
<property name="connection.password">hibernate</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="cn/bean/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
·编写实体类
package cn.bean;
//学生类
public class Student {
//id
private int id;
//姓名
private String name;
//性别
private String sex;
//密码
private String password;
public Student() {
}
public Student(int id, String name, String sex, String password) {
// super();
this.id = id;
this.name = name;
this.sex = sex;
this.password = password;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
·编写实体类映射文件Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.bean.Student" table="student" schema="hibernate" lazy="false">
<id name="id" type="int">
<column name="ID"/>
<generator class="sequence">
<param name="sequence">seq_Student</param>
</generator>
</id>
<property name="name" type="string">
<column name="NAME"/>
</property>
<property name="password" type="string">
<column name="PASSWORD"/>
</property>
<property name="sex" type="string">
<column name="SEX"/>
</property>
</class>
</hibernate-mapping>
·编写测试类
package cn.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.bean.Student;
public class Demo {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init() {
Configuration config = new Configuration().configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(sr);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@Test
public void testHibernateCfg() {
Student student = new Student();
// student.setId(1);
student.setName("王老五");
student.setSex("男");
student.setPassword("123456");
session.save(student);
transaction.commit();
}
@After
public void destory() {
session.close();
sessionFactory.close();
}
}
测试结果
我们在数据库中查询一下看是否真正的将数据插入到数据库中了
我们发现,已经成功添加到数据库中,说明的我们的hibernate框架已经测试成功。
(本文章只作简单的配置,至于hibernate的高级配置,请关注我的博客,谢谢!)