posts - 138, comments - 150, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2007年12月24日

圣诞将至,许下了一个愿望,希望程序员都是幸福的,快乐的,没有烦恼,没有委屈,爱自己,幸福
(关键的是写出来的代码全都是绿的!)


posted @ 2007-12-24 18:23 G_G 阅读(841) | 评论 (8)编辑 收藏

学习xdoclet 发现是很有用的。易于规范,使用方便;
参考: http://blog.easyjf.com/html/blog/20061127/3290288676803397.htm
官方完整API: Template Language (*.xdt)

因为参考中:是还是比较复杂的例(数本人愚钝^_^)特留一文:
1>ant编辑文件
<?xml version="1.0" encoding="UTF-8"?>
<project name="xdocletExample" default="doall" basedir=".">
    
<property name="basedir" location="E:\ue_test\xdoclet" />
    
<property name="xdoclet.lib.dir" location="D:\hbn\hibernate-3.2\xdoclet-1.2.3\lib" />

    
<property name="gen.src.dir" location="${basedir}/target" />
    
<property name="src.dir" location="${basedir}/src" />
    
<property name="template.dir" location="${basedir}/template" />
    
<property name="customtag.dir" location="${basedir}/bin" />

    
<path id="xdoclet.lib.path">
       
<fileset dir="${xdoclet.lib.dir}" includes="*.jar" />
    
</path>

    
<taskdef name="xdoclet" classname="xdoclet.DocletTask" classpathref="xdoclet.lib.path" />   

    
<target name="init" />

    
<target name="daogener" depends="init">
       
<xdoclet destdir="${gen.src.dir}">
           
<fileset dir="${src.dir}" includes="${cjava.file}" />
           
<template templateFile="${xdt.file}" acceptInterfaces="false" acceptAbstractClasses="false" destinationfile="{0}DAO.java" />
       
</xdoclet>
    
</target>

    
<target name="doall" depends="daogener,daoimplgener" />

</project>

在运行上面文件时候动态输入参数
 1.依照的 .java 文件( cjava.file );
 2.依照的 .xdt.file 文件( xdt.file );

比如DOS中输入:
E:\ue_test\xdoclet>ant daogener
-Dxdt.file=template\daointerface.xdt
-Dcjava.file=test\TestXDoclet.java

2.参考的java文件(简单过头了 呵呵)
package test;
  
/**
  * @hasRef
  
*/
public class TestXDoclet{
    
/**
     * @ref.model name="sytp"
     
*/
    
private PubSystype sytp;

    
private String gg;
}

3.xdt 模板
  标签解释:
  <XDtClass:ifHasClassTag tagName="hasRef">判断 java文件中有 @hasRef
 
<XDtField:forAllFields1> 循环查找
 
  2>判断有@ref.model name=。。
  <XDtField:ifHasFieldTag tagName="ref.model" paramName="name">
 
  3>取值
  
<XDtField:fieldTagValue tagName="ref.model" paramName="name" />  

package com.hycs.bs.client.itf;

import java.util.List;

//这是 packageName.className 这和好理解
import 
<XDtPackage:packageName />.<XDtClass:className />;

public interface 
<XDtClass:className />DAO {

    //添加
    boolean add(
<XDtClass:className /> instance);
    
    //逻辑添加
    
<XDtClass:ifHasClassTag tagName="hasRef">
        boolean add(
<XDtClass:className /> instance,
                
<XDtField:forAllFields>
                    
<XDtField:ifHasFieldTag tagName="ref.model" paramName="name"> 
                        String 
<XDtField:fieldTagValue tagName="ref.model" paramName="name" />
                    
</XDtField:ifHasFieldTag>
                
</XDtField:forAllFields>);  
    
</XDtClass:ifHasClassTag>

    //更新
    boolean update(
<XDtClass:className /> instance);


    //得到一个对象
    
<XDtClass:className /> get(String pkid);
}

运行DOS->
E:\ue_test\xdoclet>ant daogener
-Dxdt.file=template\daointerface.xdt
-Dcjava.file=test\TestXDoclet.java

得到的代码:
package com.hycs.bs.client.itf;

