您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關eclipse下怎么搭建hibernate5.0環境的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體如下:
hibernate引入的jar包:hibernate-release-5.0.12.Final.zip
數據庫驅動:mysql-connector-java-5.1.46
二.安裝hibernate插件
打開eclipse,點擊help-->eclipse marketplace,如圖輸入:Hibernate Tools,再點擊Goa按鈕,找到JBoss Tools
點擊install安裝
如圖選擇Hibernate Tools,點擊Confrm安裝。安裝完成后重啟eclipse。
三. 創建工程
1.創建新項目hibernateDemo,在工程下建立lib文件夾。打開jar包的目錄,導入lib/required下的和數據庫的jar包,add to build path
在src下新建文件
點擊next,默認文件名,點擊next,如圖配置數據庫信息
選擇UTF-8編碼方式,點擊finish,生成的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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">a123</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tb_test</property> <property name="hibernate.connection.username">sherman</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory> </hibernate-configuration>
注意,把 < session-factory name ="MySQL" > 的name屬性去掉,否則報org.hibernate.engine.jndi.JndiException異常,在該文件中添加一些配置,如圖:
<?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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">a123</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tb_test</property> <property name="hibernate.connection.username">sherman</property> <!-- 配置數據庫方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制臺打印sql語句 --> <property name="show_sql">true</property> <!-- 格式化sql --> <property name="format_sql">true</property> <!--在啟動時根據配置更新數據庫 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置連接池的連接數 --> <property name="connection.pool_size">20</property> <!-- 注冊實體映射類 --> <mapping class="com.gdut.app.entity.News"/> </session-factory> </hibernate-configuration>
在src下新建一個包com.gdut.app.entity,存放持久化類News,News類代碼如下
package com.gdut.app.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="NEWS_INFO") public class News { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String title; private String content; 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 setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "News [id=" + id + ", title=" + title + ", content=" + content + "]"; } }
編寫測試類:
package 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 { @Test public void beanTest() { // final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() // .configure("hibernate.cfg.xml").build(); // // SessionFactory sf = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory(); //兩種方式都可以獲取SessionFactory Configuration cfg = new Configuration().configure(); SessionFactory sf = cfg.buildSessionFactory(); Session sess =sf.openSession(); Transaction transaction = sess.beginTransaction(); News n = new News(); n.setContent("在廣工畢業"); n.setTitle("畢業季"); sess.save(n); transaction.commit(); sess.close(); } }
經過測試成功
或者通過映射文件
在com.gdut.app.entity包下簡歷一個News.hbm.xml映射配置文件,修改genarator的class屬性為active
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2018-5-22 23:45:23 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.gdut.app.entity.News" table="NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native"/> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <property name="content" type="java.lang.String"> <column name="CONTENT" /> </property> </class> </hibernate-mapping>
在hibernate.cfg.xml中配置
<mapping resource="com/gdut/app/entity/News.hbm.xml"/>
測試驗證成功。
整個工程架構如圖:
感謝各位的閱讀!關于“eclipse下怎么搭建hibernate5.0環境”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。