浅谈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库
在Netbeans 的Tools->Library Manager中点 New Library,在Library Name中输入 hibernate 2.1
然后点OK,在ClassPath 中点 Add JAR/Folder,选择{Hibernate安装目录}\lib 添加所有的文件。
再选择JavaDoc,点Add JAR/Folder 选择{Hibernate安装目录}\doc\api。这样可以获得doc
2 .这里配置MySQL的库
用#1同样的方法配置mysql-connector-java-3.1.12-bin.jar,只不过选择添加的是mysql-connector-java-3.1.12-bin.jar
然后再netbeans的工程视图下,右键点 library,选择添加library,把前面添加好的hibernate 2.1和 MySQL connector添加进去
3. 在MySQL中建立一个schmeate 叫test ,再建立一个table叫CUSTOMER,其中有几个属性,分别是id[bigint(20)],name[varchar],
email[varchar],phonenumber[varchar],其中id是primer key
4. 在netbeans中建立一个Customer类。具体代码如下
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 () { } /** * 设置OID * @param id OID,用来区分实例的ID */ public void setId(Long id) { this.id = id; } /** * 获得OID,可以用customerA.getId().compar(customerB.getId())来比较两个实例是否一样 * @return OID 用来区分是否是同一条记录 */ 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 建立一个 Customer.hbm.xml文件,注意这个XML文件不能放到包内,因为前面的类已经放到了jdbctest包内了,而且由于其他原因,所以这个XML不能放到包内,具体代码如下:
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"/> 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" /> class> hibernate-mapping>
6.再建立一个hibernate.cfg.xml
具体内容如下:
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">rootproperty> property name="connection.url">jdbc:MySQL://localhost:3306/testproperty> property name="dialect">net.sf.hibernate.dialect.MySQLDialectproperty> property name="connection.password">bd643012property> property name="connection.driver_class">org.gjt.mm.MySQL.Driverproperty> mapping resource="Customer.hbm.xml"/> session-factory> hibernate-configuration>
7.再建立一个Test类进行测试
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("[email protected]"); customer.setPassword("123456"); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save(customer); tx.commit(); session.close(); sessionFactory.close(); System.out.println("ok"); } }