ホーム > タグ > S2JDBC
S2JDBC
2way SQL の挙動がおかしい(気がする)@S2JDBC
- 2009-05-04 (月)
- Java
|
S2JDBC の 2way SQL の挙動が気になったのでメモ。
というか 2way SQL のパラメータが Map のときの挙動。
チュートリアルのテストケースを利用して検証してみました。
2.4.35 を使用します。
まずサンプルの 2way SQL をコピーして変更。
■ META-INF/sql/examples/entity/Employee/selectWithDepartment2.sql
- select e.*, d.name as department_name
- from employee e left outer join department d on e.department_id = d.id
- /*BEGIN*/
- where
- /*IF salaryMin != null*/
- e.salary >= /*salaryMin*/1000
- /*END*/
- /*IF salaryMax != null*/
- and e.salary <= /*salaryMax*/2000
- /*END*/
- – 追加ここから
- /*IF departmentId != null*/
- and e.department_id = /*departmentId*/1
- /*END*/
- – 追加ここまで
- /*END*/
- order by e.salary
■ テストケースその1
パラメータをすべて指定してみる。
- public void testSqlFile() throws Exception {
- // DTO
- SelectWithDepartmentDto dto = new SelectWithDepartmentDto();
- dto.salaryMin = 1200;
- dto.salaryMax = 1800;
- dto.departmentId = 3;
- List<EmployeeDto> results =
- jdbcManager
- .selectBySqlFile(EmployeeDto.class, SQL_FILE, dto)
- .getResultList();
- System.out.println("—DTO—————————");
- for (EmployeeDto e : results) {
- System.out
- .println(e.name + " " + e.salary + " " + e.departmentName);
- }
- System.out.println("————————-DTO—–");
- // MAP
- Map map = new HashMap();
- map.put("salaryMin", 1200);
- map.put("salaryMax", 1800);
- map.put("departmentId", 3);
- List<EmployeeDto> results2 =
- jdbcManager
- .selectBySqlFile(EmployeeDto.class, SQL_FILE, map)
- .getResultList();
- System.out.println("—MAP—————————");
- for (EmployeeDto e : results2) {
- System.out
- .println(e.name + " " + e.salary + " " + e.departmentName);
- }
- System.out.println("————————MAP——");
- }
同じ SQL が発行されます。当然結果も同じです。
■ テストケースその2
パラメータを2つにしてみた。
- public void testSqlFile2() throws Exception {
- // DTO
- SelectWithDepartmentDto dto = new SelectWithDepartmentDto();
- dto.salaryMin = 1200;
- dto.salaryMax = 1800;
- List<EmployeeDto> results =
- jdbcManager
- .selectBySqlFile(EmployeeDto.class, SQL_FILE, dto)
- .getResultList();
- System.out.println("—DTO—————————");
- for (EmployeeDto e : results) {
- System.out
- .println(e.name + " " + e.salary + " " + e.departmentName);
- }
- System.out.println("————————-DTO—–");
- // MAP
- Map map = new HashMap();
- map.put("salaryMin", 1200);
- map.put("salaryMax", 1800);
- List<EmployeeDto> results2 =
- jdbcManager
- .selectBySqlFile(EmployeeDto.class, SQL_FILE, map)
- .getResultList();
- System.out.println("—MAP—————————");
- for (EmployeeDto e : results2) {
- System.out
- .println(e.name + " " + e.salary + " " + e.departmentName);
- }
- System.out.println("————————MAP——");
- }
同じ SQL が発行され、結果も同じなのですが、Map の方では WARN が出力される。
WARN 2009-05-04 06:52:33,250 [main] 引数(departmentId)が見つかりません
以下のように Map に key を設定すれば回避できますが…(ーー;)
- map.put("departmentId", null);
■ テストケースその3
パラメータを1つにしてみた。
- public void testSqlFile3() throws Exception {
- // DTO
- SelectWithDepartmentDto dto = new SelectWithDepartmentDto();
- dto.salaryMin = 1200;
- List<EmployeeDto> results =
- jdbcManager
- .selectBySqlFile(EmployeeDto.class, SQL_FILE, dto)
- .getResultList();
- System.out.println("—DTO—————————");
- for (EmployeeDto e : results) {
- System.out
- .println(e.name + " " + e.salary + " " + e.departmentName);
- }
- System.out.println("————————-DTO—–");
- // MAP
- Map map = new HashMap();
- map.put("salaryMin", 1200);
- List<EmployeeDto> results2 =
- jdbcManager
- .selectBySqlFile(EmployeeDto.class, SQL_FILE, map)
- .getResultList();
- System.out.println("—MAP—————————");
- for (EmployeeDto e : results2) {
- System.out
- .println(e.name + " " + e.salary + " " + e.departmentName);
- }
- System.out.println("————————MAP——");
- }
Map のとき、発行される SQL が違が …(*゚ロ゚*)
- select e.*, d.name as department_name
- from employee e left outer join department d on e.department_id = d.id
- where
- e.salary >= 1200
- and e.salary <= 1200
- and e.department_id = 1200
- order by e.salary
Map のサイズが1のときだけ…?
中途半端ですがコードを追いきれなかったので今日はココまでです!
- Comments: 0
- Trackbacks: 0
Home > Tags > S2JDBC


