Hibernate 入門小案例
阿新 • • 發佈:2017-05-08
java程序 div nocache org student 包含 target .so tell
前言:
學習學到現在終於要學習框架了,心裏有點小激動呢,也不知道自己能不能學好呢,只能按著一步一個腳印的走下去,好了廢話不多說。讓我們打開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 入門小案例