你好,游客 登录 注册 搜索
背景:
阅读新闻

Hibernate4之Hello World(基础环境搭建)

[日期:2015-04-13] 来源:Linux社区  作者:Linux [字体: ]

刚开始复习Hibernate,刚复习时,发现全忘了,连环境搭建都不会了,等于从头再来啊,没办法硬着头皮,只得从头再来了。

Hibernate是一款优秀的ORM框架,即object relation mapping 对象关系映射。我的理解就是自动把pojo类对象的操作转为对数据库中相应表的操作。简单说就是创建一个pojo类对象,那么数据库中相应的表中也会插入这么一个对象。修改,删除,当然也是的了。可以理解就是尽可能的隔离数据库操作与java开发。

一、下载Hibernate jar包

在http://www.Hibernate.org/中找到需要的Hibernate版本。我下载的是最新的Hibernate-release-4.3.1.Final 。

二、复制jar包

复制在lib/required下的所有jar包到项目的lib文件夹下

三、部署Hibernate.cfg.xml,相应的pojo 的Student.hbm.xml

在项目的src目录下新建Hibernate.cfg.xml文件。在documentation/manual/en-US/html_single/index.html

<?xml version='1.0' encoding='utf-8'?>
<!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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">Oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:lubby</property>
        <property name="connection.username">admin</property>
        <property name="connection.password">admin</property>

   
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

     
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <!-- <property name="hbm2ddl.auto">update</property> -->

        <mapping resource="com/lubby/pojo/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

前四个标签是配置数据库的驱动地址和用户名密码。

 <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

这个是配置数据库名字和版本。在documentation/manual/en-US/html_single/index.html中能找到相应数据库对应的字段 我用的是oracle 11g 对应就是上面所写的

      <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

这个是选择是否显示sql语句。

 <mapping resource="com/lubby/pojo/Student.hbm.xml"/>

这个非常重要是告诉hibernate配置文件所配置的pojo类的配置文件地址 

四、创建pojo类Student

package com.lubby.pojo;

public class Student {
 private String sid;
 private String sname;
 private int age;
 public Student() {
  super();
  // TODO Auto-generated constructor stub
 }
 public Student(String sid, String sname, int age) {
  super();
  this.sid = sid;
  this.sname = sname;
  this.age = age;
 }
 public String getSid() {
  return sid;
 }
 public void setSid(String sid) {
  this.sid = sid;
 }
 public String getSname() {
  return sname;
 }
 public void setSname(String sname) {
  this.sname = sname;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 @Override
 public String toString() {
  return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age
    + "]";
 }
 
}

五、配置Student.hbm.xml 

每个pojo类都有一个相应的配置文件。都放在pojo类所在的包里面。

<?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 package="com.lubby.pojo">
 <class name="Student" table="tab_stu">
  <id name="sid" column="sid"></id>
  <property name="sname" column="sname"></property>
  <property name="age" column="age"></property>
 </class>
</hibernate-mapping>

<class name="Student" table="tab_stu"> </class>

name:类名 

table:数据库中对应表名

<id name="sid" column="sid"></id>

id:是设置pojo类的主键,column是数据库中对应的主键名。如果column不写,默认和name的内容一样。

<property name="sname" column="sname"></property>
<property name="age" column="age"></property>

property:设置一般的属性

六、调用hibernate

package com.lubby.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jboss.weld.logging.messages.EventMessage;

import com.lubby.pojo.Student;

public class StudentTest {
 
 public static void main(String args[]){
  Student stu = new Student();
  stu.setSid("3");
  stu.setAge(25);
  stu.setSname("李惠堂");
  Configuration  cfg = new Configuration();
  SessionFactory sf = cfg.configure().buildSessionFactory();
  Session session = sf.openSession();
  session.beginTransaction();
  session.save(stu);
  session.getTransaction().commit();
  session.close(); 
  sf.close();
 
 }
}

Hibernate整体理解 http://www.linuxidc.com/Linux/2014-07/104405.htm

Hibernate 的详细介绍请点这里
Hibernate 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-04/116141.htm

linux
相关资讯       Hello World  Hibernate4 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款