hibernate3.0 eclipse搭建

                                                                                                                            Hibernate3 环境搭建

之前的几篇博文都是吧东西都完了然后开始写的,虽然我在博文中打字的时候一味的追求速度难免会出现错别字,但是在一些技术细节上可能有所疏漏,所以今天开始打算边做编写,这样可以减少疏漏的地方。

 step1:在eclipse中新建一个动态web project :

点击next 然后ok就创建一个工程了。

step2:

下载hibernate3.2版本  下载地址hibernate 下载


step3:

创建hibernate配置文件(在src下面和struts.xml是一样的)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">


<hibernate-configuration>
//这里面写hibernated的相关配置
</hibernate-configuration> 

我的代码如下:

<?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">


//这是mysql的配置

<hibernate-configuration>
   <session-factory>
  <!--     指定数据库驱动
 <property name="connection.driver_class">
    com.mysql.jdbc.Driver
 </property>
 指定数据库的名称与url
 <property name="connction.url">
    jdbc:mysql://localhost/mvcdb
 </property>
 指定数据库的用户名
 <property name="connection.username">
    root
 </property>
 数据库的密码
  <property name="connection.password">
    root
  </property>
  <property name="dialect">
            org.hibernate.dialect.MySQLDialect
       </property> 
        -->
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="connection.url">jdbc:mysql://localhost/mvcdb </property> 
        <property name="connection.username">root</property>
         <property name="connection.password">root</property> 
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        
        <mapping resource="./Person.hbm.xml"/>  
   </session-factory>
</hibernate-configuration> 


//这是SqlServer的配置:

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN">


<!-- Generated by MyEclipse Hibernate Tools. -->
-<hibernate-configuration> 
-<session-factory>
<property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> 
<property name="connection.url"> jdbc:sqlserver://localhost:1433;databaseName=Drink </property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver </property> 
<property name="myeclipse.connection.profile"> Microsoft SQL Server </property>
<mapping resource="./Right.hbm.xml"/> <mapping resource="./Role.hbm.xml"/>
<mapping resource="./Member.hbm.xml"/> <mapping resource="./Product.hbm.xml"/> 
<mapping resource="./SysInfo.hbm.xml"/> <mapping resource="./Categ.hbm.xml"/> 
<mapping resource="./InStore.hbm.xml"/> <mapping resource="./OutStore.hbm.xml"/>
</session-factory>

 </hibernate-configuration>

在此配置数据库的时候要在lib下面加入相应的数据库驱动包 mysql加入 mysql-connector-java-5.1.7-bin.jar   sqlserver数据库加入sqljdbc.jar驱动包,不然的话之后会报错说没有合适的驱动Driver连接的

step4:

 新建自己的JavaBean  我这里测试随便建了一个Person

package com.soft.grand;


public class Person {
  private int id;
  private String name;
  private String sex;
  private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}

然后新建对应的bhm.xml文件  

<!DOCTYPE hibernate-mapping PUBLIC 
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping package="com.soft.grand">  
      
    <class name="com.soft.grand.Person" table="table1">  
    
        <id name="id" type="java.lang.Integer">  
             <column name="id"/>
             <generator class="native"/>
        </id>  
    <property name="name" type="java.lang.String" >
      <column name="name" length="20"/>
     </property>
    <property name="sex" type="java.lang.String" > 
      <column name="sex" length="20"/>
     </property>
    <property name="phone" type="java.lang.String"> 
       <column name="phone" length="20"/>
     </property>
  </class>  
</hibernate-mapping>  

注意:

第一点 在hibernate你配置文件中mapping路径一定要写对,我是把这两个配置文件放在一个目录下了 还有在hbm.xml中class的路径一定要写对路径,具体情况参考上面的代码。

最后一点要注意点的就是如果你配置文件中写的id 的是native那么如果你的数据库中表的建立不是主键自增的话就会出现以下错误:Caused by: java.sql.SQLException: Field 'id' doesn't have a default value主键

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值