標籤:串連池 ber connect oracle ref native 修改 factor 日誌
前言:
學習學到現在終於要學習架構了,心裡有點小激動呢,也不知道自己能不能學好呢,只能按著一步一個腳印的走下去,好了廢話不多說。讓我們開啟hibernate 的大門吧!!!
肯定好多人都會問什麼是hibernate呢?
解答:Hibernate是一個開放原始碼的對象關係映射架構,它對JDBC進行了非常輕量級的對象封裝,使得Java程式員可以隨心所欲的使用對象編程思維來操縱資料庫。 Hibernate可以應用在任何使用JDBC的場合,既可以在Java的用戶端程式使用,也可以在Servlet/JSP的Web應用中使用,最具革命意義的是,Hibernate可以在應用EJB的J2EE架構中取代CMP,完成資料持久化的重任。
我們接觸了Intelij IDEA,聽老師說這是一款特彆強大的軟體,下面的案例就是那這個軟體操作的。有興趣的同學可以去下載。
首先我要說一點,在IntelliJ IDEA裡面“new Project”就相當於我們eclipse的“workspace”,而“new Module”才是建立一個工 程,這是要注意的一點。
Intelij IDEA 中的架構圖:
建立一個工程分為以下幾步:
(1)new project
這樣一個項目就有了,接下來我們要添加我們需要的資源
(2)建立lib檔案夾
以上就是現階段我們所需要的jar包
(3)建立resource檔案夾,其中包含hibernate 的大配置資訊,在這要注意命名問題:hibernate.ccfg.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> < !--資料庫連接設定--> < !--資料庫JDBC驅動設定--> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> < !--資料庫url--> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> < !--資料庫使用者名稱--> <property name="connection.username">lex</property> < !--資料庫使用者密碼--> <property name="connection.password">lex</property> < !--JDBC串連池(使用內建的)--> <property name="connection.pool_size">1</property> < !--SQL方言--> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> < !--使Hibernate自動會話上下文管理--> <property name="current_session_context_class">thread</property> <!--關閉二級緩衝--> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- 是否將運行期間產生的sql輸出到日誌以供調試--> <property name="show_sql">true</property> < !--在啟動時,刪除和重新建立資料表結構--> <property name="hbm2ddl.auto">update</property>
<!-- 關聯小配置-->
<mapping-resource> cn/lex/entity/Student.hbm.xml</mapping-resource></hibernate-configuration >
(4)配置小配置,存在於實體層,命名:Student.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.lex.entity"> <class name="Student" table="Student"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"></property> </class></hibernate-mapping>
(5)書寫測試
package cn.lex.test;import cn.lex.entity.Student;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.util.List;/** * Created by accp on 2017/1/9. */public class FirstTest { Configuration cfg; //設定物件 Session session; //會話對象 Transaction tx; //事務對象 @Before public void mybefore(){ cfg=new Configuration().configure(); //擷取設定物件 SessionFactory factory = cfg.buildSessionFactory(); //擷取sessionfactory對象 session= factory.openSession(); //擷取session對象 tx= session.beginTransaction(); //開啟事務 } //新增學生 @Test public void add(){ Student stu=new Student(); stu.setName("微冷的雨"); session.save(stu); //提交到資料庫 System.out.println("save ok!"); } //根據條件查詢學生姓名 @Test public void select(){ String hql="from Student where id=?"; Query query = session.createQuery(hql); query.setParameter(0,26); Student stu =(Student) query.uniqueResult(); System.out.println(stu.getName()); }
//修改學生姓名
@Test
public void update(){
Student stu=session.load(Student.class,serializable:1);
stu.setName("天空的星星");
System.out.println("update ok!");
}
//刪除學生
@Test
public void delete(){
Student stu=session.load(Student.class,serializable:1);
session.delete(stu);
System.out.println("delete ok!");
}
//查詢學生
@Test
public void select(){
Student stu=session.load(Student.class,serializable:1);
System.out.println("姓名:"+stu.getName());
} @After public void after(){ tx.commit(); //提交事務 session.close(); //關閉session }}
點擊運行即可。。。
Hibernate 入門小案例