首页 > 代码库 > Eclipse中hibernate连接mySQL数据库练习

Eclipse中hibernate连接mySQL数据库练习


(采用的是hibernate中XML配置方式连接数据库,以后在更新其他方式的连接)

Hibernate就是Java后台数据库持久层的框架,也是目前企业用最多的数据库框架,主要是基于ORM -- object relationship Mapping,翻译成中文叫“对象关系映射”,也就是将SQL这种非面向对象语言转化成hibernation面向对象的写法,本文将通过在Eclipse下搭建HIbernate框架

我所使用的Hibernate版本是Hibernate3.3.2,Hibernate官网下载www.hibernate.org

下面开始搭建hibernate数据库:
1.在eclipse中新建Java project项目,名称为:hibernateTest。右击项目名称,点击property,选择Java Build Path,加入需要导入的JAR包。
2.在src文件夹下,新建hibernate.cfg.xml配置文件,(连接数据库的参数信息)

hibernate.cfg.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE hibernate-configuration PUBLIC    
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
             
    <hibernate-configuration> 
        <session-factory> 
            <property name="hibernate.connection.driver_class"> 
                com.mysql.jdbc.Driver    
            </property> 
            <property name="hibernate.connection.url"> 
                jdbc:mysql://localhost:3306/test
            </property> 
            <!--  数据库连接设置 --> 
           
            <property name="hibernate.connection.username">root</property> 
            <property name="hibernate.connection.password">123456</property> 
 
            <!-- show_sql 生成SQL语句输出到日志以供调式 --> 
            <property name="hibernate.show_sql">true</property> 
           
            <!-- SQL dialect 方言 --> 
            <property name="hibernate.dialect"> 
                org.hibernate.dialect.MySQLDialect  
            </property> 
           
             <!-- 指定session通过当前执行的线程来跟踪和界定 -->
            <property name="hibernate.current_session_contecxt_class" >
                thread
            </property>
 
           
            <!-- 添加实体类的映射文件--> 
            <mapping resource="XXXXX.hbm.xml" /> 
    
        </session-factory> 
    </hibernate-configuration> 

3.在mySQL数据库中有一个login表,字段为id(int),name(string),password(string),在model包下新建login.java,
login.java文件代码如下:
public class login {
   
    private int id;
    private String name;
    private String password;
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}


4.在该model包下,新建login.hbm.xml,用于映射数据库字段和表
login.hbm.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE hibernate-mapping PUBLIC 
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
           
    <hibernate-mapping package="x.model"> 
        <class name="login" table="login"> 
             <id name="id" type="java.lang.Integer"></id> 
            <property name="name" type="java.lang.String"  /> 
            <property name="password" type="java.lang.String" /> 
        </class> 
    </hibernate-mapping> 

5.在该model包下,新建testLogin.java作为测试文件
TestLogin.java代码如下:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class test {
   
     public static void main(String a[])
     {
      login l = new login();
      l.setId(1);
      l.setName("456");
      l.setPassword("456");
     
      Configuration cfg = new Configuration();
      SessionFactory sf = cfg.configure().buildSessionFactory();
      Session session = sf.openSession();
      session.beginTransaction();
      session.save(l);
      session.getTransaction().commit();
      session.close();
      sf.close();
      System.out.print("success!");
     }
}

6.运行程序,运行成功。

Eclipse中hibernate连接mySQL数据库练习