随笔-10  评论-66  文章-1  trackbacks-0

   我写的这些只是Hibernate的一些基本的使用方法,如果你不是刚刚学Hibernate的话,这篇东东不再适合你看了,呵呵。我使用的是eclipse3.2.1+myeclipse5.1.1+Firebird 2.0,Firebrid 2.0 你可以从www.Firebirdsql.org上下载到最新版本的,如果你不想用这个数据库,你也可换成别的数据库。
 建表语句:
create database 'd:\sovo.fdb' user 'SYSDBA' password 'masterkey';
create  domain d_text as blob sub_type 1;
create table Testdomain(
id Integer primary key,
name varchar(30),
context d_text
);
打开Firebird数据库的aliases.conf,
在里面配置: sovo = d:\sovo.fdb
在这里配置,主要是为了简化在hibernate.cfg.xml里设connection.url

数据库配置文件 hibernate.cfg.xml

 1 <? xml version = ' 1.0 '  encoding = ' UTF-8 ' ?>
 2 <! DOCTYPE hibernate - configuration PUBLIC
 3            " -//Hibernate/Hibernate Configuration DTD 3.0//EN "

 4            " http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd " >
 5
 6 <!--  Generated by MyEclipse Hibernate Tools.                    -->

 7 < hibernate - configuration >
 8
 9      < session - factory >

10          < property name = " dialect " >
11             org.hibernate.dialect.FirebirdDialect
12          </ property >

13          < property name = " connection.url " >
14             jdbc:firebirdsql: // localhost:3050/sovo
15          </ property >
16          < property name = " connection.username " > SYSDBA </ property >
17          < property name = " connection.password " > masterkey </ property >
18          < property name = " connection.driver_class " >
19             org.firebirdsql.jdbc.FBDriver
20          </ property >

21          < property name = " myeclipse.connection.profile " >
22             Firebird
23          </ property >

24          < mapping resource = " com/datamodel/Testdomain.hbm.xml "   />
25
26      </ session - factory >

27
28 </ hibernate - configuration >

数据映射文件 Testdomain.hbm.xml

 1 <? xml version = " 1.0 "  encoding = " utf-8 " ?>
 2 <! DOCTYPE hibernate - mapping PUBLIC  " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
 3 " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
 4 <!--  
 5     Mapping file autogenerated by MyEclipse  -
 Hibernate Tools
 6 -->

 7 < hibernate - mapping >
 8      < class  name = " com.datamodel.Testdomain "  table = " TESTDOMAIN " >
 9          < id name = " id "  type = " java.lang.Integer " >
10              < column name = " ID "   />
11              < generator  class = " increment "   />
12          </ id >
13          < property name = " name "  type = " java.lang.String " >
14              < column name = " NAME "  length = " 30 "   />
15          </ property >
16          < property name = " context "  type = " java.lang.String " >
17              < column name = " CONTEXT "  length = " 0 "   />
18          </ property >
19      </ class >
20 </ hibernate - mapping >
21

持久化类 Testdomain.java

 1 package  com.datamodel;
 2

 3

 4
/**
 5  * Testdomain generated by MyEclipse - Hibernate Tools
 6   */

 7
 8 public   class  Testdomain   implements  java.io.Serializable 
{
 9

10

11      //  Fields    

12
13       /**
14      * 
15       */

16      private   static   final   long  serialVersionUID  =   1L ;
17      private
 Integer id;
18       private
 String name;
19       private
 String context;
20

21

22      //  Constructors

23
24      /**  default constructor  */
25      public  Testdomain()  {
26     }

27
28
    
29      /**  full constructor  */

30      public  Testdomain(String name, String context)  {
31          this .name  =
 name;
32          this .context  =
 context;
33     }

34
35
   
36      //  Property accessors

37
38      public  Integer getId()  {
39          return   this
.id;
40     }

41     
42      public   void  setId(Integer id) 
{
43          this .id  =
 id;
44     }

45
46      public  String getName() 
{
47          return   this
.name;
48     }

49     
50      public   void  setName(String name) 
{
51          this .name  =
 name;
52     }

53
54      public  String getContext() 
{
55          return   this
.context;
56     }

57     
58      public   void  setContext(String context) 
{
59          this .context  =
 context;
60     }

61    
62

63 }


测试类TestHibernate.java,对数据进行读写操作

 1 /**
 2  * 
 3   */

 4 package  com.test;
 5

 6 import
 java.util.List;
 7

 8 import
 org.hibernate.Session;
 9 import
 org.hibernate.SessionFactory;
10 import
 org.hibernate.Transaction;
11 import
 org.hibernate.cfg.Configuration;
12

13 import
 com.datamodel.HibernateSessionFactory;
14 import
 com.datamodel.Testdomain;
15

16 import
 junit.framework.TestCase;
17

18
/**
19  *  @author  dragon
20
 *
21   */

22 public   class  TestHiberante  extends  TestCase {
23
    
24      private
 Session session;
25      private
 Transaction tx;
26
    
27      protected   void  setUp()
{
28
        
29          try
{
30         Configuration cfg  =   new  Configuration().configure( " /com/datamodel/hibernate.cfg.xml "
);
31         SessionFactory sf  =
 cfg.buildSessionFactory(); 
32           session  =
 sf.openSession();
33           tx  =
 session.beginTransaction();
34         }
catch  (Exception e) {
35
            e.printStackTrace();
36         }
    
37
    
38     }

39 //    保存数据
40      public   void  testSave() {
41         Testdomain td  =   new
 Testdomain();
42         td.setName( " dragon "
);
43         td.setContext( " 我爱北京天安门! "
);
44
        
45
        session.save(td);
46
            
47     }

48 //     更新数据
49      public   void  _testUpdate() {
50        Integer id  = new  Integer( 1
);
51        Testdomain td  = (Testdomain) session.get(Testdomain. class
, id);
52        td.setName( " javadragon "
);
53
       session.update(td);
54     }

55     
56 //     查询全部数据

57      public   void  testLoad() {
58       List list =  session.createQuery( " from Testdomain "
).list();
59        for  ( int  i  = 0 ; i  <  list.size(); i ++ )
{
60           Testdomain td  =
(Testdomain) list.get(i);
61
          
62            System.out.println(td.getName() + "    " +
td.getContext()); 
63       }

64     }

65 //     删除指定的某条数据
66      public   void  testDelete() {
67        Integer id  =   new  Integer( 1
);
68        Testdomain td  =  (Testdomain) session.get(Testdomain. class
, id);
69
       session.delete(td);
70     }

71     
72
    
73      protected   void  tearDwon()
{
74
        tx.commit();
75
        session.close();
76     }

77 }

78
posted on 2007-04-29 22:56 javadragon 阅读(640) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: