Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...