为了简化这个问题,是否有可能使 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>
发布评论
评论(1)
您需要为您的对象实现一个 RowMapper,然后您可以使用 jdbcTemplate.queryForObject 方法。
您可以看看这个博客: 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.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.)