hibernate入门实例

最近重新复习了下hibernate,看了一些文章,受到了启发,把我的收获总结一下;

一、hibernate的定义

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。

注:我理解的hibernate就是一个数据持久化架构,可以将对象自动的生成数据库中的信息,使得开发更加的面向对象。这样作为程序员就可以使用面向对象的思想来操作数据库,而不用关心繁琐的JDBC。所以,Hibernate主要负责三层架构中的D层(持久层)。

二、hibernate的优点

1、Hibernate可以使用在java的任何项目中,不一定非要使用在java web项目中。因为Hibernate不需要类似于tomact这些容器的支持,可以直接通过一个main方法进行测试。

2、使用Hibernate可以大大减少代码量,深入了解oop的思想。

3、由于使用了Hibernate,代码中不涉及具体的JDBC语句,所以就方便了代码的可移植性。

4、hibernate中的hql语句,自动生成表结构的工具都方便了我们的开发。

三、hibernate实例的开发

首先需要导入需要的jar包和我的目录结构:


hibernate.cfg.xml的配置:

<!DOCTYPE hibernate-configuration PUBLIC  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  
<hibernate-configuration>  
    <session-factory >  
        <!-- mysql数据库驱动 -->  
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
        <!-- mysql数据库名称 -->  
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
        <!-- 数据库的登陆用户名 -->  
        <property name="hibernate.connection.username">root</property>  
        <!-- 数据库的登陆密码 -->  
        <property name="hibernate.connection.password">root</property>  
        <!-- 方言:为每一种数据库提供适配器,方便转换 -->  
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
        <mapping resource="com/test/java/User.hbm.xml"/>
    </session-factory>  
</hibernate-configuration>  
实体类和hbm.xml配置

package com.test.java;
import java.util.Date;  
  
public class User {  
    private String id;  
    private String username;  
    private String password;  
    private Date createTime;  
    private Date expireTime;  
      
    public String getId() {  
        return id;  
    }  
    public void setId(String id) {  
        this.id = id;  
    }  
    public String getUsername() {  
        return username;  
    }  
    public void setUsername(String userName) {  
        this.username = userName;  
    }  
    public String getPassword() {  
        return password;  
    }  
    public void setPassword(String password) {  
        this.password = password;  
    }  
    public Date getCreateTime() {  
        return createTime;  
    }  
    public void setCreateTime(Date createTime) {  
        this.createTime = createTime;  
    }  
    public Date getExpireTime() {  
        return expireTime;  
    }  
    public void setExpireTime(Date expireTime) {  
        this.expireTime = expireTime;  
    }  
}  
<?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>  
    <class name="com.test.java.User">  
        <id name="id">  
            <generator class="uuid"/>  
        </id>  
        <property name="username"/>  
        <property name="password"/>  
        <property name="createTime"/>  
        <property name="expireTime"/>  
    </class>  
</hibernate-mapping>  

通过hbm生成表结构

package com.test.java;
import org.hibernate.cfg.Configuration;  
import org.hibernate.tool.hbm2ddl.SchemaExport;  
/** 
 * 将hbm生成ddl 
 * 通过hbm生成表结构
 * @author BCH 
 * 
 */  
public class ExoprtDB {  
  
    public static void main(String[] args) {  
        //默认读取hibernate.cfg.xml文件  
        Configuration cfr = new Configuration().configure();  
          
        SchemaExport export = new SchemaExport(cfr);  
        export.create(true, true);  
    }  
}  
测试方法

package com.test.java;
import java.util.Date;  
  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;  
  
public class OperateTest {  
    public static void main(String[] args) {  
        //读取配置文件 ,获取sessionFactory,取得session对象
        Configuration cfg = new Configuration().configure();  
          
        SessionFactory factory = cfg.buildSessionFactory();  
          
        Session session = null;  
        try{  
            session = factory.openSession();  
            //开启事务  
            session.beginTransaction();  
              
            User user = new User();  
            user.setUsername("用户名");  
            user.setPassword("123");  
            user.setCreateTime(new Date());  
            user.setExpireTime(new Date());  
              
            session.save(user);  
            //提交事务  
            session.getTransaction().commit();  
              
        }catch(Exception e){  
            e.printStackTrace();  
            //回滚事务  
            session.getTransaction().rollback();  
        }finally{  
            if(session != null){  
                if(session.isOpen()){  
                    //关闭session  
                    session.close();  
                }  
            }  
        }  
    }  
} 
最后的结果如图


遇到的问题: 

SLF4J: The requested version 1.5.8 by your slf4j binding is not compatible with...

如上图标记的一样,将jar包替换到提示的版本就好了


源码地址




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值