久しぶりの勉強
まずは復習。
だからTableから作る。S2Daoの復習ってことで。
-- -- -- DROP TABLE STORE; CREATE TABLE STORE ( ID NUMERIC(8) NOT NULL, NAME VARCHAR(30), ADDRESS VARCHAR(30), CREATE_DATE DATE, UPDATE_DATE DATE, CONSTRAINT PK_STORE PRIMARY KEY (ID) ); -- DROP SEQUENCE SEQ_STORE; CREATE SEQUENCE SEQ_STORE START WITH 1000; -- -- --
DAOを作るために、まずはテストクラスを作成。慣れてきた。
abstract public class BaseDaoTestCase extends S2DaoTestCase { public BaseDaoTestCase() { super(""); } public void setUp() { include("examples/dicon/alldao.dicon"); } }
public class StoreDaoTest extends BaseDaoTestCase { private StoreDao dao; private Store entity; public void setUp() { super.setUp(); entity = new Store(new Integer(2)); } public void setUpAfterContainerInit() { deleteTable(Store.TABLE); readXlsWriteDb("StoreTestData.xls"); } public void testFindBeanTx() { Store found = dao.findBean(entity); assertNotNull(found); assertEquals(entity, found); } public void testFindAllTx() { List found = dao.findAll(); assertNotNull(found); assertEquals(3, found.size()); } public void testInsertTx() { Store ins = new Store(); ins.setName("INSテスト"); ins.setAddress("INS県"); dao.insert(ins); Store found = dao.findBean(ins); assertNotNull(found); assertEquals(ins.toString(), found.toString()); } public void testUpdateTx() { Store up = dao.findBean(entity); up.setName("UPテスト"); up.setAddress("UP県"); dao.update(up); Store found = dao.findBean(entity); assertNotNull(found); assertEquals(up.toString(), found.toString()); } public void testDeleteTx() { Store del = dao.findBean(entity); dao.delete(del); Store found = dao.findBean(entity); assertNull(found); } }
本当は少しずつ作ってるけど。。。とりあえず、DAOとDto(Entity)クラス
public interface StoreDao { public Class BEAN = Store.class; public Store findBean(Store entity); public List findAll(); public void insert(Store ins); public void update(Store up); public void delete(Store del); }
public class Store implements Serializable { public static final String TABLE = "STORE"; public static final String id_ID = "sequence, sequenceName=SEQ_STORE"; private Integer id; private String name = ""; private String address = ""; // ----------------------------------------------------------------------- public Store() { } public Store(Integer id) { this.id = id; } // ----------------------------------------------------------------------- public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } // ----------------------------------------------------------------------- public boolean equals(Object o) { if (!(o instanceof Store)) { return false; } Store other = (Store) o; return id.equals(other.getId()); } public int hashCode() { return id.intValue(); } public String toString() { StringBuffer buf = new StringBuffer("["); setupToString(buf); buf.append("]"); return buf.toString(); } protected void setupToString(StringBuffer buf) { buf.append(id).append(", "); buf.append(name).append(", "); buf.append(address); } }
alldao.diconファイルの記述は、最近風(?)にしてみる。
<?xml version="1.0" encoding="Shift_JIS"?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components> <include path="dao.dicon"/> <include path="aop.dicon"/> <component class="org.seasar.framework.container.auto.FileSystemComponentAutoRegister"> <initMethod name="addClassPattern"> <arg>"examples.store.dao"</arg> <arg>".*Dao"</arg> </initMethod> <initMethod name="registAll"></initMethod> </component> <component class="org.seasar.framework.container.auto.InterceptorAutoRegister"> <property name="interceptorName">"aop.traceInterceptor"</property> <initMethod name="addClassPattern"> <arg>"examples.store.dao"</arg> <arg>".*Dao"</arg> </initMethod> <initMethod name="registAll"></initMethod> </component> <component class="org.seasar.framework.container.auto.InterceptorAutoRegister"> <property name="interceptorName">"dao.interceptor"</property> <initMethod name="addClassPattern"> <arg>"examples.store.dao"</arg> <arg>".*Dao"</arg> </initMethod> <initMethod name="registAll"></initMethod> </component> </components>
これでテストはもちろんグリーンバー。
途中何回かレッドバーになってるけどカット。
DAOが増えてきたらありがたみがわかりそう。
DAOのパッケージを移動したときにalldao.diconファイルがエラーにならなかった。当たり前だけど。。。あわせて修正することを忘れないようにしないと。