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;
}
}
對應的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框架程序設計有所幫助。
- 深入解析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的入門學習教程
相關(guān)文章
Spring Boot中使用Spring-data-jpa的配置方法詳解
今天小編就為大家分享一篇關(guān)于Spring Boot中使用Spring-data-jpa的配置方法詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Mybatis框架之模板方法模式(Template Method Pattern)的實現(xiàn)
MyBatis中使用了模板方法模式來控制SQL語句的執(zhí)行流程,本文主要介紹了Mybatis框架之模板方法模式(Template Method Pattern)的實現(xiàn),需要的朋友們下面隨著小編來一起學習學習吧2024-11-11
SpringBoot中的@ControllerAdvice使用方法詳細解析
這篇文章主要介紹了SpringBoot中的@ControllerAdvice使用方法詳細解析, 加了@ControllerAdvice的類為那些聲明了@ExceptionHandler、@InitBinder或@ModelAttribute注解修飾的 方法的類而提供的專業(yè)化的@Component,以供多個 Controller類所共享,需要的朋友可以參考下2024-01-01
解決Spring boot整合mybatis,xml資源文件放置及路徑配置問題
這篇文章主要介紹了解決Spring boot整合mybatis,xml資源文件放置及路徑配置問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
spring cloud gateway全局過濾器實現(xiàn)向request header中放數(shù)據(jù)
這篇文章主要介紹了spring cloud gateway全局過濾器實現(xiàn)向request header中放數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

