Hibernate 简述及入门实例
程序员文章站
2024-02-24 23:28:16
...
一.Hibernate简述
总的概括,Hibernates是一个ORM的轻量级持久层框架,解决了对象和关系数据库中表的不匹配问题(阻抗不匹配)以及拥有开发代码不用去继承hibernate类或接口的优势(无侵入性)。hibernate框架实现使得开发人员可以避免反复地编写javajdbc部分代码,应用面向对象的思维操作关系型数据库。
二.使用myeclipse创建hibernate实例两种方法(以hibernate3.5.2及mysql为例)
a)手动编写hibernate.cfg.xml及*.hbm.xml文件
1.新建java项目,导入相应所需jar包(lib\required文件夹内所有jar包+mysql相应jar包+hibernate3.jar包)到java项目下新建的lib文件夹中,选中所有jar右键Build Path->Add Build Path,在src文件夹下新建一个pro包test包和hibernate.cfg.xml文件
2.在pro包下建Student实体类(PS:myeclipse自动生成get/set方法:Ctrl+Shift+s -> Generate Getters and Setters)
package test.hibernate.pro;
public class Student {
int id;
int age;
String name;
int classid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getClassid() {
return classid;
}
public void setClassid(int classid) {
this.classid = classid;
}
}
3.使用MysqlGUI工具或cmd窗口建表(也可使用hibernate的SchemaExport方法自动生成表结构)。这里使用cmd窗口,进入mysql文件夹下lib目录输入mysql –hlocalhost –u root –p,之后Enter Password进入数据库create建表
4.在pro包下建*.hbm.xml文件导入头文件,完成object与表结构的映射关系
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test.hibernate.pro.Student" table="student">
<!-- name值为对象属性名,column值为相应列名 ,相互对应 -->
<id name="id" column="stu_id">
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- 实体类属性 -->
<property name="age" column="stu_age"></property>
<property name="name" column="stu_name"></property>
<property name="classid" column="stu_classid"></property>
</class>
</hibernate-mapping>
5.配置hibernate.cfg.xml文件
<?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>
<!-- 配置资源信息 -->
<!-- mysql默认端口号为3306 -->
<property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 注册调用*。hbm.xml文件 -->
<mapping resource="test/hibernate/pro/Student.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
6.运行test包下test.java测试
package test.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import test.hibernate.pro.Student;
public class test {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
// hibernate3.5写法
SessionFactory sf = cfg.buildSessionFactory();
// hibernate4.0 -hibernate4.2 写法
// ServiceRegistry reg = new
// ServiceRegistryBuilder().applySettings(cfg.getProperties())
// .buildServiceRegistry();
// hibernate4.3 写法
// ServiceRegistry reg = new
// StandardServiceRegistryBuilder().applySettings(cfg.getProperties())
// .build();
// SessionFactory sf = cfg.buildSessionFactory(reg);
Session session = null;
Transaction ta = null;
try {
session = sf.openSession();
ta = session.beginTransaction();
Student stu = new Student();
stu.setAge(17);
stu.setName("Neo");
stu.setClassid(2);
session.save(stu);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
if (ta != null) {
ta.rollback();
}
} finally {
if (session != null && session.isOpen()) {
session.close();
}
}
}
}
b)使用myeclipse自动生成hibernate.cfg.xml及*.hbm.xml文件
1.新建java web项目,src目录下建pro,factory,dao,test包2.打开任务栏window -> Show View -> DB Browser,导入mysqljar包新建mysql jdbc Driver。右键项目 -> MyEclipse -> ProjectFacets -> Install Hibernate Facet创建hibernate.cfg.xml及SessionFactory文件命名为SF.java
3.DB Browser中右键表结构Hibernate Reverse Engineering反向生成实体类到pro包,选项三个勾一个点,选择主键增长策略,实例中选择native(默认为native)
4.dao包中创建dao层类
package test.hibernate.dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import test.hibernate.factory.SF;
import test.hibernate.pro.Student;
public class StudentDao {
Session session = null;
Transaction ta = null;
public void addStudent(Student stu) {
try {
session = SF.getSession();
ta = session.beginTransaction();
session.save(stu);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
if (ta != null)
ta.rollback();
} finally {
if (session != null)
session.close();
}
}
}
5.test包中创建test测试类测试
package test.hibernate.test;
import test.hibernate.dao.StudentDao;
import test.hibernate.pro.Student;
public class Test {
public static void main(String[] args) {
StudentDao sd = new StudentDao();
Student stu = new Student();
stu.setStuAge(19);
stu.setStuName("Mike");
stu.setStuClassid(1);
sd.addStudent(stu);
}
}
总结,hibernate实现了orm对象关系映射,以对象形式对数据库数据进行增删改查的操作从而提升了编程的效率。
上一篇: UART串口通信