【MyBatis】动态SQL实例

首先来分享几个坑吧,刚才找了一会bug

 

 

 

 

 

 

if

 

 

 @Test
    public void testQueryIf()
    {
        SqlSession sqlSession=MybatisUtils.getSqlSession();
        BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class);
        Map<String,String> map=new HashMap<>();
        map.put("title","Mybatis");
        List<Blog> list=blogMapper.queryIf(map);
        for (Blog blog : list) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

 

 

 

choose when otherwise

 

 

 

 

 

 

 

set

 

 

 

SQL片段

类似于一个重复的sql代码块 进行每次复用

 

 

 

foreach

 

 

 @Test
    public void testForech()
    {
        SqlSession sqlSession=MybatisUtils.getSqlSession();
        BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class);
        Map map=new HashMap();
        List<Integer> list=new ArrayList<>();
        list.add(3);
        list.add(2);
        map.put("list",list);
        List<Blog> blog=blogMapper.queryForeach(map);
        for (Blog blog1 : blog) {
            System.out.println(blog1);
        }
        sqlSession.close();
    }

 

posted @ 2021-01-28 23:52  枫叶像思念  阅读(109)  评论(0编辑  收藏  举报