「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:
  1. <component name="NewTableListDAO" class="jp.co.ezic.system.dao.NewTableListDAO">
  2.     <aspect>dao_system.interceptor</aspect>
  3. </component>
  4. <component name="NewTableListBO" class="jp.co.ezic.system.bo.NewTableListBO">
  5.     <property name="newTableListDao">NewTableListDAO</property>
  6. </component>

★NewTableListBO.java [新規]

JAVA:
  1. package jp.co.ezic.system.bo;
  2.  
  3. //import java.util.Date;
  4. //import java.util.List;
  5.  
  6. import jp.co.ezic.system.dao.NewTableListDAO;
  7.  
  8. public class NewTableListBO {
  9.    
  10.     private NewTableListDAO newTableListDAO;
  11.    
  12.     public NewTableListBO() {
  13.         super();
  14.         S2Container container = SingletonS2ContainerFactory.getContainer();
  15.         this.setNewTableListDao((NewTableListDAO)container.getComponent(NewTableListDAO.class));
  16.     }
  17.    
  18.     public NewTableListDAO getNewTableListDao() {
  19.         return newTableListDAO;
  20.     }
  21.    
  22.     public void setNewTableListDao(NewTableListDAO newTableListDAO) {
  23.         this.newTableListDAO = newTableListDAO;
  24.     }
  25.    
  26. }

★ NewTableListDAO.java [新規]

JAVA:
  1. package jp.co.ezic.system.dao;
  2.  
  3. //import java.util.ArrayList;
  4. //import java.util.List;
  5.  
  6. import jp.co.ezic.system.entity.NewTableListBean;
  7.  
  8. public interface NewTableListDAO {
  9.    
  10.     public Class BEAN=NewTableListBean.class;
  11.    
  12.     public static final String getRecordById_QUERY ="public.new_table_list.id=/*Id*/'1'";
  13.    
  14.     public NewTableListBean getRecordById(int Id);
  15.    
  16. }

★ NewTableListBean.java [新規]

JAVA:
  1. package jp.co.ezic.system.entity;
  2.  
  3. //import java.util.Date;
  4.  
  5. public class NewTableListBean {
  6.     public static final String TABLE = "public.new_table_list";
  7.    
  8.     //----------------
  9.     // 変数設定
  10.     //----------------
  11.     private int id;
  12.     private String item;
  13.  
  14.     //----------------
  15.     // getter/setter
  16.     //----------------
  17.     public int getId() {
  18.         return id;
  19.     }
  20.     public void setId(int id) {
  21.         this.id = id;
  22.     }
  23.  
  24.     public String getItem() {
  25.         return item;
  26.     }
  27.     public void setItem(String item) {
  28.         this.item = item;
  29.     }
  30. }

★ 実装先.java [追記]

JAVA:
  1. import jp.co.ezic.system.dao.NewTableListDAO;
  2. import jp.co.ezic.system.entity.NewTableListBean;
  3.  
  4. public class 実装先 {
  5.     //BO
  6.     private NewTableListBO newTableListBO;
  7.  
  8.     public 実装先() {
  9.         super();
  10.         S2Container container = SingletonS2ContainerFactory.getContainer();
  11.         this.setNewTableListBO((NewTableListBO)container.getComponent(NewTableListBO.class));
  12.     }
  13.  
  14.     // getter/setter
  15.     public NewTableListBO getNewTableListBO() {
  16.         return newTableListBO;
  17.     }
  18.     public void setNewTableListBO(NewTableListBO newTableListBO) {
  19.         this.newTableListBO = newTableListBO;
  20.     }
  21.    
  22.     // 「public.new_table_list」からリスト取得
  23.     NewTableListBO ntlbo = this.getNewTableListBO();
  24.     NewTableListDAO ntldao = ntlbo.getNewTableListDao();
  25.     int whereId = 1; // 抽出条件
  26.     NewTableListBean ntlbean = ntldao.getRecordById(whereId);
  27.     String itemValue = "";
  28.    
  29.     // レコード取得
  30.     if(ntlbean != null){
  31.         if(ntlbean.getItem() != null){
  32.             // 抽出条件がid=1なので、itemValueは「orange」になる
  33.             itemValue = ntlbean.getItem();
  34.         }
  35.     }
  36. }

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