zoukankan      html  css  js  c++  java
  • Hibernate入门

    如何搭建Hibernat

    1.下载jar包

    2.部署jar文件

    3.创建配置文件

    4.创建持久化类和映射文件

    resource.xml

    hibernate.cfg.xml属性

    头文件

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">





    连接的四大属性
    connection.url:表示数据库url
    connection.usrname:用户名 (oracle)
    connection.password:oracle密码
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <!--根节点-->
    <hibernate-configuration>
    <session-factory>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
    <property name="connection.username">happy1</property>
    <property name="connection.password">happy1</property>
    <!-- SQL dialect SQL方言 -->
    <property name="dialect"> org.hibernate.dialect.Oracle10gDialect</property>
    <!-- Enable Hibernate's automatic session context management session和当前线程绑定-->
    <!-- <property name="current_session_context_class">thread</property>-->
    <!--打印sql 控制台-->
    <property name="show_sql">true</property>
    <!--自动构建表结构 create 先delete表结构 再创建,update直接更新表结构-->
    <property name="hbm2ddl.auto">update</property>
    <!--关联entity--><!--找小配置-->
    <mapping resource="cn/happy/entity/Dog.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>

    写完了,就单测一把看看能不能在oracle数据库里家个Dog表

    package cn.text.java.cn.happy.test;

    import cn.happy.entity.Dog;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.jcp.xml.dsig.internal.dom.DOMBase64Transform;
    import org.junit.Test;

    /**
    * Created by Happy on 2017-09-18.
    * 勿忘国耻 为中华之崛起而读书
    */
    public class Test20170918 {


    @Test
    public void test02(){
    //Configuration
    Configuration cfg=new Configuration().configure();//1[计算机] 配置
    //session对象
    SessionFactory factory=cfg.buildSessionFactory(); //2 创建session对象
    Session session = factory.openSession(); //3

    Dog dog=session.get(Dog.class,1);
    System.out.println(dog.getDogname());
    session.close();
    /*
    * 所有增删改操作
    * */
    }

    @Test
    public void test01(){
    //Configuration
    Configuration cfg=new Configuration().configure();//1
    //session对象
    SessionFactory factory=cfg.buildSessionFactory(); //2
    Session session = factory.openSession(); //3
    Transaction tx = session.beginTransaction();//4
    /*
    * 所有增删改操作
    * */
    Dog dog=new Dog();
    dog.setDogname("贝贝");
    dog.setDogage(20);
    session.save(dog);
    tx.commit();//5
    System.out.println("add ok!");
    session.close(); //☆⌒(>。≪)6
    }
    }
    
    
  • 相关阅读:
    第三周作业
    面向过程(或者叫结构化)分析方法与面向对象分析方法到底区别在哪里?请根据自己的理解简明扼要的回答。
    移动APP开发使用什么样的原型设计工具比较合适?
    java 从上至下打印二叉树
    Qt applendPlainText()/append() 多添加一个换行解决方法
    tolua 转换 std::shared_ptr
    cmake add_custom_command 使用
    Boost使用笔记(Smart_ptr)
    webpack4 安装
    git安装管理
  • 原文地址:https://www.cnblogs.com/hualishu/p/7552992.html
Copyright © 2011-2022 走看看