ラベル Seasar の投稿を表示しています。 すべての投稿を表示
ラベル Seasar の投稿を表示しています。 すべての投稿を表示

8/14/2008

S2Click: Seasar2 integration for Click Framework

S2Click is Seasar2 integration for Click Framework. Seasar2 is an opensource DI container made in Japan. S2Click adds powerful features to Click.
  • HOT deploy - You don't have to restart application server when you modify your sourcecode.
  • Java5 annotation - S2Click provides some annotations for ease of development
  • Ajax & File download support - The page base class S2ClickPage provides methods to render Ajax & file downloading response.
  • public field handling - You can use public fields instead of setter & getter in many places.
  • Extended controls - S2ClickForm, ToolTip, ConfirmSubmit, AjaxLinks and AjaxButtons
Now, Seasar2 and S2Click focuses to Japanese developers (So documentation is provided with Japanese mainly).

However I will introduce S2Click detailed features in this blog. And also I will feedback these features to Click Framework from S2Click in the feature.

2/21/2008

The First Seasar2 Example

At first, I show the very simple example to use S2Container. I created following 3 files into "jp.sf.amateras.seasar.example" package.

example.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN"
          "http://www.seasar.org/dtd/components24.dtd">
<components>
  <component class="java.util.ArrayList" name="list">
    <initMethod name="add"><arg>"Naoki Takezoe"</arg></initMethod>
  </component>
</components>
<components>
  <component class="jp.sf.amateras.seasar.example.Hello" name="hello">
    <property name="target">list</property>
  </component>
</components> 

Hello.java

package jp.sf.amateras.seasar.example;
import java.util.List;

public class Hello {
 private List target;
 public void setTarget(List target){
   this.target = target;
 }
 public void sayHello(){
   for(int i=0;i<target.size();i++){
     System.out.println("Hello, " + target.get(i));
   }
 }
}

Main.java

package jp.sf.amateras.seasar.example;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;

public class Main {
 public static void main(String[] args){
   S2Container container = S2ContainerFactory.create(
              "jp/sf/amateras/seasar/example/example.dicon");
   Hello hello = (Hello)container.getComponent("hello");
   hello.sayHello();
 }
}

Run the "Main" class as a Java application, it displays "Hello, Naoki Takezoe".

2/12/2008

S2Dao: 2-Way SQL

S2Dao can assemble dynamic query using SQL comment. For example:

@Sql("SELECT * FROM emp WHERE empno = /*empno*/7788")
@Arguments("empno")
public Emp selectEmpByEmpNo(int empNo);

/*empno*/ replaces the given argument and 7788 is removed by S2Dao at runtime. We can execute this SQL without no modification with the database client software such as psql. This solutuion is called "2-Way SQL" in S2Dao.

We can also use IF..ELSE statement as SQL comment.

/*IF hoge != null*/hoge = /*hoge*/'abc'/*END*/

And see the following example. If all conditions are false, S2Dao ignores /*BEGIN*/ ... /*END*/ block. So WHERE phrase is removed when it's not needed.

/*BEGIN*/WHERE
  /*IF job != null*/job = /*job*/'CLERK'/*END*/
  /*IF deptno != null*/AND deptno = /*deptno*/20/*END*/
/*END*/

Again, these SQL can be executed with database client software with no modifications. 2-Way SQL makes possible to test SQL easily.

2/11/2008

S2Dao: The most powerfull database access framework

S2Dao is a database access framework based on the Seasar2 and AOP solution. Today, I introduce this great framework to you.

S2Dao makes DAO from Java interface using AOP at runtime. We have to write only Java interface such as:

@S2Dao(bean=UserInfo.class)
public interface UserInfoDao {
  public List<UserInfo> selectAllUserInfos();
  public void insertUserInfo(UserInfo userInfo);
  public void updateUserInfo(UserInfo userInfo);
  public void deleteUserInfo(UserInfo userInfo);
}

SQLs are generated automatically by the entity definition (which is specified by @S2Dao) and DAO method names. So we don't have to write SQL in simple cases such as CRUD to the single table.

Of course, we can use complex SQL instead of auto generated SQL. We can specify WHERE and ORDER BY using @Query:

@Query("DEPTID=? ORDER BY EMPNO")
public List<UserInfo> selectUserInfoByDeptId(int deptId); 

And using @Sql, we can specify all parts of SQL:

@Sql("SELECT COUNT(*) FROM USERINFO")
public int getCount(); 

These are parts of S2Dao great features. If we have to use complex and large SQL, we can write it as an external SQL file named "classname_methodname.sql" and S2Dao also support dynamic query using SQL comment. I will talk about them as another entries.