古之成大事者,不唯有超世之才,亦唯有坚韧不拔之志也!

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  3 随笔 :: 44 文章 :: 1 评论 :: 0 Trackbacks
1: 创建工程并添加必要的java包,创建完成后工程如下:
   

2:配置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 <hibernate-configuration>
 7 
 8 <session-factory>
 9     <property name="connection.username">root</property>
10     <property name="connection.password">root</property>
11     <property name="connection.url">
12         jdbc:mysql://127.0.0.1:3306/ssh0
13     </property>
14     <property name="dialect">
15         org.hibernate.dialect.MySQLDialect
16     </property>
17     <property name="connection.driver_class">
18         com.mysql.jdbc.Driver
19     </property>
20     <mapping resource="com/lei/vo/ADM_MENU.hbm.xml" />
21     <!--mapping resource="com/lei/vo/USER.hbm.xml" />  -->
22 
23 </session-factory>
24 
25 </hibernate-configuration>

3:编写对象的hbm文件,如:ADM_MENU.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 <hibernate-mapping>
 5     <class name="com.lei.vo.ADM_MENU" table="ADM_MENU" catalog="ssh0">
 6         <id name="MENUID" type="string">
 7             <column name="MENUID" length="20" not-null="true" />
 8             <generator class="assigned" />
 9         </id>
10         <property name="MENUNAME" type="string">
11             <column name="MENUNAME" length="20" not-null="true" />
12         </property>
13         <property name="PARANTID" type="string">
14             <column name="PARANTID" length="20" not-null="true" />
15         </property>
16         <property name="URL" type="string">
17             <column name="URL" length="100" not-null="true" />
18         </property>
19     </class>
20 </hibernate-mapping>
21


4: 编写ant脚本bulid.xml
<?xml version="1.0" encoding="GBK"?>

<project name="hibernate-tutorial" default="compile">

    
<property name="sourcedir" value="${basedir}/src"/>
    
<property name="targetdir" value="${basedir}/bin"/>
    
<property name="librarydir" value="${basedir}/WebContent/WEB-INF/lib"/>
    
<property name="schema.dir" value="${basedir}/data"/> 
    
    
<path id="libraries">
        
<fileset dir="${librarydir}">
            
<include name="*.jar"/>
        
</fileset>
    
</path>
    
<path id="project.class.path">
    
<!-- Include our own classes, of course -->
        
<pathelement location="${targetdir}" />
    
<!-- Include jars in the project library directory -->
        
<fileset dir="${librarydir}">
            
<include name="*.jar"/>
        
</fileset>
    
</path>

    
<target name="clean">
        
<delete dir="${targetdir}"/>
        
<mkdir dir="${targetdir}"/>
    
</target>

    
<target name="copy-resources">
        
<copy todir="${targetdir}">
            
<fileset dir="${sourcedir}">
                
<exclude name="**/*.java"/>
            
</fileset>
        
</copy>
    
</target>

    
<target name="compile" depends="clean, copy-resources">
        
<javac srcdir="${sourcedir}"
            destdir
="${targetdir}"
            classpathref
="libraries"/>
    
</target>

    
<target name="run" depends="compile">
        
<java fork="true" classname="ergal.BusinessService" classpathref="libraries">
            
<classpath path="${targetdir}"/>
            
<arg value="${action}"/>
        
</java>
    
</target>
    
<!-- create .java form  *.hbm.xml -->
    
<target name="hbm2java"  depends="compile"
        description
="Generate Java source from the O/R mapping files">
        
<taskdef name="hbm2java" 
            classname
="org.hibernate.tool.ant.HibernateToolTask" 
            classpathref
="project.class.path"/>
            
<hbm2java destdir="${sourcedir}">
                
<configuration configurationfile="${targetdir}/hibernate.cfg.xml" />  
                
<hbm2java  jdk5="true"/>
                
<!-- <cfg2hbm/> --> 
            
</hbm2java>  
    
    
</target>
    
    
<target name="hbm2javaonly" depends="copy-resources"
        description
="Generate Java source from the O/R mapping files">
        
<taskdef name="hbm2java" 
            classname
