第四章 Hibernate入门

1.构建了一个Student实体类

public class Student {

private Integer id;

//name

private String name;

//age

private Integer age;

}

2.构建一个大配置

 src根目录下书写

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">

 

<<hibernate-configuration>

   <session-factory>

<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>

<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>

<property name="connection.username">sb</property>

<property name="connection.password">sb</property>

 

<!-- 输出所有 SQL 语句到控制台。 -->

<property name="hibernate.show_sql">true</property>

 

<!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->

<property name="hibernate.format_sql">true</property>

<!-- 方言 -->

<property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property>

 

 

 

   <!-- 关联小配置 -->

   

   </session-factory>

 

</hibernate-configuration>

 

3.构建小配置,和实体类对应的

  Student.hbm.xml

<?xml version='1.0' encoding='utf-8'?>

<!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="Student" table="Student">

         <id name="id" type="int" column="id">

         </id>

         <property name="name" type="string" column="name"/>

         <property name="age" type="int" column="age"/>

     </class>    

 </hibernate-mapping>

 

4.测试代码

  session进行探究。

  Session.save(stu);

  package cn.happy.test;

 

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.classic.Session;

import org.junit.Test;

 

import cn.happy.entity.Student;

 

public class H_01Test {

  @Test

  public void testAdd(){

  //1.1构建一个学生对象

  Student stu=new Student();

  stu.setAge(18);

  stu.setName("2016年8月28日09:21:09训练营");

  stu.setId(3);

  

  //1.2 找到和数据库的接口      xxx========session--->sessionFactory--->configure.buildSessionFactory()

  //咱们要想打通和db通道

  Configuration cf=new Configuration().configure("hibernate.cfg.xml");

  SessionFactory factory = cf.buildSessionFactory();

  Session session = factory.openSession();

  Transaction tx = session.beginTransaction();

  

  

  //1.3保存   

  session.save(stu);

  tx.commit();

  session.close();

  }

 

}

转载于:https://www.cnblogs.com/superws/p/5815217.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值