网站建设牜金手指花总十四,食品配送做网站需要什么功能,诚信网站 互联网建站,四川省建设厅消防网站sql片段#xff1a;
有时候可能某个 sql 语句我们用的特别多#xff0c;为了增加代码的重用性#xff0c;简化代码#xff0c;我们需要将这些代码抽取出来#xff0c;然后使用时直接调用。
sql idif-title-authorif testtitle ! null
有时候可能某个 sql 语句我们用的特别多为了增加代码的重用性简化代码我们需要将这些代码抽取出来然后使用时直接调用。
sql idif-title-authorif testtitle ! nulltitle #{title}/ifif testauthor ! nulland author #{author}/if
/sql
引用SQL片段
select idqueryBlogIf parameterTypemap resultTypeblogselect * from blogwhere!-- 引用 sql 片段如果refid 指定的不在本文件中那么需要在前面加上 namespace --include refidif-title-author/include!-- 在这里还可以引用其他的 sql 片段 --/where
/select
Foreach 编写接口
ListBlog queryBlogForeach(Map map);
编写SQL语句
select idqueryBlogForeach parameterTypemap resultTypeblogselect * from blogwhere!--collection:指定输入对象中的集合属性item:每次遍历生成的对象open:开始遍历时的拼接字符串close:结束时拼接的字符串separator:遍历对象之间需要拼接的字符串select * from blog where 11 and (id1 or id2 or id3)--foreach collectionids itemid openand ( close) separatororid#{id}/foreach/where
/select
测试
Test
public void testQueryBlogForeach(){SqlSession session MybatisUtils.getSession();BlogMapper mapper session.getMapper(BlogMapper.class);HashMap map new HashMap();ListInteger ids new ArrayListInteger();ids.add(1);ids.add(2);ids.add(3);map.put(ids,ids);ListBlog blogs mapper.queryBlogForeach(map);System.out.println(blogs);session.close();
}