この記事では、冬眠のHello World実装方法の注釈版について説明します。次のように、参照のために共有してください。
紹介する必要があるパッケージ:Hibernate-Commons-Annotations-4.0.4.final.jar
私が使用しているので、hibernate-release-4.3.5.finalは、必要なディレクトリで既に利用可能です。
豆:
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;プライベート文字列名;プライベートストリングタイトル。 @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構成<! - データベース接続設定 - > <プロパティ名= "connection.driver_class"> com.mysql.jdbc.driver </property> <property name = "connection.url"> jdbc:mysql:// localhost/hibernate </property name> <property name = "connection.username"プール(ビルトインを使用) - > <! - <プロパティ名= "connection.pool_size"> 1 </property> - > <! - sql dialect-> <property name = "dialect"> org.hibernate.dialect.mysqldialect </property> <! - hibernateの自動セッションコンテキストを有効セカンドレベルのキャッシュを無効にします - > <プロパティ名= "cache.provider_class"> org.hibernate.cache.internal.nocacheprovider </property> <! - 実行されたすべてのsqlへ - > <プロパティname = "show_sql"> property> < name = "hbm2ddl.auto"> update </property> - > <マッピングリソース= "com/hibernate/model/suduent.hbm.xml"/> <mapping/> </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){学生a = new Student(); A.SetID(123); a.setage(32); a.setName( "Hello Hibernate!");ティーチャーティー=新しい先生(); tea.setid(4); tea.setname( "mysql"); tea.settitle( "high");構成cfg = new annotationConfiguration(); sessionfactory cf = cfg.configure()。buildSessionFactory();セッションセッション= cf.opensession(); session.begintransaction(); SESSION.SAVE(TEA); session.getTransaction()。commive(); session.close(); cf.close(); }}注:コードはパッケージパスを省略します。
その他の側面:
1.注釈は、属性に追加するか、メソッドを取得できます。
2。注釈付きXMLのマッピングとXML構成の違い! 1つはリソースで、もう1つはクラスです。
この記事が、すべての人の冬眠フレームワークプログラミングに役立つことを願っています。