Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

spring batch - SqlPagingQueryProviderFactoryBean to support In clause

I want to write an SQL for SqlPagingQueryProviderFactoryBean. I will pass the parameter for an IN clause. I am not getting the result when I am passing it as a parameter(?). But I am getting the correct result when I am hard coding the values.

Please guide me on this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot have a single place holder and replace it with an array due to sql injection security policy, However the gettter/setters of sqlPagingQueryProvider properties selectclause, fromClause and whereCLause are String and not preparedStatement. The PreparedStatement would be constructed later by spring batch during post construct method. Hence you can send the where clause as String with values(Prepared) and pass it to job as parameter. Hence your code would something of this sort.

String topics = { "topic1", "topic2", "topic3", "topic4"};

StringBuilder str = new StringBuilder("('");

for (String topic : topics) {
    str.append(topic + "','");
}
str.setLength(str.length() - 2);
str.append("')");


final JobParameters jobParameters = new JobParametersBuilder()
                .addLong("time", System.nanoTime())
                .addString("inputsTopics", str.toString())
                .toJobParameters();

And your pagingreader bean would look like below and make sure you set scope to step

<bean id="sqlPagingReader" class="<your extended prging prvder>.KPPageingProvider" scope="step" >
        <property name="dataSource" ref="dataSource" />
        <property name="selectClause" value="u.topic,cu.first_name ,cu.last_name, cu.email" />
        <property name="fromClause" value="ACTIVE_USER_VIEWS_BY_TOPIC u inner join cl_user cu on u.user_id=cu.id" />
        <property name="whereClause" value="u.topic in #{jobParameters['inputsTopics']}" ></property>
</bean>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...