この記事では、Eclipseの下でHibernate 5.0環境を構築する手順を紹介し、それをあなたと共有します。詳細は次のとおりです。
2. Hibernateプラグインをインストールします
Eclipseを開き、[ヘルプ]をクリックします - > [Eclipse Marketplace、Enter:Hibernate Tools、[GoA]ボタンをクリックしてJBossツールを見つけます
[インストール]をクリックしてインストールします
図に示すようにHibernateツールを選択し、インストールするためにconprmをクリックします。インストールが完了した後、日食を再起動します。
3.プロジェクトを作成します
1.新しいプロジェクトを作成し、プロジェクトの下でLIBフォルダーを作成します。 JARパッケージのディレクトリを開き、lib/execredとデータベースの下にJARパッケージをインポートし、ビルドパスに追加します
SRCの下に新しいファイルを作成します
[次へ]、デフォルトのファイル名をクリックし、[次へ]をクリックして、図に示すようにデータベース情報を構成します
UTF-8エンコーディングメソッドを選択し、[完了]をクリックし、生成されたhibernate.cfg.xml構成ファイルコンテンツは次のとおりです。
<?xml version = "1.0" encoding = "utf-8"?> <!doctype hibernate-configuration public " - // hibernate/hibernate構成<プロパティ名= "hibernate.connection.driver_class"> com.mysql.jdbc.driver </property name> <property name = "hibernate.connection.password"> a123 </property> <Property name = "hibernate.connection.url"> jdbc:mysql://///// </</</</</property name = "hibernate.connection.username"> sherman </property> <property name = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </property> </session-factory> </hibernate-configuration>
<session-factory name = "mysql">の名前属性を削除することに注意してください。それ以外の場合は、org.hibernate.engine.jndi.jndiexceptionが報告され、図に示すように、いくつかの構成がファイルに追加されます。
<?xml version = "1.0" encoding = "utf-8"?> <!doctype hibernate-configuration public " - // hibernate/hibernate構成<プロパティ名= "hibernate.connection.driver_class"> com.mysql.jdbc.driver </property name> <property name = "hibernate.connection.password"> a123 </property> <Property name = "hibernate.connection.url"> jdbc:mysql://///// </</</</</property name = "hibernate.connection.username"> sherman </property> <! - データベースの方言の構成 - > <プロパティ名= "hibernate.dialect"> org.hibernate.dialect.mysql5dialect </property> <! - sql statement statement on the console-> <of> name = "format_sql"> true </property> <! - 起動時の構成に従ってデータベースを更新します - > <プロパティ名= "hibernate.hbm2ddl.auto"> update </property> <! - 接続数を接続数を構成 - 接続プール - > <プロパティname. <connection.pool_sizize "> 20 <<<< </session-factory> </hibernate-configuration>
SRCの下で新しいパッケージcom.gdut.app.entityを作成し、永続的なクラスのニュースを保存すると、ニュースクラスコードは次のとおりです。
パッケージcom.gdut.app.entity; import javax.persistence.entity; import javax.persistence.generatedValue; Import javax.generationType; Import javax.persistence.id; import javax.persistence.table;@entity@table(name new_info ")public class {@id@generatedValue(Strategy = GenerationType.Identity)プライベート整数ID;プライベート文字列タイトル;プライベート文字列コンテンツ; public News(){} public News(integer id、string title、string content){this.id = id; this.title = title; this.content = content;} public Integer getId(){return id;} public void setid(integer id){this.id = id;} public string gettitle(){return title;} public void setitle(string settitle(string){this.title = title;} public string getContent( content;}@overridepublic string toString(){return "news [id =" + id + "、content =" + content + "];}}}テストクラスを書く:
パッケージcom.gdut.app.entity; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.hibernate.cfg.configuration; import org.junit.test; public class beantest {@testest public void beantest new Stardentserviceregistrybuilder()// .configure( "hibernate.cfg.xml")。build(); // sessionfactory sf = new metadatasources(serviceregistry).buildmetadata()。buildsessionfactory(); //両方のメソッドを取得することができますsessionfactory configuration cfg = new configuration()。configure(); sessionfactory sf = cfg.buildsessionfactory();セッションsess = sf.opensession();トランザクショントランザクション= sess.begintransaction();ニュースn = new News(); n.setContent( "広州の卒業生"); n.settitle( "卒業季節"); sess.save(n); transaction.commit(); sess.close(); }}正常にテストされました
またはファイルをマッピングします
com.gdut.app.entityパッケージの下のnews.hbm.xmlマッピング構成ファイルを再開し、Genaratorのクラス属性をアクティブに変更します
<?xml version = "1.0"?> <!doctype hibernate-mapping public " - // hibernate/hibernateマッピングDTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <! - > <hibernate-mapping> <class name = "com.gdut.app.entity.news" table = "news"> <id name = "id" type = "java.lang.integer"> <column name = "id"/> <generator/>> </id> <propertion name = "titper" type = "jhava.String"> type = "java.lang.string"> <column name = "content"/> </property> </class> </hibernate-mapping>
hibernate.cfg.xmlで構成します
<マッピングリソース= "com/gdut/app/entity/news.hbm.xml"/>
テストの検証は成功しました。
プロジェクトアーキテクチャ全体が図に示されています。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。