自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hibernate結(jié)合MYSQL數(shù)據(jù)庫簡單教程

開發(fā) 后端
本文向您介紹在Eclipse中使用Hibernate結(jié)合MySQL數(shù)據(jù)庫開發(fā)的一個簡明教程。

在網(wǎng)上找了很多Hibernate的相關(guān)教程,大多數(shù)都是結(jié)合WEB服務(wù)器的,自己寫一個單獨的示例在Eclipse下直接運行,不需要自己寫ANT腳本,不需要結(jié)合web服務(wù)器。但是需要MYSQL數(shù)據(jù)庫-_-

首先要學會如何使用Eclipse,然后要下載Hibernate需要的所有JAR文件,最后安裝好MYSQL
準備開始!

第一步,我們要創(chuàng)建一個表 結(jié)構(gòu)如下
  +-----------+--------------+------+-----+---------+----------------+
  | Field  | Type | Null | Key | Default | Extra  |
  +-----------+--------------+------+-----+---------+----------------+
| id| int(11)  | NO| MUL | NULL| auto_increment |
| title| varchar(400) | YES | | NULL||
| content | text | YES | | NULL||
| time  | datetime | YES | | NULL||
  +-----------+--------------+------+-----+---------+----------------+

第二步,在Eclipse中創(chuàng)建一個JAVA項目(我在項目中用到的包名是cn.com.nick.hbm)。編寫News.java類,這個類對應(yīng)了數(shù)據(jù)庫中的表

   package cn.com.nick.hbm;
import java.util.Date;
public class News
{
private int id;
private String title;
private String content;
private Date date;
public int getId()
{
return id;
}
public void setId(int 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;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}

第三步,配置對應(yīng)關(guān)系 保存為News.hbm.xml文件 與News類在同一目錄下(并不是一定要在同一目錄下,為了方便暫時先放在這里)
   
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"










 

第四步,配置hibernate.cfg.xml 注意這個名字不能改,并且要放到SRC的跟路徑下(這里要注意,如果放錯地方示例中的方法是找不到這個文件的)
    
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"
true

com.mysql.jdbc.Driver

thread

jdbc:mysql://localhost:3306/test

root
123

org.hibernate.dialect.MySQLDialect

my

最后創(chuàng)建一個測試類 Test.java 代碼如下,里邊有注釋說明
package cn.com.nick.hbm;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class Test
{
private static final SessionFactory sessionFactory;
static
{
try
{
// 這里創(chuàng)建了SessionFactory 將hibernate.cfg.xml文件放到SRC的跟路徑下
// Hibernate會自己找到
sessionFactory = new Configuration().configure()
  .buildSessionFactory();
} catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
public static void main(String[] args)
{
// 實例化一個新的News對象,并填充內(nèi)容
News news = new News();
news.setTitle("測試標題");
news.setContent("添加測試內(nèi)容");
news.setDate(new Date());
Test t = new Test();
// 調(diào)用Test類下的存儲方法,相當于執(zhí)行INSERT語句
// t.Save(news);
// 調(diào)用查詢方法,顯示數(shù)據(jù)庫的內(nèi)容
t.select();
// 調(diào)用更新方法
// t.update();
// 調(diào)用刪除
// t.delete();
}
/**
* 一個簡單的添加數(shù)據(jù)方法
* @param news news對象,這個對象將被添加到庫中
*/
public void Save(News news)
{
try
{
//獲取hibernate的session
Session session = Test.getSessionFactory().getCurrentSession();
session.beginTransaction();
//這里只需要調(diào)用save方法把news對象傳進去就插入成功了!
session.save(news);
session.getTransaction().commit();
} catch (HibernateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查詢方法
*/
public void select()
{
try
{
Session session = Test.getSessionFactory().getCurrentSession();
session.beginTransaction();
//注意?。?!這里的 News 不是表名稱! 是對象名所以要注意大小寫
String sql=" from News";
//帶條件的查詢
//String sql="from News where id=1";
//用session.createQuery()執(zhí)行HQL查詢語句
List l = session.createQuery(sql).list();
//在控制臺循環(huán)輸出
for (News n : l)
{
 System.out.println(n.getId());
 System.out.println(n.getTitle());
 System.out.println(n.getContent());
 System.out.println(n.getDate());
 System.out.println("==============");
}
session.getTransaction().commit();
} catch (HibernateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 更新方法
*/
public void update()
{
try
{
Session session = Test.getSessionFactory().getCurrentSession();
session.beginTransaction();
//定義了要裝載對象的ID
Integer id = 1;
//用load方法裝載一個對象進來
News n = (News) session.load(News.class, new Integer(id));
//重新設(shè)置這個對象的標題
n.setTitle("更新后標題");
//用update方法更新這個對象
session.update(n);
session.getTransaction().commit();
} catch (HibernateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete()
{
try
{
Session session = Test.getSessionFactory().getCurrentSession();
session.beginTransaction();
//定義了要裝載對象的ID
Integer id = 6;
//用load方法裝載一個對象進來
News n = (News) session.load(News.class, new Integer(id));
//用delete方法刪除這個對象
session.delete(n);
session.getTransaction().commit();
} catch (HibernateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


好啦,直接運行Test類看一下效果吧!

您正在閱讀: Hibernate結(jié)合MYSQL數(shù)據(jù)庫簡單教程

【編輯推薦】

  1. Hibernate單元測試的方法:HSQLDB
  2. Hibernate的兩種配置文件格式
  3. Hibernate/JPA成功使用的十點心得
責任編輯:張攀 來源: 百度空間
相關(guān)推薦

2011-03-21 17:00:23

MySQL數(shù)據(jù)庫

2011-08-30 12:59:52

Qt數(shù)據(jù)庫

2009-09-25 13:18:15

Hibernate數(shù)據(jù)

2009-09-24 14:12:22

Hibernate數(shù)據(jù)

2010-06-04 09:58:03

MySQL數(shù)據(jù)庫備份

2010-06-09 11:32:51

MySQL數(shù)據(jù)庫備份

2009-09-24 13:17:37

Hibernate類庫

2009-07-02 09:35:02

hibernate訪問

2009-09-29 14:03:14

Hibernate數(shù)據(jù)

2009-06-24 07:53:47

Hibernate數(shù)據(jù)

2017-05-23 14:56:49

MySQLvsftpd虛擬用戶

2011-04-06 09:09:17

MySQL數(shù)據(jù)庫備份

2010-05-14 11:04:17

連接MySQL

2019-01-02 09:30:59

MySQL數(shù)據(jù)庫日志審計

2025-04-09 11:35:00

MySQL數(shù)據(jù)庫監(jiān)控

2009-09-28 13:33:48

Hibernate訪問

2009-06-24 07:58:52

Hibernate多數(shù)

2010-05-17 10:24:44

MySQL數(shù)據(jù)庫

2010-06-12 13:47:30

連接MySQL 數(shù)據(jù)庫

2018-01-04 10:43:43

OracleMysqlJava
點贊
收藏

51CTO技術(shù)棧公眾號