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

            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 閱讀(465) 評論(0)  編輯  收藏 所屬分類: SSH

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 中方县| 山阳县| 永新县| 威远县| 石泉县| 博白县| 永顺县| 博野县| 凭祥市| 新余市| 贵定县| 白山市| 开封县| 嵩明县| 大邑县| 宁陕县| 甘肃省| 陇川县| 平顶山市| 石渠县| 冕宁县| 儋州市| 德安县| 长白| 永胜县| 丰县| 石嘴山市| 理塘县| 天台县| 东乌| 尉氏县| 石河子市| 扶绥县| 巴楚县| 怀宁县| 中方县| 德江县| 巩留县| 正蓝旗| 肥东县| 莱州市|