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!");
    }
}

 

posted @ 2019-02-23 14:11  胜过这首歌  阅读(203)  评论(0编辑  收藏  举报
AmazingCounters.com