Oops! Eclipse + Hibernate Quick Start

Oops! Eclipse + Hibernate Quick Start
 

Oops! Eclipse + Hibernate Quick Start

Purpose:

学会使用Hibernate

Precondition:

eclipse-java-europa-win32.zip

hibernate-3.2.5.ga.zip

mysql-5.0.45-win32.zip

Quick Start:

mySql数据库里面添加一张表。



对应的
sql语句是:

CREATE TABLE CUSTOMER(

CID INTEGER,

USERNAME VARCHAR(12) NOT NULL,

PASSWORD VARCHAR(12)

);

ALTER TABLE CUSTOMER ADD CONSTRAINT PK PRIMARY KEY(CID);


eclipse里面新建一个java project, 项目名为:Oops_hibernate


新建一个
lib目录,在lib目录下面添加以下jar包,全部可以在hibernate.zip文件里面找到


选择
project – properties – java build path – libraries – add jars

Oops_hibernate目录下面的所有lib加进来



src目录下面添加以下文件:


Customer.hbm.xml

<? xml version="1.0" ?>

<! DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>

< hibernate-mapping >

    
< class  name ="Customer"  table ="CUSTOMER" >

        
< id  name ="id"  column ="CID" >

            
< generator  class ="increment"   />

        
</ id >

        
< property  name ="username"  column ="USERNAME"   />

        
< property  name ="password"  column ="PASSWORD"   />

    
</ class >

</ hibernate-mapping >


Customer.java


public   class  Customer {

    

    
private   int  id;

    
private  String username;

    
private  String password;

    
public   int  getId() {

        
return  id;

    }

    
public  String getPassword() {

        
return  password;

    }

    
public  String getUsername() {

        
return  username;

    }

    
public   void  setId( int  id) {

        
this .id  =  id;

    }

    
public   void  setPassword(String password) {

        
this .password  =  password;

    }

    
public   void  setUsername(String username) {

        
this .username  =  username;

    }

}


hibernate.cfg.xml
,注意红色部分要和数据库对应。

         <?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE hibernate-configuration

    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"

    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

   

    <session-factory name="java:/hibernate/HibernateFactory">

       

        <property name="show_sql">true</property>

        <property name="connection.driver_class">

            com.mysql.jdbc.Driver

        </property>

        <property name="connection.url">

            jdbc:mysql://localhost:3306/test

        </property>

        <property name="connection.username">

            root

        </property>

        <property name="connection.password">

            admin

        </property>

        <property name="dialect">

            org.hibernate.dialect.MySQLDialect

        </property>

       

        <mapping resource="Customer.hbm.xml" />

       

    </session-factory>

   

</hibernate-configuration>

Test.java


import  org.hibernate. * ;

import  org.hibernate.cfg. * ;

public   class  Test {

    
public   static   void  main(String[] args) {

        
try  {

            SessionFactory sf 
=

                
new  Configuration().configure().buildSessionFactory();

            Session session 
=  sf.openSession();

           Transaction tx 
=  session.beginTransaction();

            
for  ( int  i  =   0 ; i  <   200 ; i ++ ) {

                Customer customer 
=   new  Customer();

                customer.setUsername(
" customer "   +  i);

                customer.setPassword(
" customer " );

                session.save(customer);

            }

            tx.commit();

            session.close();

        } 
catch  (HibernateException e) {

            e.printStackTrace();

        }

    }

}



右键点击项目,Run as – java application



在窗口选择
Test





运行,完成!


你可能感兴趣的:(Oops! Eclipse + Hibernate Quick Start)