美文网首页
1.hibernate基础

1.hibernate基础

作者: arkulo | 来源:发表于2017-07-23 21:41 被阅读6次

百科定义:

hibernate是java语言下的对象关系映射解决方案

同类产品:

ibatis:半ORM产品,可以直接写sql
mybatis:这是ibatis的升级版
springJDBC: 这是spring提供的持久层技术

hibernate和mybatis各有各的方式,hibernate封装比较完整,完全是面向对象方式操作数据库,而mybatis是可以写sql的,在大多数互联网应用中,使用mybatis比较多,但是hibernate还是要学习的

安装配置

配置文件

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
<!--        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test"</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <mapping resource="entity/User.hbm.xml" /> -->
        
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <mapping resource="entity/User.hbm.xml"/>       
        
    </session-factory>
</hibernate-configuration>

sessionFactory工具类(单例模式)

    package util;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.classic.Session;
    
    public class hibernateUtil {
        private static SessionFactory factory;
        
        static{
            try{
                //读取配置文件hibernate.cfg.xml
                //这里最后的函数configure,如果不加的话,是读取.propertys文件
                Configuration cfg = new Configuration().configure();
                factory = cfg.buildSessionFactory();
            }catch(Exception e){
                e.printStackTrace();
            }       
        }
        
        public static Session getSession()
        {
            return factory.openSession();
        }
        
        public static void closeSession(Session session)
        {
            if(session!=null)
            {
                if(session.isOpen())
                {
                    session.close();
                }
            }
        }
        
        public static SessionFactory getSessionFactory()
        {
            return factory;
        }
    }

实体类:

    package entity;
    
    public class User {
        private int id;
        private String userName;
        private String passWd;
        
        public User(){}
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassWd() {
            return passWd;
        }
    
        public void setPassWd(String passWd) {
            this.passWd = passWd;
        }
        
        
    }

映射文件:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="entity.User">
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="userName" />        
            <property name="passWd" />
        </class>    
    </hibernate-mapping>

建表代码:

    package util;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    
    
    public class ExportDb {
        public static void main(String[] args) {
            //读取配置文件
            Configuration cfg = new Configuration().configure();
            
            SchemaExport export = new SchemaExport(cfg);
            //自动生成对应的表
            export.create(true, true);
        }
    }

相关文章

  • 1.hibernate基础

    百科定义: hibernate是java语言下的对象关系映射解决方案 同类产品: ibatis:半ORM产品,可以...

  • Hibernate的基础

    1.Hibernate的基本使用方式 1.Hibernate是一个对象关系映射框架 2.Hibernate的命名规...

  • Java 笔试题之框架篇(一)

    1.Hibernate通过sessionFactory获取session的方法 1)openSession()获取...

  • 三大框架(Hibernate+)

    1.hibernate 开源的ORM 框架(jdbc) ORM:Object Relational Mapping...

  • Hibernate(1):简介及入门案例

    简介: 1.Hibernate是什么? --是一个ORM框架(持久层框架) --orm(object relati...

  • Hibernate的工作原理

    hibernate的工作原理 1.Hibernate 的初始化. 读取Hibernate 的配置信息-〉创建Ses...

  • Mybatis基础知识

    一.Mybatis 1.Hibernate全表映射,而Mybatis半自动映射且可配置动态SQL 2.Mapper...

  • Hibernater学习笔记(三)

    1.Hibernate缓存 Hibernate一级缓存hibernate 的一级缓存默认是打开的hibernate...

  • Hibernate

    一、执行流程 二、介绍Hibernate(扯皮) 1.Hibernate 是一个持久化框架 (1)从狭义的角度来讲...

  • 1.Hibernate 的增删改查

    //可以抽取出一个openSession的工具类简化代码,但是自己练练手就多写了几遍 public class H...

网友评论

      本文标题:1.hibernate基础

      本文链接:https://www.haomeiwen.com/subject/hpmvzttx.html