import java.util.List;

//这是 packageName.className 这和好理解
import test.TestXDoclet;

public interface TestXDocletDAO {

    
//添加
    boolean add(TestXDoclet instance);
    
//逻辑添加
        boolean add(TestXDoclet instance,String sytp); 
    
//更新
    boolean update(TestXDoclet instance);

    
//得到一个对象
    TestXDoclet get(String pkid);
}






posted @ 2007-12-24 17:54 G_G 阅读(819) | 评论 (0)编辑 收藏

为了了解hbm的rmic真正的威力还有为了更近一步了解;在此特使用文本和dos命令完成这次学习。
在此读者最好先做好不使用 IDE 的准备(^_^!  文本编辑,dos编辑和运行)
阅读需要:
 java,javac,rmiregistry,rmic,ant 等命令有所涉及(只是简单使用,不会也别怕!)

可行性使用说明:
  持久层程序员成功开启rmiregistry服务,在局域网内其他使用数据的队友就只要加载
hbmBean 的映射jar和 dao接口jar后 就可以取得数据。根本感觉不到hbn使用。
优点:
1.其他队员编译classpath中不需要hbm的任何东西,跟关注自己东西。
2.强制接口规范。
3.正交编程。
4.DAO测试性能方便。
5.这还是 EJB 原理,转换容易。

缺点:
1.rmic 突破放火墙能力有限,可以换成EJB
2.大型项目注册服务器,配置繁琐,可以使用EJB3.0
 


使用:
1.jdk1.5
2.jar使用(为了用hbm)     
    jta.jar;
    asm.jar;
    commons-collections-2.1.1.jar;
    commons-logging-1.0.4.jar;
    hibernate3.jar;
    dom4j-1.6.1.jar;
    cglib-2.1.3.jar;
    antlr-2.7.5H3.jar
    MYSQL.jar
3.ant1.7(使用hbm生成 mapping;config...)
4.Middlegen-Hibernate-r5 同上
5.使用文件路径
  E:\rmi_test->
     +-src
     --hibernate3.xml
     --hibernate3_ant.properties

开始:
 1>使用ant 对 hbm  配置映射参考 :ant 项目实际 并使用 hibernate3.xml
    dos 输入 -> ant -f hibernate3.xml ddl2hbm (使用了Middlegen-Hibernate-r5)
             -> ant -f hibernate3.xml create_config (hibernate.cfg.xml建立)
    在 hibernate.cfg.xml中添加-> <mapping resource="hibernate\Liu.hbm.xml" />
    dos 输入 -> ant -f hibernate3.xml hbm2java
 
变成+-src
       |-hibernate
          |-Liu.hbm.xml
          |-Liu.java
       |-hbmConfig
          |-hibernate.cfg.xml
 2>手动建hbmHibernateSessionFactory在hbmConfig文件中(为了简单copy->myeclipes生成的)
