使用 spring 从数据库构造一个对象,指定要从中构造对象的行 ID

发布于 2024-10-26 12:04:07 字数 1380 浏览 2 评论 0 原文

为了简化这个问题,是否有可能使 spring 以非常基本的活动记录模式运行

背景:

我有一个具有多种类型的现有数据模型的系统,所有 UID 都存储在只读表中。当用户将数据输入系统时,使用 UID 将输入与现有行进行比较,以将输入数据与现有数据进行匹配。

问题:

我想使用 spring 从现有数据构造对象进行比较,这将允许我轻松添加和配置数据模型。我不想使用 hibernate 或任何其他复杂的 ORM,因为我只需要读取访问权限,并且希望使系统尽可能简单和轻量,所以我正在考虑一个简单的 spring jdbc 模板。

我应该如何传递 importId 来构造对象?我可以创建一个 setter 方法,但随后我必须调用构造函数并放置一个空对象,直到我设置 id,此时我开始初始化其他变量,这看起来很尴尬。有没有标准的方法来做到这一点?我怀疑我可能遗漏了一些明显的东西。这样做还有其他建议吗?

如果不陷入代码困境,从现有数据构造的对象类的示例可能是:

public class Import extends Model {
    private JdbcTemplate mJdbcTemplate;
    private int mImportId;
    public Import(int importId, JdbcTemplate template) {
        this.mImportId = importId;
        this.mJdbcTemplate = jdbcTemplate;
        ...
        // construct object form fields
        ... 
    }
    ... 
    // Other stuff
    ... 
} 
<beans>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean> 
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    ...
    data source setup
    ...
    </bean>
    <bean id="import" class="package.models.Import">
        <constructor-arg ref="jdbcTemplate"/>
        ... what to do here...
    </bean>
</beans>

To Simplify this question, is it possible to make spring behave in a very basic active record pattern

Background:

I have a system with multiple types of existing data models all with UIDs stored in read only tables. As a user inputs data into the system the input is compared against the existing row using the UID to match the input data with the existing data.

Problem:

I want to use spring to construct objects from the existing data for comparison, this will allow me to add and configure data models with ease. I don't want to use hibernate or any other complex ORM becuase I only need read access and want to keep the system as simple and light weight as possible so I am considering a simple spring jdbc template.

How should I pass the importId to construct the object? I could make a setter method but then I will have to call the constructor and have a empty object lying round till I set the id at which point I begin initialising other variables, this seems awkward. Is there a standard way to do this? I suspect I might be missing something obvious. Are there any other suggestions for doing this?

Without getting bogged down with the code an example of an object class constructed from the existing data might be:

public class Import extends Model {
    private JdbcTemplate mJdbcTemplate;
    private int mImportId;
    public Import(int importId, JdbcTemplate template) {
        this.mImportId = importId;
        this.mJdbcTemplate = jdbcTemplate;
        ...
        // construct object form fields
        ... 
    }
    ... 
    // Other stuff
    ... 
} 
<beans>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean> 
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    ...
    data source setup
    ...
    </bean>
    <bean id="import" class="package.models.Import">
        <constructor-arg ref="jdbcTemplate"/>
        ... what to do here...
    </bean>
</beans>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2024-11-02 12:04:08

您需要为您的对象实现一个 RowMapper,然后您可以使用 jdbcTemplate.queryForObject 方法。

public Import getImportById(long id) {
       return jdbcTemplate.queryForObject(
          "Select a, b FROM import WHERE id = ?",
          this.importRowMapper,
          id);
}


private RowMapper<Import> importRowMapper = new RowMapper<Import>() {    
    public Customer mapRow(ResultSet resultSet, int i) throws SQLException {
      long id = resultSet.getInt("id");
      String a = resultSet.getString("a");
      String b = resultSet.getString("b");
      return new Customer(id, a, b);
    }

您可以看看这个博客: Green Beans: Getting Started with Spring in your Service Tier,它对此进行了一些描述。 (本博客中只有最后一部分是关于 JPA 的。

What you need ti implement is a RowMapper for you Object, then you can yuse the jdbcTemplate.queryForObject method.

public Import getImportById(long id) {
       return jdbcTemplate.queryForObject(
          "Select a, b FROM import WHERE id = ?",
          this.importRowMapper,
          id);
}


private RowMapper<Import> importRowMapper = new RowMapper<Import>() {    
    public Customer mapRow(ResultSet resultSet, int i) throws SQLException {
      long id = resultSet.getInt("id");
      String a = resultSet.getString("a");
      String b = resultSet.getString("b");
      return new Customer(id, a, b);
    }

You can have a look at this blog: Green Beans: Getting Started with Spring in your Service Tier, it describe this a little bit. (Only the last section in this blog is about JPA.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文