国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Hibernate的Annotation版Hello world實例

2019-11-26 14:29:31
字體:
來源:轉載
供稿:網友

本文實例講述了Hibernate的Annotation版Hello world實現方法。分享給大家供大家參考,具體如下:

需要引入的包:hibernate-commons-annotations-4.0.4.Final.jar
由于我使用的是:hibernate-release-4.3.5.Final,在required目錄下已經有了。

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框架程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 红河县| 瓮安县| 南木林县| 甘泉县| 顺义区| 昆山市| 云浮市| 阿合奇县| 武城县| 榕江县| 蕲春县| 新郑市| 富阳市| 朝阳区| 三穗县| 金山区| 沂水县| 砚山县| 甘肃省| 务川| 灵川县| 土默特左旗| 杭锦旗| 鄂州市| 延边| 五大连池市| 蚌埠市| 鹤壁市| 章丘市| 仙游县| 屏南县| 明溪县| 疏附县| 扎囊县| 宁陵县| 昂仁县| 洪江市| 汽车| 庄河市| 平遥县| 池州市|