Hibernate的Annotation版Hello world實例
本文實例講述了Hibernate的Annotation版Hello world實現(xiàn)方法。分享給大家供大家參考,具體如下:
需要引入的包:hibernate-commons-annotations-4.0.4.Final.jar
由于我使用的是:hibernate-release-4.3.5.Final,在required目錄下已經(jīng)有了。
bean:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="teacher")
public class Teacher {
private int id;
private String name;
private String title;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
對應(yīng)的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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<!--
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--
<property name="hbm2ddl.auto">update</property>
-->
<mapping resource="com/hibernate/model/Student.hbm.xml"/>
<mapping class="com.hibernate.model.Teacher"/>
</session-factory>
</hibernate-configuration>
測試類:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import com.huxing.hibernate.model.Student;
import com.huxing.hibernate.model.Teacher;
public class StudentTest {
public static void main(String[] args) {
Student a = new Student();
a.setId(123);
a.setAge(32);
a.setName("hello hibernate!");
Teacher tea = new Teacher();
tea.setId(4);
tea.setName("mysql");
tea.setTitle("high");
Configuration cfg = new AnnotationConfiguration();
SessionFactory cf = cfg.configure().buildSessionFactory();
Session session = cf.openSession();
session.beginTransaction();
session.save(tea);
session.getTransaction().commit();
session.close();
cf.close();
}
}
注意:代碼省略了包路徑。
其他方面:
1.注解可以加在屬性上,也可以加在get方法上。
2.注解的mapping和xml配置的xml的不同!一個是resource,一個是class。
希望本文所述對大家Hibernate框架程序設(shè)計有所幫助。
- 深入解析Java的Hibernate框架中的持久對象
- Java的Hibernate框架中的基本映射用法講解
- Java Hibernate中使用HQL語句進行數(shù)據(jù)庫查詢的要點解析
- Java的Hibernate框架中一對多的單向和雙向關(guān)聯(lián)映射
- Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)
- 全面解析Hibernate關(guān)聯(lián)操作、查詢操作、高級特性、并發(fā)處理機制
- 解決Hibernate4執(zhí)行save()或update()無效問題的方法
- SSH框架網(wǎng)上商城項目第16戰(zhàn)之Hibernate二級緩存處理首頁熱門顯示
- Spring,hibernate,struts經(jīng)典面試筆試題(含答案)
- 擴展Hibernate使用自定義數(shù)據(jù)庫連接池的方法
- Hibernate延遲加載原理與實現(xiàn)方法
- Hibernate延遲加載技術(shù)詳解
- 基于hibernate實現(xiàn)的分頁技術(shù)實例分析
- hibernate批量操作實例詳解
- MyBatis與Hibernate的比較
- 詳解Java的Hibernate框架中的Interceptor和Collection
- Java的Hibernate框架結(jié)合MySQL的入門學(xué)習(xí)教程
相關(guān)文章
教你從頭開始用JAVA創(chuàng)建一個自己的簡單API并實現(xiàn)第三方調(diào)用
在日常開發(fā)的時候,經(jīng)常會遇到需要調(diào)用別人的接口的場景,下面這篇文章主要給大家介紹了關(guān)于如何從頭開始用JAVA創(chuàng)建一個自己的簡單API并實現(xiàn)第三方調(diào)用的相關(guān)資料,需要的朋友可以參考下2023-12-12

