您的位置:首页 > 产品设计 > UI/UE

Mybatis dynamic query

2012-11-28 01:43 295 查看
In my project, I ran into the issue that I have to use Sql In statement and the value in the in() is dynamic. With mybatis, we can create dynamic sql foreach.

Here is my case:

1. In the mapper class, I define my method:

@Component("twitterTweetMapper")
public interface TweetMapper{

public List<Tweet> getTweetsByOrganizationIds(List<Long> organizationIds);

}


2. In the mapper.xml file, I define my sql statement.

<select id="getTweetsByOrganizationIds" resultType="org.twitter.model.Tweet">
SELECT * FROM TWEET
WHERE ORGANIZATIONID in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
ORDER BY TWEETCREATEDATE DESC LIMIT 3
</select>


That's all. It's very easy, right?

For more dynamic queries, read the link:

http://loianegroner.com/2011/03/ibatis-mybatis-working-with-dynamic-queries-sql/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: