MyEclipse + Hibernate 配置

Hibernate是一个ORMapping框架,对象和数据库关系映射,提供强大的对象和数据库之间的映射,从而通过调用类的方法代替SQL语句操作。这样J2EE框架可以分为表示层,业务逻辑层,数据持久化层,数据库几层。

使用版本:

MyEclipse8.5

Hibernate (hibernate-3.2.6.ga.zip)

新建WEB项目,在项目的Lib中拷贝hibernate3.jar和hibernate中lib下的所有jar包

操作步骤:

(1)创建数据库

使用MySQL简历数据库testdatabase,创建数据库表notebook

 

 

(2)建立对应的持久化类POLO类,只包含setter与getter类

注意各个属性与数据库中数据属性要对应

Notebook.java(置于src下的notebook包内)

package notebook;

public class Notebook {


 private String noteId;
 private String title;
 private String content;
 private String noter;


 public String getNoteId() {
  return noteId;
 }
 public void setNoteId(String noteId) {
  this.noteId = noteId;
 }
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
 public String getNoter() {
  return noter;
 }
 public void setNoter(String noter) {
  this.noter = noter;
 }
 
}

(3)持久化类和关系数据库映射

hibernate.cfg.xml对数据库环境进行配置(置于src文件包下)

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

 <session-factory>
  <property name="dialect">
   org.hibernate.dialect.MySQLDialect
  </property>
  <property name="connection.url">
   jdbc:mysql://localhost:3306/testdatabase
  </property>
  <property name="connection.username">root</property>
  <property name="connection.password">123</property>
  <property name="connection.driver_class">
   com.mysql.jdbc.Driver
  </property>
  <property name="myeclipse.connection.profile">
   com.mysql.jdbc.Driver
  </property>
  <mapping resource="notebook/Notebook.hbm.xml" />

 </session-factory>

</hibernate-configuration>

 

Notebook.hbm.xml映射文件描述类与表之间的关系,把其放在src文件夹下的notebook包内

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="notebook.Notebook" table="notebook" catalog="testdatabase">
        <id name="noteId" type="java.lang.String">
            <column name="NoteId" length="5" />
            <generator class="assigned" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="Title" length="20" not-null="true" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="Content" length="50" />
        </property>
        <property name="noter" type="java.lang.String">
            <column name="Noter" length="20" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

 

(4)编写session操作

 

在hibernate里任何数据库的操作都是通过session进行的,

 

a.首先获取session ,session是一个接口,必须通过其他类实例化

private Session se = null;

 

b.找到数据库配置
  Configuration config = new Configuration().configure();

 

c.找到工厂
   SessionFactory sf = config.buildSessionFactory();

 

d.从工厂取出session
  this.se = sf.openSession();

 

(5)编写应用

插入

 public void insert(Notebook book)
 {
  Transaction tx = this.se.beginTransaction();
  this.se.save(book);
  tx.commit();
 }

更新
 public void update(Notebook book)
 {
  Transaction tx = this.se.beginTransaction();
  this.se.update(book);
  tx.commit();
  
 }

步骤(4)(5)d写在一个类里Operation.java,置于notebook包中

代码如下:

 

package notebook;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

 

public class Operation {
 private Session se = null;
 //在构造方法之中实例化对象
 public Operation(){
  //找到配置
  Configuration config = new Configuration().configure();
     //找到工厂
  SessionFactory sf = config.buildSessionFactory();
  //从工厂中取出session
  this.se = sf.openSession();
 }
    //所有的操作都是通过session进行的
 public void insert(Notebook book)
 {
  Transaction tx = this.se.beginTransaction();
  this.se.save(book);
  tx.commit();
 }
 public void update(Notebook book)
 {
  Transaction tx = this.se.beginTransaction();
  this.se.update(book);
  tx.commit();
  
 }
 
}

 

(6)编写jsp

<%@ page import="java.util.*,notebook.*,org.hibernate.Session,org.hibernate.*" %>

     <%
        Notebook b = new Notebook();
  b.setNoteId("2324");
  b.setContent("2");
  b.setNoter("2");
  b.setTitle("2");
  Operation op = new Operation();
  op.insert(b);
  %>

发布运行即可

 提示:

 也可以用MyEclipse中自带的Hibernate,步骤MyEclipse->add hibernate capabilities 然后依据里边的提示可自动生成

hibernate.cfg.xml,然后可以在window->open perspective ->Database explorer 中连接好数据库后,点击需要映射的

数据库表右键 选择Hibernate Reverse Engineering 然后依据提示就可建立notebook.hbm.xml表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值