S2JDBC

JdbcManager

★外部SQLファイルの用法

SQLファイルはsrc/main/resources配下にある

selectBySqlFile(Class<T> baseClass, String Path)

selectBySqlFile(Class<T> baseClass, String Path, Object parameter)

getCountBySqlFile(String Path)

getCountBySqlFile(String Path, Object parameter)

updateBySqlFile(String Path)

updateBySqlFile(String Path, Object parameter)

updateBatchBySqlFile(String Path, List<T> params)

updateBatchBySqlFile(String Path, T... paramClasses)

SQLファイル

SELECT id, name

FROM table

WHERE

salary >= /*min*/

AND salary <= /*max*/

クラス群

public class SearchParam {

public Integer min;

public Integer max;

}

public class SearchDto {

public Long id;

public String name;

}

public class SearchAction {

public List<SearchDto> dtoList;

@Resource

protected SearchService searchService;

@Execute(validator=false)

public String index() {

SearchParam param = new SearchParam();

param.min = 1000;

param.max = 2000;

this.dtoList = searchService.getSearchList(param);

return "index.jsp";

}

}

public class SearchService {

@Resource

protected JdbcManager jdbcManager;

public List<SearchDto> getSearchList(SearchParam param) {

return jdbcManager

.selectBySqlFile(SearchDto.class, "sample/search.sql", param)

.getResultList();

}

public long getSearchCount(SearchParam param) {

return jdbcManager.getCountBySqlFile("sample/count.sql", param);

}

}

★排他制御

SELECT...FOR UPDATE は必ずトランザクション中に実行すること

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

public void insert() {

// 排他制御処理

...

}

2WaySQL

★パラメータの埋め込み

クライアントで実行も可能

/*パラメータ名*/ プレースホルダ

WHERE

salary >= /*min*/1000

AND salary <= /*max*/2000

WHERE

id IN /*id*/(1, 2)

WHERE

name LIKE /*name*/'Andy%'

public class MyDto implements Serializable {

public List<String> codes;

}

SELECT t.no, t.name

FROM table t

WHERE t.no IN /*codes*/()

/*$パラメータ名*/ 文字列置換(SQLインジェクション注意)

ORDER BY /*$orderBy*/emp_no

★条件分岐

/*BEGIN*/

WHERE

/*IF min != null*/

salary >= /*min*/

/*END*/

/*IF max != null*/

AND salary <= /*max*/

-- ELSE salary IS NULL

/*END*/

/*END*/