Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an IN clause in iBATIS?

Tags:

java

sql

ibatis

I'm using iBATIS to create select statements. Now I would like to implement the following SQL statement with iBATIS:

SELECT * FROM table WHERE col1 IN ('value1', 'value2');

With the following approach, the statement is not prepared correctly and no result returns:

SELECT * FROM table WHERE col1 IN #listOfValues#;

iBATIS seems to restructure this list and tries to interpret it as a string.

How can I use the IN clause correctly?

like image 865
guerda Avatar asked Oct 28 '09 14:10

guerda


People also ask

How do I use iBATIS?

To define SQL mapping statement using iBATIS, we would add <select> tag in Employee. xml file and inside this tag definition, we would define an "id" which will be used in IbatisRead. java file for executing SQL SELECT query on database.

What is difference between MyBatis and iBATIS?

MyBatis is a fork from iBATIS, and according to Wikipedia most of iBATIS' developers moved over to MyBatis too. The iBATIS project is currently marked as Inactive, therefore you should go with MyBatis for new projects and only use iBATIS if you're maintaining an existing project which already uses iBATIS.

Does MyBatis use JDBC?

MyBatis does four main things: It executes SQL safely and abstracts away all the intricacies of JDBC. It maps parameter objects to JDBC prepared statement parameters. It maps rows in JDBC result sets to objects.

Is iBATIS a framework?

The iBATIS Data Mapper framework helps solve these problems. iBATIS is a persistence framework that provides the benefits of SQL but avoids the complexity of JDBC.


Video Answer


1 Answers

Here's a blog post that answers your question:

iBatis: Support for Array or List Parameter with SQL IN Keyword

<select id="select-test" resultMap="MyTableResult" parameterClass="list">
select * from my_table where col_1 in
  <iterate open="(" close=")" conjunction=",">
   #[]#
  </iterate>
</select>

And in Java you should pass in a java.util.List. E.g.

List<String> list = new ArrayList<String>(3);
list.add("1");
list.add("2");
list.add("3");
List objs = sqlMapClient.queryForList("select-test",list);
like image 86
jitter Avatar answered Nov 15 '22 18:11

jitter