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

最新文章

  1. 为 placeholder 自定义样式
  2. table中某一个tr边框样式设置
  3. c++ STL 学习记录 草稿。
  4. 使用PHPExcel导出文件
  5. UVa 10071 - Back to High School Physics
  6. [ CodeVS冲杯之路 ] P1171
  7. hdu 4150 Powerful Incantation
  8. java UncaughtExceptionHandler 处理线程意外中止
  9. OutputStream窥探
  10. HDU4276 - The Ghost Blows Light(树形DP)
  11. MVC下载Excel
  12. 第二次冲刺spring会议(第六次会议)
  13. Spark SQL数据源
  14. woe_iv原理和python代码建模
  15. HTML5:在移动端禁用长按选中文本功能
  16. SVN 分支主干的相互合并
  17. c#英文大小写快捷键
  18. 论文阅读:Learning Visual Question Answering by Bootstrapping Hard Attention
  19. ALGO-143_蓝桥杯_算法训练_字符串变换
  20. BZOJ1171 : 大sz的游戏

热门文章

  1. javascript : 点击按钮,把某标签中的内容复制到剪贴板
  2. JDBC 连接 MySQL 8.0.15+ 常见错误记录
  3. Python灰帽子:黑客与逆向工程师的Python编程之道PDF高清完整版免费下载|百度云盘
  4. 前端学习(八):CSS
  5. spring 循环依赖的一次 理解
  6. PHP zip_entry_close() 函数
  7. PHP quotemeta() 函数
  8. vue做多行滚动广告牌
  9. 字节跳动2020-ByteCamp暑期夏令营研发组习题
  10. python程序设计PDF高清完整版免费下载|百度云盘