zoukankan      html  css  js  c++  java
  • Hibernate搭建案例步骤:

    5.1引入依赖:
    <!--hibernate core-->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.6.Final</version>
    </dependency>
    
    <!--jta的jar包-->
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0</version>
    </dependency>
    
    5.2创建Hibernate.cfg.xml
    <?xml version="1.0" encoding="GBK"?>
    <!-- 指定Hibernate配置文件的DTD信息 -->
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <!-- hibernate- configuration是连接配置文件的根元素 -->
    <hibernate-configuration>
        <session-factory>
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@//localhost/orcl</property>
            <property name="connection.username">scott</property>
            <property name="connection.password">scott</property>
            <!-- 指定数据库方言 -->
            <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
            <!-- 根据需要自动创建数据表 -->
            <property name="hbm2ddl.auto">update</property>
            <!-- 显示Hibernate持久化操作所生成的SQL -->
            <property name="show_sql">true</property>
            <!-- 将SQL脚本进行格式化后再输出 -->
            <property name="hibernate.format_sql">true</property>
            <!-- 罗列所有的映射文件 -->
            <mapping resource="cn/happy/entity/Dept.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
    5.3创建Dept.hbm.xml文件
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="cn.happy.entity">
    <class name="Dept" table="Dept">
        <id name="deptNo" column="deptNo">
            <!--主键生成策略
          native:由底层数据决定主键值
           Mysql:自增 auto_increment
           Oracle: 序列
           hibernate_session 序列
          -->
            <generator class="native"/>
        </id>
    <!--普通的列-->
        <property name="deptName" type="string" column="deptName"/>
    </class>
    </hibernate-mapping>
    
    
    5.4 写main函数
    package cn.happy;
    import cn.happy.entity.Dept;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    public class Test_first {
        public static void main(String[] args) {
            Configuration config=new Configuration().configure();
            System.out.println(config+"===");
            SessionFactory sessionFactory = config.buildSessionFactory();
    //和数据连接的对象  不仅仅是connection
            Session session = sessionFactory.openSession();
            Dept dept=new Dept();
            dept.setDeptName("我的第一次Hibernate,佛祖保佑永无BUG");
            Transaction transaction = session.beginTransaction();
            session.save(dept);
            //在Hibernate中 ,要想 CUD Success  ,must  run  in transaction environment
            transaction.commit();
            session.close();
            System.out.println("add OK!");
        }
    }

  • 相关阅读:
    slot的使用实例
    vue slot插槽的使用方法
    ES6必知必会 (九)—— Module
    MVC3 项目总结
    项目总结,
    oa项目总结
    项目总结。。。
    Delphi 项目总结
    Ubuntu常用命令大全
    linux系统文件目录解析
  • 原文地址:https://www.cnblogs.com/www-yang-com/p/10422538.html
Copyright © 2011-2022 走看看