【Hibernate】(一)快速入门

来源:互联网 发布:美工刀片生锈了怎么办 编辑:程序博客网 时间:2024/06/02 04:12

hibernate

       Hibernate核心内容是ORM(关系对象模型)。可以将对象自动的生成数据库中的信息,使得开发更加的面向对象。这样作为程序员就可以使用面向对象的思想来操作数据库,而不用关心繁琐的JDBC。所以,Hibernate处于三层架构中的D层(持久层)如下图:


【优点】

    1、面向对象一体化。面向对象的分析,到面向对象的设计,到面向对象的开发,使得面向对象更加一体化,使开发更加对象化。

    2、很好的移植性。Hibernate对持久层重新做了封装,那么所有持久层的代码就具有复用性,那么修改数据库时就可以直接修改了。

    3、轻量级框架。Hibernate只需要Java环境即可,不需要容器,测试方便,提高生产力。

    4、没有侵入性。支持透明持久化。Hibernate不需要继承任何类,不需要实现任何接口。

    5.使用Hibernate可以使我们采用对象化的思维操作关系型数据库。

【缺点】

    1、使用数据库特性的语句,将很难调优

    2、对大批量数据更新存在问题

    3、系统中存在大量的攻击查询功能

【入门小程序】

1.创建Java Project,项目结构如下:


2.引入jar

        Hibernate3.jar、数据库驱动、和一些第三方jar包,下载地址:          http://download.csdn.NET/detail/u013036274/9763940

3.提供hibernate缺省配置文件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>        <!-- 数据库驱动、url、用户名、密码 -->        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first?characterEncoding=utf-8</property>        <property name="hibernate.connection.username">root</property>        <property name="hibernate.connection.password">123456</property>        <!--关于MySQL的数据库语言 -->        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="hibernate.show_sql">true</property>        <!-- 根据resource的链接找到对应的User类和数据库的映射文件 -->        <mapping resource="com/bjpowernode/hibernate/User.hbm.xml" />    </session-factory></hibernate-configuration>

4.创建持久化类User.java

package com.bjpowernode.hibernate;import java.util.Date;public class User {    private String id;    private String name;    private String password;    private Date createTime;    private Date expireTime;    // get和set方法略}

5.提供User.hbm.xml文件,完成实体映射

<?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.bjpowernode.hibernate.User">        <id name="id">            <generator class="uuid"/>        </id>        <property name="name"/>        <property name="password"/>        <property name="createTime"/>        <property name="expireTime"/>    </class></hibernate-mapping>

6.编写工具类,需要在数据库中创建对应的数据库(数据库名在hibernate.cfg.xml中),将hbm生成ddl,也就是hbm2ddl

package com.bjpowernode.hibernate;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * 将hbm生成ddl * @author happy * */public class ExportDB {    public static void main(String[] args) {        //默认读取hibernate.cfg.xml文件        Configuration cfg = new Configuration().configure();        SchemaExport export = new SchemaExport(cfg);        export.create(true, true);    }}

       运行该main方法,执行过程会先判断数据库中是否存在要插入的表(也就是我们的User表),不存在才会创建。执行效果如下:


7.客户端测试类:向表中添加数据

package com.bjpowernode.hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class Client {    public static void main(String[] args) {        //读取hibernate.cfg.xml文件        Configuration cfg = new Configuration().configure();        //建立SessionFactory        SessionFactory factory = cfg.buildSessionFactory();        //取得session        Session session = null;        try {            session = factory.openSession();            //开启事务            session.beginTransaction();            User user = new User();            user.setName("楚乔");            user.setPassword("123");            user.setCreateTime(new Date());            user.setExpireTime(new Date());            //保存User对象            session.save(user);            //提交事务            session.getTransaction().commit();        }catch(Exception e) {            e.printStackTrace();            //回滚事务            session.getTransaction().rollback();        }finally {            if (session != null) {                if (session.isOpen()) {                    //关闭session                    session.close();                }            }        }    }}

测试结果如下:


原文地址:http://blog.csdn.net/u013036274/article/details/63251774

原创粉丝点击