package   hbmConfig; 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {
@link http://hibernate.org/42.html}.
 
*/
public class HibernateSessionFactory {

    
/** 
     * Location of hibernate.cfg.xml file.
     * NOTICE: Location should be on the classpath as Hibernate uses
     * #resourceAsStream style lookup for its configuration file. That
     * is place the config file in a Java package - the default location
     * is the default Java package.<br><br>
     * Examples: <br>
     * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
     * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code> 
     
*/
    
private static String CONFIG_FILE_LOCATION = "/hbmConfig/hibernate.cfg.xml";

    
/** Holds a single instance of Session */
    
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

    
/** The single instance of hibernate configuration */
    
private static final Configuration cfg = new Configuration();

    
/** The single instance of hibernate SessionFactory */
    
private static org.hibernate.SessionFactory sessionFactory;

    
/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  
@return Session
     *  
@throws HibernateException
     
*/
    
public static Session currentSession() throws HibernateException {
        Session session 
= (Session) threadLocal.get();

        
if (session == null) {
            
if (sessionFactory == null) {
                
try {
                    cfg.configure(CONFIG_FILE_LOCATION);
                    sessionFactory 
= cfg.buildSessionFactory();
                }
                
catch (Exception e) {
                    System.err.println(
"%%%% Error Creating SessionFactory %%%%");
                    e.printStackTrace();
                }
            }
            session 
= sessionFactory.openSession();
            threadLocal.set(session);
        }

        
return session;
    }

    
/**
     *  Close the single hibernate session instance.
     *
     *  
@throws HibernateException
     
*/
    
public static void closeSession() throws HibernateException {
        Session session 
= (Session) threadLocal.get();
        threadLocal.set(
null);

        
if (session != null) {
            session.close();
        }
    }

    
/**
     * Default constructor.
     
*/
    
private HibernateSessionFactory() {
    }

}

dos中编译->
E:\rmi_test\src>javac -cp   .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\hibernate3.jar   
hbmConfig/*.java


3>DAO实现
接口->
package rmic;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface IHello extends Remote {
    
public String sayHello(String id) throws RemoteException;
}

实现类->
package rmic;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
//取消显示指定的编译器警告!
//参考 : http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/lang/SuppressWarnings.html
@SuppressWarnings("serial")
public class Hello extends UnicastRemoteObject implements IHello {
    
public Hello() throws RemoteException {
        
super();
    }
    
    
public void rebind(String name) { 
        
try {
            Naming.rebind(name,
this);
            System.out.println(
"Server is running");
        } 
catch(Exception e) {
            e.printStackTrace();
        }
    }
    
public String sayHello(String id) throws RemoteException {
        System.out.println(
"run Server.");
        
    org.hibernate.Session session 
= hbmConfig.HibernateSessionFactory.currentSession();
    org.hibernate.Query qu 
= session.createQuery("from Liu lt where lt.id=:id");
    hibernate.Liu ll 
= (hibernate.Liu)qu.setString("id"
,id).uniqueResult();
    hbmConfig.HibernateSessionFactory.closeSession();


        
return "Hello "+ll.getName()+" This is processed by RMI";
    }

}

dos编译->
E:\rmi_test\src>javac -cp .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.
2\hibernate3.jar  rmic/*.java

4>java注册机开启
E:\rmi_test\src>rmiregistry 1099

5> 本例简单服务器开启
package start;

import java.rmi.RMISecurityManager;
import rmic.Hello ;

public class Start {

    
public static void main(String[] args) {
        
try {
            
//System.setSecurityManager(new RMISecurityManager());
            new Hello().rebind("RMI/Mclaren");
        } 
catch(Exception e) {
            e.printStackTrace();
        }
    }
}

dos编译->E:\rmi_test\src>javac  start/*.java
运行->
java -classpath .;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\jta.jar;
D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\asm.jar;
D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\commons-collections-2.1.1.jar;
D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\commons-logging-1.0.4.jar;
D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\hibernate3.jar;
D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\dom4j-1.6.1.jar;
E:\lib\MYSQL.JAR;D:\hbn\hibernate-3.2\hibernate-3.2.5.ga\hibernate-3.2\lib\cglib-2.1.3.jar;
E:\and\ant\bin\antlr-2.7.5H3.jar
  start.Start


6>客户端
 得到存根Hello_Stub.class-> E:\rmi_test\src>rmic rmic.Hello
 换个地方:c:\\TT 并copy rmic中的IHello.class和Hello_Stub.class连同目录rmic一同copy
package client;

import java.rmi.Naming;
import rmic.IHello;

public final class Client {
    
public static void main(String[] args) {
        System.out.println(
"client.");
        
try {
            IHello hello 
= (IHello)Naming.lookup("rmi://localhost:1099/RMI/Mclaren");
            System.out.println(hello.sayHello(
"1"));
        } 
catch(Exception e) {
            e.printStackTrace();
        }
    }

}

 dos编译-> C:\TT>javac -d . Client.java
 C:\TT>java client.Client
 client....
 Hello gg This is processed by RMI (gg就是数据 成功! )

mysql> select * from liu;
+----+------+-----+
| id | name | avg |
+----+------+-----+
|  1 | gg   |  24 |
+----+------+-----+








posted @ 2007-12-24 13:36 G_G 阅读(753) | 评论 (4)编辑 收藏