二.安裝hibernate插件 打開eclipse,點擊help-->eclipse marketplace,如圖輸入:Hibernate Tools,再點擊Goa按鈕,找到JBoss Tools 點擊install安裝 如圖選擇Hibernate Tools,點擊Confrm安裝。安裝完成後重啟e ...
- 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"/>
測試驗證成功。
整個工程架構如圖: