第一个成功的Hibernate实例

第一个成功的Hibernate实例


数据库设计
注意:自增主键这一功能将在以后的Mapping中由Hibernate 实现,在 Hibernate 中有很多种主键的生成算法

USE master

DROP DATABASE RMSDB
CREATE DATABASE RMSDB
GO

USE RMSDB

--create base tables
DROP TABLE Houses
CREATE TABLE Houses
(
HID INT PRIMARY KEY,
--房间编号
alias VARCHAR(20),
--房间的别称
description VARCHAR(100),
--房间的描述
capity INT NOT NULL
--房间的旅客容量
)
DROP TABLE Menus
CREATE TABLE Menus
(
MID INT PRIMARY KEY,
--菜单编号
mName VARCHAR(20) NOT NULL,
--菜单名称
price FLOAT NOT NULL
--菜单价格
)
DROP TABLE Tables
CREATE TABLE Tables
(
TID INT PRIMARY KEY,
tAlias VARCHAR(20)
)
DROP TABLE Customers
CREATE TABLE Customers
(
CID VARCHAR(20) PRIMARY KEY,
cName VARCHAR(20),
cAddress VARCHAR(40),
cPhone VARCHAR(20)
)

DROP TABLE Staffs
CREATE TABLE Staffs
(
SID VARCHAR(6) PRIMARY KEY,
username VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL,
sName VARCHAR(20) NOT NULL,
address VARCHAR(40),
phone VARCHAR(20),
sPosition VARCHAR(20)
--position including manager or common staffs
)

DROP TABLE Notice
CREATE TABLE Notice
(
NID INT PRIMARY KEY,
title VARCHAR(20) NOT NULL,
content VARCHAR(200)
)

GO
二、在Eclipse中生成POLO和映射文件
1) 在Eclipse中配置环境,包括数据库驱动程序
2) 生成POLO类和映射文件

3)不会用以上两种方法的同志请参阅MyEclipse的帮助文档,此处省略
4)此处以notice为例,生成的原代码和映射文件如下:

原代码:

注意:如果把get方法写为getname()或者getNAME(),会导致Hibernate在运行时抛出以下异常:
  
      net.sf.hibernate.PropertyNotFoundException:
     Could not find a getter
     for property name in class mypack.Customer

          如果已经出现这个异常,请您改下大小写

 

package  com.demo.hibernate;

public   class  Notice  {

    
private int nid;
    
private String title;
    
private String content;
    
    
public String getContent() {
        
return content;
    }

    
public void setContent(String content) {
        
this.content = content;
    }

    
public int getNid() {
        
return nid;
    }

    
public void setNid(int nid) {
        
this.nid = nid;
    }

    
public String getTitle() {
        
return title;
    }

    
public void setTitle(String title) {
        
this.title = title;
    }
    
}

 

配置文件:Notice.hbm.xml

 

<? 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 - Hibernate Tools
-->
< hibernate-mapping >
    
< class  name ="com.demo.hibernate.Notice"  table ="notice"  catalog ="rmsdb" >
        
< id  name ="nid"  type ="java.lang.Integer" >
            
< column  name ="NID"   />
            
< generator  class ="increment" ></ generator >
        
</ 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 ="200"   />
        
</ property >
    
</ class >
</ hibernate-mapping >

 

生成的hibernate.cfg.xml

 

<? 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 ="connection.username" > root </ property >
        
< property  name ="connection.url" >
            jdbc:mysql://localhost:3306/rmsdb
        
</ property >
        
< property  name ="dialect" >
            org.hibernate.dialect.MySQLDialect
        
</ property >
        
< property  name ="myeclipse.connection.profile" > MySQL </ property >
        
< property  name ="connection.password" > admin </ property >
        
< property  name ="connection.driver_class" >
            com.mysql.jdbc.Driver
        
</ property >
        
< mapping  resource ="com/demo/hibernate/Customers.hbm.xml"   />
        
< mapping  resource ="com/demo/hibernate/Houses.hbm.xml"   />
        
< mapping  resource ="com/demo/hibernate/Menus.hbm.xml"   />
        
< mapping  resource ="com/demo/hibernate/Notice.hbm.xml"   />
        
< mapping  resource ="com/demo/hibernate/Staffs.hbm.xml"   />
        
< mapping  resource ="com/demo/hibernate/Tables.hbm.xml"   />

    
</ session-factory >

</ hibernate-configuration >

 

完成插入Notice表的代码,可将此代码放到NoticeDAO文件中,也可放在main中,此例中,我放在了NoticeDAO文件中,代码如下:

 

                     Configuration configuration;
    SessionFactory sessionFactory;
    Session session;
    
    
public   void  saveNotice(Notice notice) {
        configuration 
= new Configuration().configure();
        sessionFactory 
= configuration.buildSessionFactory();
        session 
= sessionFactory.openSession();
        Transaction tx 
= session.beginTransaction();
        session.save(notice);
        tx.commit();
        session.close();
    }

 

在主函数中调用即可完成,主函数代码如下:

 

public   class  ExecuteTransaction {
    
    
    
    
public static void main(String[] args){
        Notice notice 
= new Notice();
        notice.setTitle(
"NoticeTitle");
        notice.setContent(
"NoticeContent");
        
        NoticeDAO noticeDAO 
= new NoticeDAO();
        
for(int i=0;i<50;i++){
        noticeDAO.saveNotice(notice);
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值