第四章 Hibernate入門

來源:互聯網
上載者:User

標籤:

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();

  }

 

}

第四章 Hibernate入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.