Artikel ini memperkenalkan langkah -langkah untuk membangun lingkungan Hibernate 5.0 di bawah Eclipse, dan membagikannya kepada Anda. Dengan rincian sebagai berikut:
2. Pasang plug-in hibernate
Buka Eclipse, klik Bantuan-> Eclipse Marketplace, masukkan: alat hibernasi, lalu klik tombol Goa untuk menemukan alat JBoss
Klik Instal untuk menginstal
Pilih alat Hibernate seperti yang ditunjukkan pada gambar dan klik CONFRM untuk menginstal. Restart Eclipse setelah instalasi selesai.
3. Buat proyek
1. Buat proyek baru Hibernedemo dan buat folder lib di bawah proyek. Buka Direktori Paket JAR, Impor Paket JAR di bawah Lib/Diperlukan dan Database, Tambahkan ke Build Path
Buat file baru di bawah SRC
Klik Berikutnya, Nama File Default, Klik Berikutnya, Konfigurasikan Informasi Basis Data seperti yang ditunjukkan pada gambar
Pilih Metode Pengkodean UTF-8, Klik Selesai, dan Konten File Konfigurasi Hibernate.cfg.xml yang dihasilkan adalah sebagai berikut
<? 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-facory-.0.dronpony"> hibernate "> <donfiguration- configuration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration- <name properti = "hibernate.connection.driver_class"> com.mysql.jdbc.driver </property> <property name = "hibernate.connection.password"> a123 </prop Property = "hibernate. name = "hibernate.connection.username"> sherman </pruptent> <name properti = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </preate> </sion-factory> </hibernate-konfigurasi>
Perhatikan bahwa menghapus atribut nama <sesi-factory name = "mysql">, jika tidak org.hibernate.engine.jndi.jndiexception dilaporkan, dan beberapa konfigurasi ditambahkan ke file, seperti yang ditunjukkan pada gambar:
<? 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-facory-.0.dronpony"> hibernate "> <donfiguration- configuration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration-.donfiguration- <name properti = "hibernate.connection.driver_class"> com.mysql.jdbc.driver </property> <property name = "hibernate.connection.password"> a123 </prop Property = "hibernate. name = "hibernate.connection.userName"> Sherman </property> <!-Mengkonfigurasi dialek database-> <name properti = "hibernate.dialect"> org.hibernate.dialect.mysql 5dialect </peremudi "<!-cetak SQL pernyataan pada konsol-> <mysql5 name =" show "</peremudi" </sqly </sql pernyataan sql pada konsol-> <mysq name = "show —kereAct </sq <! name = "format_sql"> true </pruptent> <!-perbarui database sesuai dengan konfigurasi saat startup-> <properti name = "hibernate.hbm2ddl.auto"> update </preate> <!-Konfigurasikan jumlah koneksi ke kumpulan koneksi-> <nama properti = "connection.pool_size"> 20 </20 </classies> </property> <Property name = "connection.pool_size"> 20 </20 </classy> The Properties> <Property name = "connection.pool_size"> 20 </20 </ </sesi-Factory> </hibernate-konfigurasi>
Buat paket baru com.gdut.app.entity di bawah SRC, dan simpan berita kelas persisten, dan kode kelas berita adalah sebagai berikut
Paket com.gdut.app.entity; import javax.persistence.entity; import javax.persistence.generatedValue; import javax.persistence.generationType; import javax.persistence.id; import javax.persistence.table;@entitas@table (name = "news_inf {@Id@generatedValue (strategy = generationType.Identity) Private Integer ID; Judul String Pribadi; Konten String Pribadi; Berita Publik () {} Berita Publik (ID Integer, Judul String, Konten String) {this.id = id; this.title = judul; this.content = content;} public integer getId () {return id;} public void setId (integer id) {this.id = id;} public string getTitle () {return title;} public void setTitle (string title) {this.title = title;} string publik getContent () {return contents; {this. konten;}@overridepublic string toString () {return "news [id =" + id + ",, content =" + content + "]";}}Tulis kelas tes:
Paket com.gdut.app.entity; impor org.hibernate.session; impor org.hibernate.sessionFactory; impor org.hibernate.transaction; impor org.hibernate.cfg.configuration; Layanan Impor org.junit.test; StandardServiceregistryBuilder () // .configure ("hibernate.cfg.xml"). Build (); // // sessionfactory sf = MetadataSources baru (serviceregistry) .buildmetadata (). BuildSessionFactory (); // Kedua metode dapat diperoleh Konfigurasi Sesi CFG = Konfigurasi Baru (). Configure (); Sessionfactory sf = cfg.buildsessionFactory (); Sesi sess = sf.opensession (); Transaksi transaksi = sess.begintransaction (); Berita n = berita baru (); N.SetContent ("Lulusan di Guangxi"); N.Settitle ("Musim Kelulusan"); sess.save (n); transaction.commit (); sess.close (); }}Berhasil diuji
Atau dengan memetakan file
Lanjutkan sebuah news.hbm.xml file konfigurasi pemetaan di bawah paket com.gdut.app.entity, ubah atribut kelas genarator menjadi aktif
<? XML Versi = "1.0"?> <! Doctype Hibernate-Mapping Public "-// Hibernate/Hibernate Mapping DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-papping-3.0.dtd"> <! -> <hibernate-mapping> <class name = "com.gdut.app.entity.news" table = "news"> <id name = "id" type = "java.lang.integer"> <kolom name = "id"/<generator/> </d> <nama properti = "title" type = "java.string"/"colom" </id name = "title" type = "java.string" colom "/</id name =" titu type = "java.lang.string"> <kolom name = "content"/> </propert> </belas> </hibernate-Mapping>
Konfigurasikan di hibernate.cfg.xml
<pemetaan sumber daya = "com/gdut/app/entitas/news.hbm.xml"/>
Verifikasi tes berhasil.
Seluruh arsitektur proyek ditunjukkan pada gambar:
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.