【发布时间】:2015-05-10 20:07:09
【问题描述】:
我正在使用 Apache Commons DBUtils 根据其文档中的QueryRunner#insert 方法,插入返回 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,则作者对象为空。所以我想知道我误解了 QueryRunner#insert 方法还是它有一个错误。我已经检查了下面的链接。
顺便说一句:选择对 BR_Author 类工作正常的查询,因此这意味着不应该有任何映射问题。
【问题讨论】:
标签: java sql spring apache-commons-dbutils