参画しているプロジェクトで使用しているので、自分でも試してみることにした。
プロジェクトでは既にDBアクセス用仕組みがあり、ロジックを実装する側はあまり意識する
事なく使っているので正直理解しているとは言えないので。
http://thinkit.co.jp/free/article/0606/13/3/ を参考に挑戦してみた。
環境
Eclipse 3.5
JAVA 6
IBator
MySQL5.0.8
Dao Framework
iBatisの基本的な使い事ということで、DAO Frameworkもあわせて
試す。
そこでハマったこと。
実行すると、DAOの定義ファイルの読込みで、com.ibatis.dao.client.DaoException が発生。
よく見るとIBatorの実行で使用する xmlファイルの
daoGenerator タグの TYPE属性値が IBATIS でなく GENERIC-CI だった。
これはEclipseのプラグインの IBator が出力したデフォルト値だった。
テーブル名とDBの接続情報ぐらいしかチェックしていなかった。
IBatorのxmlファイル
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
<abatorContext >
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="xxxx" password="xxxx" >
<classPathEntry location="D:\Dev\JavaLib\mysql-connector-java-5.0.8-bin.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="dto" targetProject="ibatis" />
<sqlMapGenerator targetPackage="map" targetProject="ibatis" />
<daoGenerator type="IBATIS" targetPackage="dao" targetProject="ibatis" />
<table schema="test" tableName="SHAIN" ></table>
<table schema="test" tableName="BUSHO" ></table>
</abatorContext>
</abatorConfiguration>
実行用のクラス
public static void main(String[] args) throws Exception {
Reader reader;
reader = Resources.getResourceAsReader("dao.xml");
daoManager = DaoManagerBuilder.buildDaoManager(reader);
ShainDAO dao = (ShainDAO)daoManager.getDao(ShainDAO.class);
ShainExample example = new ShainExample();
example.createCriteria().andShainIdEqualTo(111);
List<Shain> shainList = dao.selectByExample(example);
Shain dto = shainList.get(0);
// 検索処理(JOIN)
System.out.println("-----検索(JOIN)結果-----");
System.out.println("shainId = " + dto.getShainId());
System.out.println("shainName = " + dto.getShainName());
System.out.println("bushoId = "
+ dto.getBushoId());
System.out.println("\n");
}