Mybatis动态SQL之IF语句详解

网友投稿 609 2023-01-23

Mybatis动态SQL之IF语句详解

Mybatis动态SQL之IF语句详解

mysql 5.0 以后,支持了动态sql语句,我们可以通过传http://递不同的参数得到我们想要的值.

1. Mybatis–动态SQL之IF语句

没有搭建环境的请点击

1.1 BlogMapper.java

// 查询博客

List queryBlogIf(Map map);

1.2 BlogMapper.xml

select * from mybatis.blog where 1=1

and title = #{title}

and author = #{author}

1.3 Test.java

1.3.1 第一种情况,不加任何查询条件,默认会把所有数据查出来

// 第一种情况,不加任何查询条件,默认会把所有数据查出来

@org.junit.Test

public void test01() {

SqlSession sqlSession = MybatisUtils.getSqlSession();

BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

Map map = new HashMap();

List blogs = mapper.queryBlogIf(map);

for (Blog blog : blogs) {

System.out.println(blog);

}

}

运行结果:

查询出了所有记录

1.3.2 第二种情况,添加参数title

@org.junit.Test

public void test01() {

SqlSession sqlSession = MybatisUtils.getSqlSession();

BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

Map map = new HashMap();

map.put("title", "Spring");

List blogs = mapper.queryBlogIf(map);

for (Blog blog : blogs) {

System.out.println(blog);

}

}

查询出了一条记录

1.3.3 第三种情况,添加2个参数

@org.junit.Test

public void test01() {

SqlSession sqlSession = MybatisUtils.getSqlSession();

BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

Map map = new HashMap();

map.put("title", "微服务");

map.put("author", "天天天");

List blogs = mapper.queryBlogIf(map);

for (Blog blog : blogs) {

System.out.println(blog);

}

}

总结

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:桌面安全应用(电脑安全壁纸软件)
下一篇:如何实现混合app开发(如何实现混合app开发工具)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~