淺談NetBeans下配置Hibernate連接MySQL 5
NetBeans下配置Hibernate連接MySQL 5前提:
首先安裝Hibernate 2.1
其次安裝MySQL 5
然后安裝mysql-connector-java-3.1.12-bin.jar
需要有Netbeans 5.XIDE
1 .配置Hibernate庫(kù)
在Netbeans 的Tools->Library Manager中點(diǎn) New Library,在Library Name中輸入 hibernate 2.1
然后點(diǎn)OK,在ClassPath 中點(diǎn) Add JAR/Folder,選擇{Hibernate安裝目錄}\lib 添加所有的文件。
再選擇JavaDoc,點(diǎn)Add JAR/Folder 選擇{Hibernate安裝目錄}\doc\api。這樣可以獲得doc
2 .這里配置MySQL的庫(kù)
用#1同樣的方法配置mysql-connector-java-3.1.12-bin.jar,只不過(guò)選擇添加的是mysql-connector-java-3.1.12-bin.jar
然后再netbeans的工程視圖下,右鍵點(diǎn) library,選擇添加library,把前面添加好的hibernate 2.1和 MySQL connector添加進(jìn)去
3. 在MySQL中建立一個(gè)schmeate 叫test ,再建立一個(gè)table叫CUSTOMER,其中有幾個(gè)屬性,分別是id[bigint(20)],name[varchar],
email[varchar],phonenumber[varchar],其中id是primer key
4. 在netbeans中建立一個(gè)Customer類(lèi)。具體代碼如下
- package jdbctest;
- import java.io.Serializable;
- import java.sql.Date;
- import java.sql.Timestamp;
- /**
- *
- * @author AlarnMartin
- */
- public class Customer implements Serializable
- {
- /** Creates a new instance of Customer */
- public Customer ()
- {
- }
- /**
- * 設(shè)置OID
- * @param id OID,用來(lái)區(qū)分實(shí)例的ID
- */
- public void setId(Long id)
- {
- this.id = id;
- }
- /**
- * 獲得OID,可以用customerA.getId().compar(customerB.getId())來(lái)比較兩個(gè)實(shí)例是否一樣
- * @return OID 用來(lái)區(qū)分是否是同一條記錄
- */
- public Long getId()
- {
- return this.id;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return this.name;
- }
- public void setEmail(String email)
- {
- this.email = email;
- }
- public String getEmail()
- {
- return this.email;
- }
- public void setPassword(String password)
- {
- this.password = password;
- }
- public String getPassword()
- {
- return this.password;
- }
- public void setPhone(int phone)
- {
- this.phone = phone;
- }
- public int getPhone()
- {
- return this.phone;
- }
- private Long id;
- private String name;
- private String email;
- private String password;
- private int phone;
- }
5 .在Netbeans 建立一個(gè) Customer.hbm.xml文件,注意這個(gè)XML文件不能放到包內(nèi),因?yàn)榍懊娴念?lèi)已經(jīng)放到了jdbctest包內(nèi)了,而且由于其他原因,所以這個(gè)XML不能放到包內(nèi),具體代碼如下:
- xml version="1.0" encoding="UTF-8"?>
- PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
- <hibernate-mapping>
- <class name="jdbctest.Customer" table="CUSTOMERS">
- <id name="id" column="ID" type="long">
- <generator class="increment"/>
- < SPAN>id>
- <property name="name" column="NAME" type="string" not-null="true" />
- <property name="email" column="EMAIL" type="string" not-null="true" />
- <property name="password" column="PASSWORD" type="string" not-null="true"/>
- <property name="phone" column="PHONE" type="int" />
- <property name="address" column="ADDRESS" type="string" />
- < SPAN>class>
- < SPAN>hibernate-mapping>
6.再建立一個(gè)hibernate.cfg.xml
具體內(nèi)容如下:
- xml version=’1.0’ encoding=’UTF-8’?>
- "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="connection.username">root< SPAN>property>
- <property name="connection.url">jdbc:MySQL://localhost:3306/test< SPAN>property>
- <property name="dialect">net.sf.hibernate.dialect.MySQLDialect< SPAN>property>
- <property name="connection.password">bd643012< SPAN>property>
- <property name="connection.driver_class">org.gjt.mm.MySQL.Driver< SPAN>property>
- <mapping resource="Customer.hbm.xml"/>
- < SPAN>session-factory>
- < SPAN>hibernate-configuration>
7.再建立一個(gè)Test類(lèi)進(jìn)行測(cè)試
- package jdbctest;
- import net.sf.hibernate.*;
- import net.sf.hibernate.cfg.*;
- import java.math.*;
- public class Test {
- /**
- * @author 鮑冠辰
- */
- public static void main(String[] args) throws Exception{
- // TODO Auto-generated method stub
- SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
- Customer customer = new Customer();
- customer.setId(Long.valueOf("4"));
- customer.setName("martin");
- customer.setEmail("tain198127@163.com");
- customer.setPassword("123456");
- Session session = sessionFactory.openSession();
- Transaction tx = session.beginTransaction();
- session.save(customer);
- tx.commit();
- session.close();
- sessionFactory.close();
- System.out.println("ok");
- }
- }
執(zhí)行一下吧,再看看MySQL的變化。
大家可以發(fā)現(xiàn),如果更換了其他的數(shù)據(jù)庫(kù)的話(huà),只需要配置一下XML文件就可以了。這就是NetBeans下配置Hibernate連接MySQL 5的具體步驟。
【編輯推薦】