遵义网站建设找工作,域名反查网站,lamp环境做网站,自己做网站好还是购买网站好转载地址#xff1a;http://blog.csdn.net/aboy123/article/details/10222675
一、Hibernate概述
#xff08;一#xff09;什么是Hibernate#xff1f; hibernate核心内容是ORM#xff08;关系对象模型#xff09;。可以将对象自动的生成数据库中的信息#xff0c;使…
转载地址http://blog.csdn.net/aboy123/article/details/10222675
一、Hibernate概述
一什么是Hibernate hibernate核心内容是ORM关系对象模型。可以将对象自动的生成数据库中的信息使得开发更加的面向对象。这样作为程序员就可以使用面向对象的思想来操作数据库而不用关心繁琐的JDBC。所以Hibernate处于三层架构中的D层持久层。
二使用Hibernate的优点
1、Hibernate可以使用在Java的任何项目中不一定非要使用在Java web项目中。因为Hibernate不需要类似于tomact这些容器的支持可以直接通过一个main方法进行测试。
2、通过下面的实例可以发现使用Hibernate可以大大减少代码量。
3、由于使用了Hibernate代码中不涉及具体的JDBC语句所以就方便了代码的可移植性。
二、Hibernate开发的环境搭建
一Hibernate的环境搭建非常简单只需要引入Hibernate核心包单击下载以及Hibernate依赖包单击下载即可。二加入数据库驱动。下面的例子中主要是采用Mysql数据库来演示的所以在这里引入MysqL的JDBC驱动点击下载。三提供核心配置文件hibernate.cfg.xml文件在src文件夹下即可。其中的配置如下针对mysql[html] view plaincopyprint? !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数据库驱动 -- property namehibernate.connection.driver_classcom.mysql.jdbc.Driver/property !-- mysql数据库名称 -- property namehibernate.connection.urljdbc:mysql://localhost:3306/hibernate_first/property !-- 数据库的登陆用户名 -- property namehibernate.connection.usernameroot/property !-- 数据库的登陆密码 -- property namehibernate.connection.passwordroot/property !-- 方言为每一种数据库提供适配器方便转换 -- property namehibernate.dialectorg.hibernate.dialect.MySQLDialect/property /session-factory /hibernate-configuration !DOCTYPE hibernate-configuration PUBLIC-//Hibernate/Hibernate Configuration DTD 3.0//ENhttp://hibernate.sourceforge.net/hibernate-configuration-3.0.dtdhibernate-configurationsession-factory !-- mysql数据库驱动 --property namehibernate.connection.driver_classcom.mysql.jdbc.Driver/property!-- mysql数据库名称 --property namehibernate.connection.urljdbc:mysql://localhost:3306/hibernate_first/property!-- 数据库的登陆用户名 --property namehibernate.connection.usernameroot/property!-- 数据库的登陆密码 --property namehibernate.connection.passwordroot/property!-- 方言为每一种数据库提供适配器方便转换 --property namehibernate.dialectorg.hibernate.dialect.MySQLDialect/property/session-factory
/hibernate-configuration 三、HIbernate第一个实例 该实例的目录结构如下说明最后一个HIBERNATE3里面包含了所有的需要引用的jar包1、新建一个普通的java项目按照上面的步骤引入相关的jar包和配置文件2、建立User实体类[java] view plaincopyprint? import java.util.Date; public class User { private String id; private String username; private String password; private Date createTime; private Date expireTime; public String getId() { return id; } public void setId(String id) { this.id id; } public String getUsername() { return username; } public void setUsername(String userName) { this.username userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password password; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime createTime; } public Date getExpireTime() { return expireTime; } public void setExpireTime(Date expireTime) { this.expireTime expireTime; } } import java.util.Date;public class User {private String id;private String username;private String password;private Date createTime;private Date expireTime;public String getId() {return id;}public void setId(String id) {this.id id;}public String getUsername() {return username;}public void setUsername(String userName) {this.username userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime createTime;}public Date getExpireTime() {return expireTime;}public void setExpireTime(Date expireTime) {this.expireTime expireTime;}
} 2、提供User.hbm.xml文件完成实体类的映射[html] view plaincopyprint? ?xml version1.0? !DOCTYPE hibernate-mapping PUBLIC -//Hibernate/Hibernate Mapping DTD 3.0//EN http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd hibernate-mapping class namecom.example.hibernate.User id nameid generator classuuid/ /id property nameusername/ property namepassword/ property namecreateTime/ property nameexpireTime/ /class /hibernate-mapping ?xml version1.0?
!DOCTYPE hibernate-mapping PUBLIC -//Hibernate/Hibernate Mapping DTD 3.0//ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtdhibernate-mappingclass namecom.example.hibernate.Userid nameidgenerator classuuid//idproperty nameusername/property namepassword/property namecreateTime/property nameexpireTime//class
/hibernate-mapping其中的property标签是将要生成是数据库表中的字段在这里不用关心各个字段是什么类型的。因为Hibernate会根据上面的实体类中属性的类型来决定将来表中字段的类型3、配置hibernate.cfg.xml文件[html] view plaincopyprint? hibernate-configuration session-factory !-- mysql数据库驱动 -- property namehibernate.connection.driver_classcom.mysql.jdbc.Driver/property !-- mysql数据库名称 -- property namehibernate.connection.urljdbc:mysql://localhost:3306/hibernate_first/property !-- 数据库的登陆用户名 -- property namehibernate.connection.usernameroot/property !-- 数据库的登陆密码 -- property namehibernate.connection.passwordroot/property !-- 方言为每一种数据库提供适配器方便转换 -- property namehibernate.dialectorg.hibernate.dialect.MySQLDialect/property span stylecolor: rgb(255, 0, 0);mapping resourcecom/example/hibernate/User.hbm.xml//span /session-factory /hibernate-configuration hibernate-configurationsession-factory !-- mysql数据库驱动 --property namehibernate.connection.driver_classcom.mysql.jdbc.Driver/property!-- mysql数据库名称 --property namehibernate.connection.urljdbc:mysql://localhost:3306/hibernate_first/property!-- 数据库的登陆用户名 --property namehibernate.connection.usernameroot/property!-- 数据库的登陆密码 --property namehibernate.connection.passwordroot/property!-- 方言为每一种数据库提供适配器方便转换 --property namehibernate.dialectorg.hibernate.dialect.MySQLDialect/propertymapping resourcecom/example/hibernate/User.hbm.xml//session-factory
/hibernate-configuration注意必须是“/”而不能是“.”。4、生成表编写工具类ExoprtDB.java,将hbm生成ddl[java] view plaincopyprint? import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; /** * 将hbm生成ddl * author BCH * */ public class ExoprtDB { public static void main(String[] args) { //默认读取hibernate.cfg.xml文件 Configuration cfr new Configuration().configure(); SchemaExport export new SchemaExport(cfr); export.create(true, true); } } import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/*** 将hbm生成ddl* author BCH**/
public class ExoprtDB {public static void main(String[] args) {//默认读取hibernate.cfg.xml文件Configuration cfr new Configuration().configure();SchemaExport export new SchemaExport(cfr);export.create(true, true);}
}
到这里就可以生成User表了但是如果直接运行ExoprtDB.java文件是不能生成User表的。因为在mysql数据中还没有建立数据库Hibernate-first。所以在mysql控制台中通过create database hibernate-first; use hibernate-first;之后再执行ExoprtDB.java文件就可以生成表了。5、向表中添加数据[java] view plaincopyprint? import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Client { public static void main(String[] args) { //读取配置文件 Configuration cfg new Configuration().configure(); SessionFactory factory cfg.buildSessionFactory(); Session session null; try{ session factory.openSession(); //开启事务 session.beginTransaction(); User user new User(); user.setUsername(用户名); user.setPassword(123); user.setCreateTime(new Date()); user.setExpireTime(new Date()); session.save(user); //提交事务 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); }finally{ if(session ! null){ if(session.isOpen()){ //关闭session session.close(); } } } } } import java.util.Date;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Client {public static void main(String[] args) {//读取配置文件Configuration cfg new Configuration().configure();SessionFactory factory cfg.buildSessionFactory();Session session null;try{session factory.openSession();//开启事务session.beginTransaction();User user new User();user.setUsername(用户名);user.setPassword(123);user.setCreateTime(new Date());user.setExpireTime(new Date());session.save(user);//提交事务session.getTransaction().commit();}catch(Exception e){e.printStackTrace();//回滚事务session.getTransaction().rollback();}finally{if(session ! null){if(session.isOpen()){//关闭sessionsession.close();}}}}
} 执行该java文件就可以完成向表中增加数据了,效果如下四总结
通过上面的代码我们可以看出在代码中没有涉及到任何有关JDBC的代码作为开发人员只需要写好相应的实体类然后通过配置就可以实现了表的建立以及向表中实现数据的插入。在代码中有许多Hibernate的核心对象例如Configuration、SessionFactory、Session等等。这些内容将在以后介绍。一、什么是映射 在上一篇文章中 新手上路之Hibernate第一个Hibernate例子通过一个例子了解了什么是hibernate。而其中最重要的就是通过User.hbm.xml来配置映射。这里所说的映射就是对象关系映射将对象数据保存到数据库中同时可以将数据库数据读入对象中开发人员只对对象进行操作就可以完成对数据库数据的操作。 二、什么是基本映射 知道了什么是映射那么我们先来看一下最基本的映射关系——基本映射即根据表结构创建相应实体类 例如还是以MySQL数据库为例进行说明。有如下一张表 建立相应的实体类User [java] view plaincopy public class User { private String id; private String username; public String getId() { return id; } public void setId(String id) { this.id id; } public String getUsername() { return username; } public void setUsername(String userName) { this.username userName; } } 对于Hibernate来说最重要的就是配置文件即还需要一个User.hbm.xml文件 [html] view plaincopy hibernate-mapping class namecom.example.hibernate.User tablet_user id nameid generator classuuid/ /id property nameusername/ /class /hibernate-mapping 其他部分的代码参照上一篇文章即可。 三、建立User实体类的一些原则 1、实现无参的默认的构造函数 2、提供一个标识 3、建议不要使用fianl修饰实体类 4、建议为实体类生成setter和getter方法 对于第3条建议如果使用了fianl关键字那么延迟加载load将会无效。具体含义将会在下一篇文章中给出实例。 四、HIbernate主键生成策略 在上面User.hbm.xml配置文件中有一个标签generator。它表示的是主键生成策略。主键生成策略必须配置用来为该持久化类的实例生成唯一的标识。它有如下几种策略 uuid 用一个128-bit的UUID算法生成字符串类型的标识符这在一个网络中是唯一的使用了IP地址UUID被编码为一个32位16进制数字的字符串它的生成是由Hibernate生成一般不会重复UUID包含IP地址JVM的启动时间系统时间和一个计算器值。 identityMySql数据库中自增长 SequenceOracle数据库自增长 native根据底层数据库能力选择identity、sequence中的一个。 assigned手动分配主键.generator没有指定时的默认生成策略 foreign使用另外一个相关联的对象的标示符。通常和one-to-one配合使用
五、总结
概括一下Hibernate的基本映射就是实体类——对应了——数据库中表、实体类中的属性——对应了——表中字段。