標籤:ted 列印 串連 格式 ima demo util and org
上篇文章我們講述了eclipse安裝hibernate外掛程式的過程,這篇文章我們來做第一個HibernateDemo。
1)、hibernate的jar開發包的下載,官網:http://hibernate.org/orm/downloads/,解壓後我們可以在hibernate-release-5.2.3.Final\lib\required檔案夾下找到hibernate開發用到的jar包。
2)、建立一個Java Project項目,匯入jar包(記得把MySQL的jar包也加進去),在src根目錄下建立一個***.cfg.xml檔案(安裝外掛程式成功後可以快速建立),然後我們進行檔案配置,代碼如下:
<session-factory> <!-- 串連資料庫的基本資料 --> <property name="connection.username">root</property><!-- MySQL資料庫的使用者名稱 --> <property name="connection.password">root</property><!-- MySQL資料庫的密碼 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- MySQL驅動 --> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property><!--MySQL的URL,localhost(本地地址,可以省略)也可以改成遠程伺服器位址 --> <!-- 配置hibernate的基本資料 --> <!-- hibernate所使用的基本方言 --> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- 執行操作時是否在控制台列印 --> <property name="show_sql">true</property> <!-- 是否對sql格式化 --> <property name="format_sql">true</property> <!-- 指定自動產生資料表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定關聯的.hbm.xml檔案,這一步應該產生java beans後再配置 --> <mapping resource="com/hibernateHello/News.hbm.xml"/> </session-factory>
3)、定義JavaBeans(對應你的資料表),
package com.hibernateHello;import java.sql.Date;public class News { private Integer id; private String author; private String title; private Date date; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public News() { // TODO Auto-generated constructor stub } public News(String author, String title, Date date) { // super(); this.author = author; this.title = title; this.date = date; }}
4)、快捷產生JavaBeans對應的News.hbm.xml檔案(也是外掛程式提供),具體操作如所示。
產生的程式碼:
<hibernate-mapping> <class name="com.hibernateHello.News" table="NEWS"> <!-- id 主鍵 --> <id name="id" type="java.lang.Integer"> <column name="ID" /> <!-- 指定主鍵產生方式 native:使用資料庫本地方式 --> <generator class="native" /> </id> <property name="author" type="java.lang.String"> <column name="AUTHOR" /> </property> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <property name="date" type="java.sql.Date"> <column name="DATE" /> </property> </class></hibernate-mapping>
5)、 ***.cfg.xml指定關聯的**.hbm.xml檔案(*千萬不要忘記)
6)、通過JUnit測試,代碼如下:
package com.hibernateHello;import java.sql.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.junit.After;import org.junit.Before;public class Test { private Configuration configuration; private StandardServiceRegistry registry; private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void first() { // 建立Configuration對象:對應hibernate的基本配置資訊和對象關係映射資訊 configuration = new Configuration().configure(); //建立一個對象StandardServiceRegistry,configure("你的***.cfg.xml"),預設為hibernate.cfg.xml registry = new StandardServiceRegistryBuilder().configure().build(); // 建立會話工廠對象 sessionFactory = configuration.buildSessionFactory(registry); // 建立一個Session對象 session = sessionFactory.openSession(); // 開啟事物 transaction = session.beginTransaction(); }
/*
* saveNews()添加資料
* getNews()和loadNews()尋找資料
* updateNews()修改資料
* deleteNews()刪除資料
*/
@org.junit.Test
public void saveNews() { News news = new News("63va", "fe", new Date( new java.util.Date().getTime())); News a = new News(); session.save(news); } @org.junit.Test public void getNews() { News a1 = session.get(News.class, 1); System.out.println(a1); } @org.junit.Test public void loadNews() { News a1 = session.get(News.class, 1); System.out.println(a1); } @org.junit.Test public void updateNews() { News a1 = session.get(News.class, 2); a1.setAuthor("qin"); session.update(a1); System.out.println(a1); } @org.junit.Test public void deleteNews() { News a1 = session.get(News.class, 4); session.delete(a1); // System.out.println(a1); } @After public void finsh() { // 提交事物 transaction.commit(); //關閉session session.close(); // 關閉SessionFactory對象 sessionFactory.close(); }}
7)、運行效果:
hibernate學習-HibernateDemo