QueryRunner 插入方法 Return 类型

QueryRunner Insert method Return Type

我正在根据其文档中的 QueryRunner#insert 方法使用 Apache Commons DBUtils,插入 return 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;
    }
}

我写了一个简单的插入语句 class 就像

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());
        }
    }

我正在尝试 return createAuthor 方法中的附加值,但是如果我将 id 字段配置为自动递增对象 return as

Br_Author:Br_Author [id=0, authorName=null]

当我检查数据库时,我看到它成功添加了值。

如果我禁用自动递增并从代码中设置 id,则作者对象为空。所以我想了解是我误解了 QueryRunner#insert 方法还是它有错误。我已经查看了以下链接。

顺便说一句: Select 查询在 BR_Author class 上工作正常所以这意味着那里应该不是任何映射问题。

我没有从DBUtils API找到任何官方解决方案,所以我只是return生成ID,然后在Service层插入带有这个id的记录后查询它。这种方法return服务层的对象,但我做了一个额外的查询。