在sun主页上有java文档注释的编写格式

How to Write Doc Comments for the Javadoc Tool

http://java.sun.com/j2se/javadoc/writingdoccomments/

不过是英文的

@author LEI

@version 1.10 2005-09-01
1 注释文档的格式

注释文档将用来生成HTML格式的代码报告,所以注释文档必须书写在类、域、构造函数、方法、定义之前。注释文档由两部分组成——描述、块标记。

例如:

/**

* The doGet method of the servlet.

* This method is called when a form has its tag value method equals to get.

*

* @param request

* the request send by the client to the server

* @param response

* the response send by the server to the client

* @throws ServletException

* if an error occurred

* @throws IOException

* if an error occurred

*/

public void doGet (HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

前两行为描述,描述完毕后,由@符号起头为块标记注视。
2 注释的种类
2.1 文件头注释

文件头注释以 /*开始,以*/结束,需要注明该文件创建时间,文件名,命名空间信息。

例如:

/*

* Created on 2005-7-2

* /
2.2 类、接口注释

类、接口的注释采用 /** … */,描述部分用来书写该类的作用或者相关信息,块标记部分必须注明作者和版本。

例如:

/**Title: XXXX DRIVER 3.0
*Description: XXXX DRIVER 3.0
*Copyright: Copyright (c) 2003
*Company:XXXX有限公司
*
* @author Java Development Group
* @version 3.0
*/

例如:

/**
* A class representing a window on the screen.
* For example:
*
* Window win = new Window(parent);
* win.show();
*
*
* @author Sami Shaio
* @version %I%, %G%
* @see java.awt.BaseWindow
* @see java.awt.Button
*/

class Window extends BaseWindow {

...

}
2.3 构造函数注释

构造函数注释采用 /** … */,描述部分注明构造函数的作用,不一定有块标记部分。

例如:

/**

* 默认构造函数

*/

有例如:

/**

* 带参数构造函数,初始化模式名,名称和数据源类型

*

* @param schema

* Ref 模式名

* @param name

* Ref 名称

* @param type

* byVal 数据源类型

*/
2.4 域注释

域注释可以出现在注释文档里面,也可以不出现在注释文档里面。用/** … */的域注释将会被认为是注释文档热出现在最终生成的HTML报告里面,而使用/* … */的注释会被忽略。

例如:

/* 由于triger和表用一个DMSource,所以要区分和表的迁移成功标记 */

boolean isTrigerSuccess = false;

又例如:

/** 由于triger和表用一个DMSource,所以要区分和表的迁移成功标记 */

boolean isTrigerSuccess = false;

再例如:

/**

* The X-coordinate of the component.

*

* @see #getLocation()

*/

int x = 1263732;
2.5 方法注释

方法注释采用 /** … */,描述部分注明方法的功能,块标记部分注明方法的参数,返回值,异常等信息。例如:

/**

* 设置是否有外码约束

*

* @param conn

* Connection 与数据库的连接

*/
2.6 定义注释

规则同域注释。
3 注释块标记
3.1 标记的顺序

块标记将采用如下顺序:



*

* @param (classes, interfaces, methods and constructors only)

* @return (methods only)

* @exception (@throws is a synonym added in Javadoc 1.2)

* @author (classes and interfaces only, required)

* @version (classes and interfaces only, required. See footnote 1)

* @see

* @since

* @serial (or @serialField or @serialData)

* @deprecated (see How and When To Deprecate APIs)

* …

一个块标记可以根据需要重复出现多次,多次出现的标记按照如下顺序:

@author 按照时间先后顺序(chronological)

@param 按照参数定义顺序(declaration)

@throws 按照异常名字的字母顺序(alphabetically)

@see 按照如下顺序:

@see #field

@see #Constructor(Type, Type...)

@see #Constructor(Type id, Type id...)

@see #method(Type, Type,...)

@see #method(Type id, Type, id...)

@see Class

@see Class#field

@see Class#Constructor(Type, Type...)

@see Class#Constructor(Type id, Type id)

@see Class#method(Type, Type,...)

@see Class#method(Type id, Type id,...)

@see package.Class

@see package.Class#field

@see package.Class#Constructor(Type, Type...)

@see package.Class#Constructor(Type id, Type id)

@see package.Class#method(Type, Type,...)

@see package.Class#method(Type id, Type, id)

@see package
3.2 标记介绍
3.2.1 @param标记

@param后面空格后跟着参数的变量名字(不是类型),空格后跟着对该参数的描述。

在描述中第一个名字为该变量的数据类型,表示数据类型的名次前面可以有一个冠词如:a,an,the。如果是int类型的参数则不需要注明数据类型。例如:



* @param ch the char 用用来……

* @param _image the image 用来……

* @param _num 一个数字……



对于参数的描述如果只是一短语,最好不要首字母大写,结尾也不要句号。

对于参数的描述是一个句子,最好不要首字母大写,如果出现了句号这说明你的描述不止一句话。如果非要首字母大写的话,必须用句号来结束句子。(英文的句号)

公司内部添加ByRef和ByVal两个标记,例如:

* @param _image the image ByRef 用来……

说明该参数是引用传递(指针),ByVal可以省略,表示是值传递。
3.2.2 @return标记

返回为空(void)的构造函数或者函数,@return可以省略。

如果返回值就是输入参数,必须用与输入参数的@param相同的描述信息。

必要的时候注明特殊条件写的返回值。
3.2.3 @throws 标记

@throws以前使用的是@exception。

@throws的内容必须在函数的throws部分定义。
3.2.4 @author标记

类注释标记。

函数注释里面可以不出现@author。
3.2.5 @version

类注释标记。

函数注释里面可以不出现@version
3.2.6 @since

类注释标记。

标明该类可以运行的JDK版本

例如:

@since JDK1.2
3.2.7 @deprecated

由于某种原因而被宣布将要被废弃的方法。

/**

* @deprecated As of JDK 1.1, replaced by

* setBounds

* @see #setBounds(int,int,int,int)

*/
3.2.8 @link标记

语法:{@link package.class#member label}

Label为链接文字。

package.class#member将被自动转换成指向package.class的member文件的URL。
4 HTML代码的使用

在注释描述部分可以使用HTML代码。

表示段落

表示自动标号
5 注释示例

/**

* Graphics is the abstract base class for all graphics contexts

* which allow an application to draw onto components realized on

* various devices or onto off-screen images.

* A Graphics object encapsulates the state information needed

* for the various rendering operations that Java supports. This

* state information includes:

*

*

*

*

*

*

*

*

* (see setXORMode)

*

*

* Coordinates are infinitely thin and lie between the pixels of the

* output device.

* Operations which draw the outline of a figure operate by traversing

* along the infinitely thin path with a pixel-sized pen that hangs

* down and to the right of the anchor point on the path.

* Operations which fill a figure operate by filling the interior

* of the infinitely thin path.

* Operations which render horizontal text render the ascending

* portion of the characters entirely above the baseline coordinate.

*

* Some important points to consider are that drawing a figure that

* covers a given rectangle will occupy one extra row of pixels on

* the right and bottom edges compared to filling a figure that is

* bounded by that same rectangle.

* Also, drawing a horizontal line along the same y coordinate as

* the baseline of a line of text will draw the line entirely below

* the text except for any descenders.

* Both of these properties are due to the pen hanging down and to

* the right from the path that it traverses.

*

* All coordinates which appear as arguments to the methods of this

* Graphics object are considered relative to the translation origin

* of this Graphics object prior to the invocation of the method.

* All rendering operations modify only pixels which lie within the

* area bounded by both the current clip of the graphics context

* and the extents of the Component used to create the Graphics object.

*

* @author Sami Shaio

* @author Arthur van Hoff

* @version %I%, %G%

* @since 1.0

*/

public abstract class Graphics {

/**

* Draws as much of the specified image as is currently available

* with its northwest corner at the specified coordinate (x, y).

* This method will return immediately in all cases, even if the

* entire image has not yet been scaled, dithered and converted

* for the current output device.

*

* If the current output representation is not yet complete then

* the method will return false and the indicated

* {@link ImageObserver} object will be notified as the

* conversion process progresses.

*

* @param img the image to be drawn

* @param x the x-coordinate of the northwest corner

* of the destination rectangle in pixels

* @param y the y-coordinate of the northwest corner

* of the destination rectangle in pixels

* @param observer the image observer to be notified as more

* of the image is converted. May be

* null

* @return true if the image is completely

* loaded and was painted successfully;

* false otherwise.

* @see Image

* @see ImageObserver

* @since 1.0

*/

public abstract boolean drawImage(Image img, int x, int y,

ImageObserver observer);

/**

* Dispose of the system resources used by this graphics context.

* The Graphics context cannot be used after being disposed of.

* While the finalization process of the garbage collector will

* also dispose of the same system resources, due to the number

* of Graphics objects that can be created in short time frames

* it is preferable to manually free the associated resources

* using this method rather than to rely on a finalization

* process which may not happen for a long period of time.

*

* Graphics objects which are provided as arguments to the paint

* and update methods of Components are automatically disposed

* by the system when those methods return. Programmers should,

* for efficiency, call the dispose method when finished using

* a Graphics object only if it was created directly from a

* Component or another Graphics object.

*

* @see #create(int, int, int, int)

* @see #finalize()

* @see Component#getGraphics()

* @see Component#paint(Graphics)

* @see Component#update(Graphics)

* @since 1.0

*/

public abstract void dispose();

/**

* Disposes of this graphics context once it is no longer

* referenced.

*

* @see #dispose()

* @since 1.0

*/

public void finalize() {

dispose();

}

}
# The Component to draw on
# A translation origin for rendering and clipping coordinates
# The current clip
# The current color
# The current font
# The current logical pixel operation function (XOR or Paint)
# The current XOR alternation color

* ….

Hibernate 一对一外键单向关联
事实上,单向1-1N-1的实质是相同的,1-1N-1的特例,单向1-1N-1的映射配置也非常相似。只需要将原来的many-to-one元素增加unique="true"属性,用于表示N的一端也必须是唯一的,在N的一端增加了唯一的约束,即成为单向1-1。基于外键的单向1-1的配置将与无连接表N-1关联的many-to-one增加unique="true"属性即可。
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略gettersetter方法)
public class Person11fk {
private int personid;
private String name;
private int age;
private Address11fk address11fk;
public class Address11fk {
private int addressid;
private String addressdetail;
三、表模型
mysql> desc address_11fk;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc person_11fk;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| addressId | int(11) | YES | UNI | NULL | |
+-----------+--------------+------+-----+---------+----------------+
四、生成的SQL脚本
CREATE TABLE `address_11fk` (
`addressid` int(11) NOT NULL auto_increment,
`addressdetail` varchar(255) default NULL,
PRIMARY KEY (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
CREATE TABLE `person_11fk` (
`personid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`age` int(11) default NULL,
`addressId` int(11) default NULL,
PRIMARY KEY (`personid`),
KEY `FK68A8818F3F45AA77` (`addressId`),
CONSTRAINT `FK68A8818F3F45AA77` FOREIGN KEY (`addressId`) REFERENCES `address_11fk` (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
五、映射方法:
Person中添加Address属性,映射配置为:
六、测试方法
public class Test_11fk {
public static void main(String[] args){
Person11fk p1=new Person11fk();
p1.setAge(21);
p1.setName("p1");
Address11fk add1=new Address11fk();
add1.setAddressdetail("郑州市经三路");
p1.setAddress11fk(add1);
Session session= HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add1);
session.save(p1);
tx.commit();
HibernateUtil.closeSession();
}
}
七、测试结果
1) :正常保存. 推荐这么干!
session.save(add1);
session.save(p1);
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
2) :正常保存.
session.save(p1);
session.save(add1);
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: update PERSON_11fk set name=?, age=?, addressId=? where personid=?
3) :正常保存.
// session.save(p1);
session.save(add1);
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
4) : 发生异常,不能保存.
session.save(p1);
// session.save(add1);
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._1_1_fk.Address11fk

(Hibernate)cascade

在我前面的笔记中已经写过关联关系的使用,但主要是演示,在这篇中,我将再细分析!
利用关联关系操作对象:
数据对象之间的关联关系有一对一,一对多及多对多三种。在数据库操作中,数据对象之间的关联关系使用JDBC处理很困难。例如,当删除一个班级的信息时,还要删除该班级的所有学生的基本信息。如果直接使用JDBC执行这种级联操作,会非常繁锁。Hibernate通过把实体对象之间的关联关系及级联关系在映射文件中声明,比较简单地解决了这类级联操作问题。
一对一关联关系的使用
一对一关联关系在实际生活中是比较觉的,例如学生与学生证的关系,通过学生证可以找到学生。一对一关联关系在Hibernate中的实现有两种方式,分别是主键关联和外键关联。
主键关联
主键关联的重点是,关联两个实体共享一个主键值。例如student与card是一对一关系,它们在数据库中对应的表分别是t_student和t_card。它们共用一个主键值ID,这个主键可由t_student或t_card表生成。问题是如何让另一张表引用已经生成的主键值呢?例如,t_student表未老先衰了ID的值,t_card表如何引用它?这需要在Hibernate的映射文件中使用主键的foreign生成机制!

为了表示Student与Card之间的一对一的关联关系,我们需要在它们各自的映射文件 中都要使用标记!
一对一关系我在前面已经写过例子程序了,在这里仅给出两个映射文件。如下:
学生PO映射信息:

xml version="1.0" encoding="GBK"?>
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>

<hibernate-mapping>
<class name="hibernate.PO.TStudent" table="t_student" lazy="true">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="increment" />
id>
<property name="userName" type="java.lang.String">
<column name="userName" length="20" />
property>
<property name="cardId" type="java.lang.String">
<column name="card_id" length="20" />
property>
<property name="sex" type="java.lang.String">
<column name="sex" length="2" />
property>
<property name="age" type="java.lang.Integer">
<column name="age" />
property>
<one-to-one name="card" class="hibernate.PO.TCard" fetch="join" cascade="all" />
class>
hibernate-mapping>


学生证PO映射信息:

xml version="1.0" encoding="GBK"?>
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>

<hibernate-mapping>
<class name="hibernate.PO.TCard" table="t_card" lazy="true">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="foreign">
<param name="property">studentparam>
generator>
id>

<property name="name" type="java.lang.String">
<column name="name" length="20" />
property>


<one-to-one name="student" class="hibernate.PO.TStudent" constrained="true" />

class>
hibernate-mapping>

外键关联
外键关联的要点是:两个实体各自有不同的主键,但其中一个实体有一个外键引用另一个实体的主键。例如,假设,Student和Card是外键关联的一对一关系们在数据库中相应的表分别如下:t_student表有一个主键ID,t_card表有一个主键ID和一个外键student_id,此外键对应t_student表的主键ID,那么student的映射信息如上面一样不做改动,而Card的映射文件要做相应的改动。如下:
<hibernate-mapping>
<class name="hibernate.PO.TCard" table="t_card" lazy="true">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="increment"/>
id>

<property name="name" type="java.lang.String">
<column name="name" length="20" />
property>

<many-to-one name="student" column="student_id" class="hibernate.PO.TStudent" unique="true"/>



class>
hibernate-mapping>

一对多关联关系的使用
一对多关系很觉,例如班级与学生的关系就是典型的一对多的关系。在实际编写程序时,一对多关系有两种实现方式:单向关联和双向关联。单向的一对多关系只需要在一方进行映射配置,而双向的一对多需要在关联的双方进行映射配置。下面以Group(班级)和Student(学生)为例讲解如何配置一对多的关系。
单向关联
单向的一对多关系只需要在一方进行映射配置,所以我们只配置Group的映射文件:
<hibernate-mapping>
<class name="hibernate.PO.Group" table="t_group" lazy="true">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="increment"/>
id>

<property name="name" type="java.lang.String" update="true" insert="true">
<column name="name" length="20" />
property>


<set name="students"
table
="t_student"
lazy
="true"
inverse
="false"
cascade
="all"
sort
="unsorted">
<key column="ID"/>
<one-to-many class="hibernate.PO.TStudent"/>
set>
class>
hibernate-mapping>
双向关联
如果要设置一对多双向关联关系,那么还需要在“多”方的映射文件中使用标记。例如,在Group与Student一对多的双向关联中,除了Group的映射文件外还需要在Student的映射文件中加入如下代码:
<many-to-one name="group"
class
="Group"
cascade
="none"
outer-join
="auto"
update
="true"
insert
="true"
column
="ID" />

inert和update设定是否对column属性指定的关联字段进行insert和update操作。
此外将Group.hbm.xml中元素的inverse设置为true.

多对多关联关系的使用
Student(学生)和Course(课程)的关系就是多对多关系。在映射多对多关系时需要另外使用一个连接表(如Student_Course)。Student_Course表包含二个字段:courseID和studentID。此处它们的映射文件中使用标记,在Student的映射文件中加入以下描述信息:

<set name="courses"
table
="student_course"
lazy
="false"
inverse
="false"
cascade
="save-update">
<key column="studentID" />
<many-to-many class="Course" column="CourseID"/>
set>
相应的Course的映射文件中加入以下:
<set name="students"
table
="student_course"
lazy
="false"
inverse
="true"
cascade
="save-update">
<key column="CourseID" />
<many-to-many class="Student" column="StudentID"/>
set>
添加关联关系
首先编写一个程序来看看一个名为Bill的学生选择了什么课程:
//获取代表Bill的Student对象
Student stu = (Student)session.createQuery("from Student s where s.name='Bill'").uniqueResult();
List list
= new ArrayList(stu.getCourses());
for(int i = 0 ; i < list.size(); i++)
{
Course course
= (Course)list.get(i);//取得Course对象
System.out.println(course.getName());//打印出Bill所选课程的清单
}
现在Bill还想chemistry课程,这对于程序员来说只是为Bill添加一个到chemistry的关联,也就是说在student_course表中新增加一条记录,而T_student和T_Course表都不用变更。
//获取代表Bill的Student对象
Student stu = (Student)session.createQuery("from Student s where s.name='Bill'").uniqueResult();
Course course
= (Course)session.createQuery("from Course c where c.name='chemistry'").uniqueResult();
//设置stu与course的关联关系
stu.getCourses().add(course);
course.getStudents().add(stu);
删除关联关系
删除关联关系比较简单,直接调用对象集合的remove()方法删除不要的对象就可。例如:要从学生Bill的选课清单中删除politics和chemistry两门课,程序代码如下:
//获取代表Bill的Student对象
Student stu = (Student)session.createQuery("from Student s where s.name='Bill'").uniqueResult();
Course course1
= (Course)session.createQuery("from Course c where c.name='politics'").uniqueResult();
Course course2
= (Course)session.createQuery("from Course c where c.name='chemistry'").uniqueResult();
stu.getCourse().remove(course1);
//删除politics课程
stu.getCourse().remove(course2);//删除chemistry课程
运行以上程序将从student_course表中删除这两条记录,但T_student和T_course表没有任何变化

 
Copyright 2005-2007. Hello Wiki designed by Fen, Blogger Templates by Blogcrowds.