ドキュメントのQueryRunner#insertメソッドに従って Apache Commons DBUtils を使用しています。ResultSetHandlerのジェネリック型を返します。プロジェクトに BR_Author オブジェクトがあります。
BR_Author.java
import org.springframework.stereotype.Repository;
@Repository
public class BR_Author {
private int id;
private String authorName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
}
サービスクラスで次のような単純な挿入ステートメントを作成します
AuthorService#createAuthor
public BR_Author createAuthor(String authorName) throws ApiException {
String sql = "Insert into BR_AUTHOR(authorName) VALUES(?)";
ResultSetHandler<BR_Author> rs = new BeanHandler<BR_Author>(
BR_Author.class);
QueryRunner qr = new QueryRunner(dataSource);
Object[] params = { authorName };
try {
BR_Author author = qr.insert(sql, rs, params);
System.out.println("Br_Author:" + author);
return author;
} catch (SQLException e) {
throw new ApiException(ErrorCode.ERR20001, e.getMessage());
}
}
createAuthor メソッドで追加された値を返そうとしていますが、id フィールドを自動インクリメント オブジェクトとして設定すると、次のように返されます。
Br_Author:Br_Author [id=0, authorName=null]
データベースを確認すると、値が正常に追加されていることがわかります。
自動インクリメントを無効にしてコードから ID を設定すると、作成者オブジェクトが null になります。だから私は QueryRunner#insert メソッドを誤解しているのか、それともバグがあるのか を知りたいです。私はすでに以下のリンクをチェックしています。
ところで: BR_Author クラスに対して正常に動作するクエリを選択するため、マッピングの問題は発生しません。