ユニットテーブルのDAO
ということでちゃっちゃとやってしまえぃ!
まずはユニットテーブルに対するDAOをやりましょう。BEANエンティティクラスから。こんな感じで↓
public class UnitDataEntity { public static final String TABLE = "UNITDATA"; private int id; private String modelNo = null; private String name = null; private int belongToCode; private String type = null; private String groundAdaptation = null; private String airAdaptation = null; private String waterAdaptation = null; private String spaceAdaptation = null; ・・・(getter/setterは略)・・・ public boolean equals(Object obj) { UnitDataEntity target = (UnitDataEntity) obj; if (getId() != target.getId()) {return false;} if (!getModelNo().equalsIgnoreCase(target.getModelNo())) {return false;} if (!getName().equalsIgnoreCase(target.getName())) {return false;} if (getBelongToCode() != target.getBelongToCode()) {return false;} if (!getType().equalsIgnoreCase(target.getType())) {return false;} if (!getGroundAdaptation().equalsIgnoreCase(target.getGroundAdaptation())) {return false;} if (!getAirAdaptation().equalsIgnoreCase(target.getAirAdaptation())) {return false;} if (!getWaterAdaptation().equalsIgnoreCase(target.getWaterAdaptation())) {return false;} if (!getSpaceAdaptation().equalsIgnoreCase(target.getSpaceAdaptation())) {return false;} return true; } }
ほい、次はDAOインターフェース
public interface UnitDataDao { public Class BEAN = UnitDataEntity.class; public int insert(UnitDataEntity unitDataEntity); public int update(UnitDataEntity unitDataEntity); public int delete(UnitDataEntity unitDataEntity); public UnitDataEntity getUnitDataEntity(int id); public static final String getUnitDataEntity_ARGS = "id"; public List getAllEntity(); public static final String getAllEntity_QUERY = "order by id"; }
dao.diconにDAOインターフェースを登録して
<component name="UnitData" class="redrisefirm.seasar.s2dao.dao.UnitDataDao"> <aspect>interceptor</aspect> </component>
テスト用のデータを2、3件登録する
INSERT INTO UNITDATA VALUES(1,'RX-78-2','ガンダム',1,'0','1','0','1','1'); INSERT INTO UNITDATA VALUES(2,'MS-06F','ザクⅡF型',2,'0','0','0','0','1'); INSERT INTO UNITDATA VALUES(3,'MS-06S','シャア専用ザクⅡ',2,'0','1','0','0','1'); INSERT INTO UNITDATA VALUES(4,'RX-77','ガンキャノン',1,'0','1','0','1','1') INSERT INTO UNITDATA VALUES(5,'MA-08','ピグ・ザム',2,'1','0','0','0','1') SELECT * FROM UNITDATA;
そしてテストクラスも書きまくる
public class UnitDataDaoTest extends S2TestCase { private UnitDataDao unitDataDao = null; protected void setUp() throws Exception { include("redrisefirm/seasar/s2dao/dao.dicon"); unitDataDao = (UnitDataDao) getComponent(UnitDataDao.class); } protected void setUpAfterContainerInit() throws Throwable { readXlsReplaceDb("redrisefirm/seasar/s2dao/util/getUnitDataResult.xls"); } public final void testGetUnitDataEntityOneTx() {・・・(中略)・・・} public final void testGetUnitDataEntityThreeTx() {・・・(中略)・・・} public final void testGetUnitDataEntityNoNumberTx() {・・・(中略)・・・} public final void testInsertTx() {・・・(中略)・・・} public final void testInsertDoubleTx() {・・・(中略)・・・} public final void testUpdateTx() {・・・(中略)・・・} public final void testUpdateEffectRangeTx() {・・・(中略)・・・} public final void testUpdateOutNumberTx() {・・・(中略)・・・} public final void testDeleteTx() {・・・(中略)・・・} public final void testGetAllEntityTx() {・・・(中略)・・・} }
だいぶ端折ったが、内容はBelongToDaoの時と同じである。
テストを実行して全緑であることを確認する。