Hibernate正向工程建立数据表 对数据表进行增删改查操作

步骤一

         首先在数据库中建立一数据库  Person

步骤二

在myEclipse 中建立一普通java工程 HibernateTest

建立一lib文件夹 

拷贝需要的相应hibernate中的jar包


其中最后一个是 数据库驱动包 ,然后从hibernate源文件那个解压文件中的project 中的etc(配置文件)文件夹中 拷贝hiberna.cfg.xml

  


把hiberna.cfg.xml 拷贝到config中  打开之后 删除<session-Factory name=”foo”中的所有内容> 首先来配置主要的 hiberna.cfg.xml   配置这个的时候我们要参考 hibernate.properties

因为我是用的sqlserver的所以选择这个


 

配置完后如下所示

 <hibernate-configuration>
<session-factory name="foo">
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Person</property>

<property name="hibernate.hbm2ddl.auto">update</property>
<hibernate-configuration>
<session-factory name="foo">
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Person</property>

<property name="hibernate.hbm2ddl.auto">update</property>

<!-- 这个是最后添加建立,配置好的Person.hbm.xml-->
<mapping resource="com/hibernate/test/Person.hbm.xml"/>


</session-factory>


</hibernate-configuration>


切记这个也可以在hibernate.properties中可以找到

#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate



<property name="hibernate.hbm2ddl.auto">update</property>

 

第一个貌似是创建之后就删除掉

第二个是本次创建的时候就删除上次创建的

第三个为更新 就是就算你上次创建过的本次创建添加了也不会删除上次的内容

所以这里我们选择update

最后我们来创建表中所需要的内容  因为只是做个测试 所以简单的创建一个类

TestCreateDB

创建完之后 我们就可以去配置关于这个类的 hibernate配置文件了

 

这就需要拷贝 ect中的employee.hbm.xml 拷贝到当前类的包的下面 并且改名为 类名.cfg.xml(Person.cfg.cml)

然后我们来配置Person.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">
<hibernate-mapping>
    <class name="com.hibernate.test.Person" table="tb_Person">
<id name="pid" type="integer">
    <generator class="native"></generator>
</id>
<property name="uname" type="string">
   
</property>
<property name="upwd" type="string">
   
</property>
</class>
</hibernate-mapping>

 

这个相信大家一看就能明白吧  <property>中的name值就是表中的字段名  type就是字段的属性 

 

注意的是第一个 是创建的这张表的主键 自增长 (好像hibernate中是强制这样要求的) 第一列 class 就是当前类的全类名 table 就是需要建立的表的名字  好了到这里了准备工作差不多就做完了

 

 

最后 我们用junit(昨天才学的)进行测试

 

创建一个测试类

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.junit.Test;

 

import com.hibernate.test.Person;

public class TestCreateTable {

 

    private static Configuration cf;

//这里为了以后方便 一次创建了 Configuration

static{

       cf=new Configuration();

//这里因为我创建了一个config sourc folder 里面有一个hibernate的配置文件

//(hibernate.cfg.xml)

       cf.configure("hibernate/hibernate.cfg.xml");

      

    }

    public static SessionFactory getSessionFactory(){

/返回sessionFactory     

return cf.buildSessionFactory();

    }

    @Test

    public void testCreateDb(){

       //开启session

       Session session=getSessionFactory().openSession();

       //开启事务(一定要开启事务和提交事务,不然不会成功)

       Transaction tr=session.beginTransaction();

      

       Person person=new Person();

       person.setUname("张三");

       person.setUpwd("123");

      //保存到数据中

session.save(person);

       tr.commit();

      

    }

}

 

到这里就成功的完成了 

嘿嘿 第一次发这种自己感觉有点点技术含量的帖子,不知道有哪里做得不好的 望大家指出啊。以后我会改正的。


(才学ssh,很多东西不懂,所以就把最近所学的一些东西放上来  供大家参考和指出不正确的地方)

 

转载于:https://my.oschina.net/u/210432/blog/94578

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值