zoukankan      html  css  js  c++  java
  • Hibernate的映射机制是怎样?

    Hibernate的映射机制
    对象关系映射(Object Relation Mapping(ORM))是一种为了解决面向对象与面向关系数据库互不匹配现象的技术,简而言之ORM是通过使用描述对象之间映射的元数据,将java程序中的对象自动持久化到关系数据库中,这种映射机制从本质上来说其实就是将数据从一种形式转化为另一种形式
    Hibernate的基本映射数据类型
    Hibernate的基本映射数据类型是java基本类型与标准SQL类型相互转化的桥梁,其关系
    java类型----------->Hibernate的映射数据类型----------->标准SQL类型
    通过Hibernate的基本映射数据类型可以非常方便地将数据从一种形式转化成另一个形式,完成高质量的ORM任务
    ...
    <!--
    name:这是持久化类User.java中一个String类型的属性
    column:这是数据库表user中一个类型为char(20)的字段
    type:这是Hibernate映射类型
    -->
    <property name="password"
    column="password"
    type="string"
    ...
    >

    datamap数据表
    -----------------------------------------------------------------------------------------------
    字段名称 数据类型 主键 自增 允许为空 描述
    ID int(4) 是 增1 否 ID号
    MYBOOLEAN bit(1) 逻辑型数据
    MYINT int(5) 整型数据
    MYLONG bigint(11) 长整型数据
    MYFLOAT float(8,2) 单精度浮点型数据
    MYDOUBLE double(10,2) 双精度浮点型数据
    MYDECIMAL decimal(10,2) Decimal型数据
    MYSTRING varchar(100) 字符串数据
    MYTEXT text Text型数据
    MYDATE date Date型数据
    MYTIME time Time型数据
    MYDATETIME datetime Datetime型数据
    MYTIMESTAMP timestamp Timestamp型数据
    MYBINARY varbinary(10240) Binary型数据
    MYBLOB longblob Blob型数据
    ------------------------------------------------------------------------------------------------
    其对应的持久化类Datamap.java
    com.hephec.orm
    import java.io.Serializable
    public class Datamap implements Serializable{
    private int hashValue=0;
    private Integer id;
    private Boolean myboolean;
    private Integer myint;
    private Long mylong;
    private Float myfloat;
    private Double mydouble;
    private BigDecimal mydecimal;
    private String mytext;
    private String mytext;
    private Date mydatel;
    private Time mytime;
    private Date mydatetime;
    private Timestamp mytimestamp;
    private byte[] mybinary;
    private Blob myblob;
    private Datamap(){}//构造方法
    //getter...setter省略
    }
    datamap表与Datamap类的ORM映射文件Datamap.hbm.xml
    <hibernate-mapping package="com.orm">
    <class name="Datamap" table="datamap">
    <id name="id" column="ID" type="Integer">
    <generator class="identity"/>
    </id>
    <property name="myboolean" column="MYBOOLEAN" type="boolean"/>
    <property name="myint" column="MYINT" type="integer"/>
    <property name="mylong" column="MYLONG" type="long"/>
    <property name="myfloat" column="MYFLOAT" type="float"/>
    <property name="mydouble" column="MYDOUBLE" type="double"/>
    <property name="mydecimal" column="MYDECIMAL" type="decimal"/>
    <property name="mystring" column="MYSTRING" type="string"/>
    <property name="mytext" column="MYTEXT" type="string"/>
    <property name="mydate" column="MYDATE" type="date"/>
    <property name="mytime" column="MYTIME" type="time"/>
    <property name="mydatetime" column="MYDATETIME" type="timestamp"/>
    <property name="mytimestamp" column="MYTEMESTAMP" type="timestamp"/>
    <property name="mybinary" column="MYBINARY" type="binary"/>
    <property name="myblob" column="MYBLOB" type="blob"/>
    </class>
    <hibernate-mapping/>

    (1)创建数据访问DAO接口TestDAO.java
    package com.DAO;
    import com.ORM.*;
    public interface TestDAO{
    public void addDatamap(Datamap datamap);
    public Datamap loadDatamap(Integer id);
    public void delDatamap(Integer id);
    }

    (2)创建数据访问DAO接口实现TestDAOImpl.java
    package com.DAO;
    import com.ORM.*;
    import org.hibernate.*;
    public class TestDAOImpl implements TestDAO{
    public void addDatamap(Datamap datamap){
    Session session=MySessionFactory.currentSession();
    Transaction ts=null;
    try{
    ts=session.beginTransaction();
    session.save(datamap);
    ts.commit();
    }catch(Exception e){
    if(ts!=null){
    ts.rollback();
    }
    e.printStackTrace();
    }finally{
    MySessionFactory.closeSession();
    }
    }
    public Datamap loadDatamap(Integer id){
    Session session=MySessionFactory.currentSession();
    Transaction ts=null;
    try{
    ts=session.beginTransaction();
    datamap=(Datamap)session.get(Datamap.class,id);
    ts.commit();
    }catch(Exception e){
    if(ts!=null){
    ts.rollback();
    }
    e.printStackTrace();
    }finally{
    MySessionFactory.closeSession();
    }
    return datamap;
    }
    public void delDatamap(Integer id){
    Session session=MySessionFactory.currentSession();
    Transaction ts=null;
    try{
    ts=session.beginTransaction();
    Datamap datamap=(Datamap)session.load(Datamap.class,id);
    session.delete(datamap);
    ts.commit();
    }catch(Exception e){
    if(ts!=null){
    ts.rollback();
    }
    e.printStackTrace();
    }finally{
    MySessionFactory.closeSession();
    }
    }
    }
    (3)创建一个可供测试用的TestBean.java
    package com.bean;
    import com.ORM.*;
    import java.io.*;
    import java.math.BigDecimal;
    import java.net.*;
    import org.hibernate.*;
    public class TestBean{
    TestDAO dao=new TestDAOImpl();
    //得到指定URL的html内容
    private String getHtmlByUrl(String url){
    StringBuffer hmtl=new StringBuffer();
    String line=null;
    try{
    URL u=new URL(url);
    URLConnection uc=u.openConnection();
    BufferedReader br=new BufferedReader(new InputStreamReader(uc.getInputStream()));
    while(line=(br.readLine())!=null){
    html.append(line);
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    return html.toString();
    }
    //读取二进制流
    private byte[] readBinary(InputStream in){
    byte[] binCodes=null;
    try{
    binCodes=new byte[in.available()];
    in.read(binCodes);
    in.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    return binCodes;
    }
    }
    //从输出流中创建BLOB对象
    private java.sql.Blob getBlob(InputStream in){
    java.sql.Blob blob=null;
    try{
    blob=Hibernate.createBlob(in);
    in.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    return blob;
    }

  • 相关阅读:
    CDQ分治
    2-sat
    整体二分
    apache性能优化
    apache反向代理出现502调整
    hadoop学习笔记肆--元数据管理机制
    ssh 免密码登录配置,及其原理
    extjs 中的一些鲜为人知的属性(深渊巨坑)
    hadoop学习笔记叁--简单应用
    hadoop学习笔记贰 --HDFS及YARN的启动
  • 原文地址:https://www.cnblogs.com/2881064178dinfeng/p/6963915.html
Copyright © 2011-2022 走看看