="org.hibernate.tool.ant.HibernateToolTask" 
            classpathref
="project.class.path"/>
            
<hbm2java destdir="${sourcedir}">
                
<configuration configurationfile="${targetdir}/hibernate.cfg.xml" />  
                
<hbm2java  jdk5="true"/>
                
<!-- <cfg2hbm/> --> 
            
</hbm2java>  
    
    
</target>
    
    
<!-- create ddl form  *.hbm.xml -->
    
<target name="hbm2ddl" depends="compile" 
            description
="Generate DB schema from the O/R mapping files">
        
<taskdef name="hbm2ddl" 
            classname
="org.hibernate.tool.ant.HibernateToolTask" 
            classpathref
="project.class.path"/>
        
<hbm2ddl destdir="${schema.dir}">
            
<configuration configurationfile="${targetdir}/hibernate.cfg.xml" /> 
            
<hbm2ddl export="true" console="false" create="true" update="false" drop="false" outputfilename="orm_createdb.sql"/> 
        
</hbm2ddl>   
    
</target>
    
    
<!-- create ddl form  *.hbm.xml -->
    
<target name="hbm2ddlonly" depends="copy-resources"
            description
="Generate DB schema from the O/R mapping files">
        
<taskdef name="hbm2ddl" 
            classname
="org.hibernate.tool.ant.HibernateToolTask" 
            classpathref
="project.class.path"/>
        
<hbm2ddl destdir="${schema.dir}">
            
<configuration configurationfile="${targetdir}/hibernate.cfg.xml" /> 
            
<hbm2ddl export="true" console="false" create="true" update="false" drop="true" outputfilename="orm_createdb.sql"/> 
        
</hbm2ddl>   
    
</target>

</project>

5:在eclipse outline视图中执行hbm2ddlonly生成数据库脚本并建库和hbm2javaonly生成java对象

6:编写测试案例测试hibernate
  

 1 package com.lei.util;
 2 
 3 import org.hibernate.SessionFactory;
 4 import org.hibernate.cfg.Configuration;
 5 
 6 public class HibernateUtil {
 7 
 8     private static final SessionFactory sessionFactory;
 9 
10     static {
11         try {
12             // Create the SessionFactory from hibernate.cfg.xml
13             sessionFactory = new Configuration().configure().buildSessionFactory();
14         } catch (Throwable ex) {
15             // Make sure you log the exception, as it might be swallowed
16             System.err.println("Initial SessionFactory creation failed." + ex);
17             throw new ExceptionInInitializerError(ex);
18         }
19     }
20 
21     public static SessionFactory getSessionFactory() {
22         return sessionFactory;
23     }
24 
25 }

 1 package com.lei.test.hibernate;
 2 
 3 import org.hibernate.Session;
 4 import org.junit.After;
 5 import org.junit.Assert;
 6 import org.junit.Before;
 7 import org.junit.Test;
 8 
 9 import com.lei.util.HibernateUtil;
10 import com.lei.vo.ADM_MENU;
11 
12 public class TestHibernate {
13 
14     private Session session ;
15     
16     @Before
17     public void setUp() throws Exception {
18          session = HibernateUtil.getSessionFactory().openSession();
19          session.beginTransaction();
20     }
21 
22     @After
23     public void tearDown() throws Exception {
24         session.getTransaction().rollback();
25         session.close();
26     }
27     
28     @Test
29     public void testAddAndQuery(){
30         ADM_MENU aMenu = new ADM_MENU();
31         aMenu.setMENUID("10000");
32         aMenu.setMENUNAME("测试");
33         aMenu.setPARANTID("100000");
34         aMenu.setURL("10000.action");
35         session.save(aMenu);
36         ADM_MENU menu1 = (ADM_MENU) session.get(ADM_MENU.classnew String("10000"));
37         Assert.assertEquals(menu1.getPARANTID(), "100000");
38     }
39     
40     @Test
41     public void testDelete(){
42         
43 
44     }
45 
46 }
47 

7:运行junit测试案例,看到绿色了把
posted on 2008-12-12 14:54 goto 阅读(451) 评论(0)  编辑  收藏 所属分类: SSH

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


网站导航: