「Seasar」でDBに追加されたテーブルを操作する書き方メモ


★環境
 /WebContent/WEB-INF/lib/s2-dao-1.0.39.jar
 /WebContent/WEB-INF/lib/s2-dao-1.0.39-sources.jar
 /WebContent/WEB-INF/lib/s2-extension-2.3.15.jar
 /WebContent/WEB-INF/lib/s2-extension-2.3.15-sources.jar
 /WebContent/WEB-INF/lib/s2-framework-2.3.15.jar
 /WebContent/WEB-INF/lib/s2-framework-2.3.15-sources.jar

★追加テーブル

 ■テーブル名
  new_table_list

 ■カラム
  id:integer
  item:varchar

 ■テーブル

------------------------------
id        item
------------------------------
1         orange
2         apple
------------------------------

★app.dicon [追記]

[XML]

dao_system.interceptor

NewTableListDAO
[/XML]

★NewTableListBO.java [新規]

[JAVA]
package jp.co.ezic.system.bo;

//import java.util.Date;
//import java.util.List;

import jp.co.ezic.system.dao.NewTableListDAO;

public class NewTableListBO {

private NewTableListDAO newTableListDAO;

public NewTableListBO() {
super();
S2Container container = SingletonS2ContainerFactory.getContainer();
this.setNewTableListDao((NewTableListDAO)container.getComponent(NewTableListDAO.class));
}

public NewTableListDAO getNewTableListDao() {
return newTableListDAO;
}

public void setNewTableListDao(NewTableListDAO newTableListDAO) {
this.newTableListDAO = newTableListDAO;
}

}
[/JAVA]

★ NewTableListDAO.java [新規]

[JAVA]
package jp.co.ezic.system.dao;

//import java.util.ArrayList;
//import java.util.List;

import jp.co.ezic.system.entity.NewTableListBean;

public interface NewTableListDAO {

public Class BEAN=NewTableListBean.class;

public static final String getRecordById_QUERY =”public.new_table_list.id=/*Id*/’1′”;

public NewTableListBean getRecordById(int Id);

}
[/JAVA]

★ NewTableListBean.java [新規]

[JAVA]
package jp.co.ezic.system.entity;

//import java.util.Date;

public class NewTableListBean {
public static final String TABLE = “public.new_table_list”;

//—————-
// 変数設定
//—————-
private int id;
private String item;

//—————-
// getter/setter
//—————-
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
}
[/JAVA]

★ 実装先.java [追記]

[JAVA]

import jp.co.ezic.system.dao.NewTableListDAO;
import jp.co.ezic.system.entity.NewTableListBean;

public class 実装先 {
//BO
private NewTableListBO newTableListBO;

public 実装先() {
super();
S2Container container = SingletonS2ContainerFactory.getContainer();
this.setNewTableListBO((NewTableListBO)container.getComponent(NewTableListBO.class));
}

// getter/setter
public NewTableListBO getNewTableListBO() {
return newTableListBO;
}
public void setNewTableListBO(NewTableListBO newTableListBO) {
this.newTableListBO = newTableListBO;
}

// 「public.new_table_list」からリスト取得
NewTableListBO ntlbo = this.getNewTableListBO();
NewTableListDAO ntldao = ntlbo.getNewTableListDao();
int whereId = 1; // 抽出条件
NewTableListBean ntlbean = ntldao.getRecordById(whereId);
String itemValue = “”;

// レコード取得
if(ntlbean != null){
if(ntlbean.getItem() != null){
// 抽出条件がid=1なので、itemValueは「orange」になる
itemValue = ntlbean.getItem();
}
}
}
[/JAVA]

スポンサードリンク

コメント

タイトルとURLをコピーしました