Hibernate简述及入门实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate简述及入门实例相关的知识,希望对你有一定的参考价值。
一.Hibernate简述
总的概括,Hibernate是一个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文件1 package test.hibernate.pro; 2 3 public class Student { 4 int id; 5 int age; 6 String name; 7 int classid; 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public int getAge() { 15 return age; 16 } 17 public void setAge(int age) { 18 this.age = age; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public int getClassid() { 27 return classid; 28 } 29 public void setClassid(int classid) { 30 this.classid = classid; 31 } 32 }
3.使用MysqlGUI工具或cmd窗口建表(也可使用hibernate的SchemaExport方法自动生成表结构)。这里使用cmd窗口,进入mysql文件夹下lib目录输入mysql –hlocalhost –u root –p,之后Enter Password进入数据库create建表
4.在pro包下建*.hbm.xml文件导入头文件,完成object与表结构的映射关系
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="test.hibernate.pro.Student" table="student"> 7 <!-- name值为对象属性名,column值为相应列名 ,相互对应 --> 8 <id name="id" column="stu_id"> 9 <!-- 主键生成策略 --> 10 <generator class="native"></generator> 11 </id> 12 <!-- 实体类属性 --> 13 <property name="age" column="stu_age"></property> 14 <property name="name" column="stu_name"></property> 15 <property name="classid" column="stu_classid"></property> 16 </class> 17 </hibernate-mapping>
5.配置hibernate.cfg.xml文件
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <!-- 配置资源信息 --> 9 <!-- mysql默认端口号为3306 --> 10 <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">123456</property> 15 <property name="format_sql">true</property> 16 <property name="show_sql">true</property> 17 18 <!-- 数据库方言 --> 19 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 20 21 <!-- 注册调用*。hbm.xml文件 --> 22 <mapping resource="test/hibernate/pro/Student.hbm.xml"></mapping> 23 </session-factory> 24 </hibernate-configuration>
6.运行test包下test.java测试
1 package test.hibernate.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 8 import test.hibernate.pro.Student; 9 10 public class test { 11 public static void main(String[] args) { 12 Configuration cfg = new Configuration().configure(); 13 14 // hibernate3.5写法 15 SessionFactory sf = cfg.buildSessionFactory(); 16 // hibernate4.0 -hibernate4.2 写法 17 // ServiceRegistry reg = new 18 // ServiceRegistryBuilder().applySettings(cfg.getProperties()) 19 // .buildServiceRegistry(); 20 // hibernate4.3 写法 21 // ServiceRegistry reg = new 22 // StandardServiceRegistryBuilder().applySettings(cfg.getProperties()) 23 // .build(); 24 // SessionFactory sf = cfg.buildSessionFactory(reg); 25 26 Session session = null; 27 Transaction ta = null; 28 try { 29 session = sf.openSession(); 30 ta = session.beginTransaction(); 31 32 Student stu = new Student(); 33 stu.setAge(17); 34 stu.setName("Neo"); 35 stu.setClassid(2); 36 session.save(stu); 37 38 ta.commit(); 39 } catch (Exception e) { 40 e.printStackTrace(); 41 if (ta != null) { 42 ta.rollback(); 43 } 44 } finally { 45 if (session != null && session.isOpen()) { 46 session.close(); 47 } 48 } 49 } 50 }
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层类
1 package test.hibernate.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import test.hibernate.factory.SF; 7 import test.hibernate.pro.Student; 8 9 public class StudentDao { 10 Session session = null; 11 Transaction ta = null; 12 13 public void addStudent(Student stu) { 14 try { 15 session = SF.getSession(); 16 ta = session.beginTransaction(); 17 session.save(stu); 18 ta.commit(); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 if (ta != null) 22 ta.rollback(); 23 } finally { 24 if (session != null) 25 session.close(); 26 } 27 } 28 }
5.test包中创建test测试类测试
1 package test.hibernate.test; 2 3 import test.hibernate.dao.StudentDao; 4 import test.hibernate.pro.Student; 5 6 public class Test { 7 public static void main(String[] args) { 8 StudentDao sd = new StudentDao(); 9 10 Student stu = new Student(); 11 stu.setStuAge(19); 12 stu.setStuName("Mike"); 13 stu.setStuClassid(1); 14 15 sd.addStudent(stu); 16 } 17 }
总结,hibernate实现了orm对象关系映射,以对象形式对数据库数据进行增删改查的操作从而提升了编程的效率。
以上是关于Hibernate简述及入门实例的主要内容,如果未能解决你的问题,请参考以下文章