Hibernate的基本配置和一个简单的实例(含思维导图)

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

优点:
1.Hibernate功能强大,较之JDBC方式操作数据库,代码量大大减少,提高持久化代码的开发速度,降低维护成本。

2.Hibernate支持许多面向对象的特性

3.可移植性好

4.Hibernate框架开源免费

缺点:

1.不适合以数据为中心的大量使用存储过程的应用。

2.大规模的批量插入、修改和删除不适合用HibernateHibernate环境搭建
下面说一下hibernate的配置过
1.导包。
导入Hibernate必须的jar包。下载地址http://download.csdn.net/download/zxfjnfjcn/10038308
2.配置hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/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/BJCSZH?characterEncoding=utf8&amp;useSSL=false</property>
      <-- 下面配置用户名 -->
      <property name="hibernate.connection.username">root</property>
      <-- 下面配置用户密码 -->
      <property name="hibernate.connection.password">root</property>
      <-- 显示数据库的执行语句 -->
      <property name="show_sql">true</property>
      <-- 下面配置usr.hbm.xml文件 -->
      <mapping resource="com/lanou/hibernate/mapping/user.hbm.xml"></mapping>
    </session-factory>

</hibernate-configuration>

3.配置user.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.lanou.hibernate.bean.User" table="tb_user">
        <id name="id" type="int">   
            <generator class="native"></generator>  <!-- id(主键)的生成规则 -->    
        </id>
        <property name="username"></property>
        <property name="pwd"></property>
    </class>
</hibernate-mapping>

以上是基本的配置文件。
3.新建实体类文件User.java

public class User {

    private int id;

    private String username;

    private String pwd;
    //----------------下面是set & get方法,已省略------------
}

4.新建测试类 UserMain.java

package com.lanou.hibernate; //包

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import com.lanou.hibernate.bean.User;

public class UserMain {
    public static void main(String[] args) {
        Configuration config;
        SessionFactory sf = null;
        Session session;
        Transaction tx = null;
        try {
            config = new Configuration().configure();
            sf = config.buildSessionFactory();
            session = sf.openSession();
            tx = session.beginTransaction();

            String hql = "from User where username=:username and pwd=:pwd";
            Query<User> query = session.createQuery(hql);
//          query.setFirstResult(0);
//          query.setMaxResults(10);

            query.setParameter("username", "zx");
            query.setParameter("pwd", "1");

            List<User> list = query.getResultList();
            System.out.println("查出来了"+list.size()+"条数据");
            for (User user : list) {
                System.out.print(user.getId()+"     ");
                System.out.print(user.getUsername()+"      ");
                System.out.println(user.getPwd());
            }
            tx.commit();            
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
        System.exit(0);
    }
}

思维导图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值