hibernate笔记:一、快速上手

   hibernate是一个开源的对象关系映射框架,它对jdbc进行了轻量级的对象封装,使用它我们可以使用对象编程思想来操作数据库,事实上,在java世界,它已成了ORM框架的代表。下面就一起来学习下hibernate

  一、获取hibernate。从https://www.hibernate.org/网站上可以获取到最新版本的hibernate和相关文档,笔记的例子使用了3.2.5。

  二、快速上手。

    1、打开myEclipse,新建一个java project,导入hibernate及JDBC相关jar包,就可以配置开发hibernate应用了。注:JDBC包是必需的,且需与数据对应,笔者使用的是mysql5.0数据库。

    2、配置hibernte。在src目录下新建一个xml文件,名称为hibernate.cfg.xml(当然,你也可以不叫这个名称,不过在代码中要作相应的修改),拷贝如下内容:

  

ExpandedBlockStart.gif 代码
<! 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:///test </ property >
        
< property  name ="hibernate.connection.username" > root </ property >
        
< property  name ="hibernate.connection.password" > password </ property >
        
< property  name ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ property >
        
< property  name ="show_sql" > true </ property >
        
< property  name ="hibernate.hbm2ddl.auto" > update </ property >  
        
        
        
< mapping  resource ="com/eja/hibernate/domain/User.hbm.xml" />
        
    
</ session-factory >
    
</ hibernate-configuration >

    3、新建一个实体类,User.java.为了方便,只有一个属性,就是name,如:

ExpandedBlockStart.gif 代码
package  com.eja.hibernate.domain;

import  java.util.Date;

public   class  User {
    
private   int  id;
    
private  String name;
    
    
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;
    }
}

    4、新建好实体类后,配置对应的xml文件。如下:

  

ExpandedBlockStart.gif 代码
<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping  package ="com.eja.hibernate.domain" >

    
< class  name ="User"   >
        
< id  name ="id" >
            
< generator  class ="native"   />
        
</ id >
        
< property  name ="name" />
    
</ class >

</ hibernate-mapping >

    好。配置已经好了。测试一下效果。增加junit,编写如下测试类。

    

ExpandedBlockStart.gif 代码
package com.eja.hibernate.Test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.eja.hibernate.domain.User;

public class TestUser {

    @Test
    public void addUser() {
        try {
            Configuration cfg = new Configuration();
            cfg.configure();   //如果配置的不是hibernate.cfg.xml。则需在此方法中引入
            SessionFactory sessionFactory = cfg.buildSessionFactory();
            Session session = sessionFactory.openSession();

            User user = new User();
            user.setName("name");
            session.save(user);
        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }
}

  点击jnuit就可以测试了。

  

  

    

 


转载于:https://www.cnblogs.com/Carmack/archive/2010/03/15/1686482.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值