mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

mybatis提供各种标签方法实现动态拼接sql。

1. if&where

1.2 需求

用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。

对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

1.3 mapper.xml

<select id="findUserList" parameterType="joanna.yan.mybatis.entity.UserQueryVo" resultType="joanna.yan.mybatis.entity.UserCustom">SELECT * FROM USER<!--where可以自动去掉条件中的第一个and  --><where><if test="userCustom!=null"><if test="userCustom.sex!=null and userCustom.sex!=''">and user.sex=#{userCustom.sex}</if><if test="userCustom.username!=null and userCustom.username!=''">and user.username LIKE '%${userCustom.username}%'</if></if></where></select><select id="findUserCount" parameterType="joanna.yan.mybatis.entity.UserQueryVo" resultType="int">SELECT count(*) FROM USER <!--where可以自动去掉条件中的第一个and  --><where><if test="userCustom!=null"><if test="userCustom.sex!=null and userCustom.sex!=''">and user.sex=#{userCustom.sex}</if><if test="userCustom.username!=null and userCustom.username!=''">and user.username LIKE '%${userCustom.username}%'</if></if></where></select>

1.4测试代码

 @Testpublic void findUserListTest() throws Exception{SqlSession sqlSession=sqlSessionFactory.openSession();UserMapper userMapper=sqlSession.getMapper(UserMapper.class);//创建包装对象,设置查询条件UserQueryVo userQueryVo=new UserQueryVo();UserCustom userCustom=new UserCustom();//由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
//        userCustom.setSex("1");userCustom.setUsername("张三丰");userQueryVo.setUserCustom(userCustom);List<UserCustom> list=userMapper.findUserList(userQueryVo);System.out.println(list);}

打印的sql:如果不设置sex的值,条件不会拼接在sql中

2.sql片段

2.1 需求

将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。方便程序员进行开发。

2.2 定义sql片段

<!--定义sql片段id:sql片段的唯一标识经验:1.是基于单表来定义sql片段的,这样的话这个sql片段可重用性才高2.在sql片段中不要包括where--><sql id="query_user_where"><if test="userCustom!=null"><if test="userCustom.sex!=null and userCustom.sex!=''">and user.sex=#{userCustom.sex}</if><if test="userCustom.username!=null and userCustom.username!=''">and user.username LIKE '%${userCustom.username}%'</if></if></sql>

2.3 引用sql片段

在mapper.xml中定义statement中引用sql片段:

 <select id="findUserList" parameterType="joanna.yan.mybatis.entity.UserQueryVo" resultType="joanna.yan.mybatis.entity.UserCustom">SELECT * FROM USER<!--where可以自动去掉条件中的第一个and  --><where><!--引用sql片段的id,如果refid指定的id不在本mapper文件中,需要在前边加namespace  --><include refid="query_user_where"></include><!--在这里还可以引用其它的sql片段  --></where></select><select id="findUserCount" parameterType="joanna.yan.mybatis.entity.UserQueryVo" resultType="int">SELECT count(*) FROM USER <!--where可以自动去掉条件中的第一个and  --><where><!--引用sql片段的id,如果refid指定的id不在本mapper文件中,需要在前边加namespace  --><include refid="query_user_where"></include><!--在这里还可以引用其它的sql片段  --></where></select>

3. foreach

向sql传递数组或List,mybatis使用foreach解析。

3.1 需求

在用户查询列表和查询总数的statement中增加多个id输入查询。

sql语句如下,两种方法:

SELECT * FROM USER WHERE id=1 OR id=10 OR id=16

SELECT * FROM USER WHERE id IN(1,10,16)

3.2 在输入参数类型中添加List<Integer> ids传入多个id

3.3 修改mapper.xml

WHERE id=1 OR id=10 OR id=16

在前面的查询条件中,查询条件定义成了一个sql片段,现在我们需要修改sql片段。

 <!--定义sql片段id:sql片段的唯一标识经验:1.是基于单表来定义sql片段的,这样的话这个sql片段可重用性才高2.在sql片段中不要包括where--><sql id="query_user_where"><if test="userCustom!=null"><if test="userCustom.sex!=null and userCustom.sex!=''">and user.sex=#{userCustom.sex}</if><if test="userCustom.username!=null and userCustom.username!=''">and user.username LIKE '%${userCustom.username}%'</if><if test="ids!=null"><!--使用foreach遍历传入的idscollection:指定输入对象中集合属性item:每个遍历生成的对象名open:开始遍历时拼接的串close:结束遍历时拼接的串separator:遍历的两个对象中需要拼接的串--><!--是要实现下边的sql拼接:AND (id=1 OR id=10 OR id=16)--><foreach collection="ids" item="user_id" open="AND (" close=")" separator="or"><!--每个遍历需要拼接的串  -->id=#{user_id}</foreach></if></if></sql>

3.4 测试代码

 @Testpublic void findUserListTest() throws Exception{SqlSession sqlSession=sqlSessionFactory.openSession();UserMapper userMapper=sqlSession.getMapper(UserMapper.class);//创建包装对象,设置查询条件UserQueryVo userQueryVo=new UserQueryVo();UserCustom userCustom=new UserCustom();//由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
//        userCustom.setSex("1");userCustom.setUsername("小明");//传入多个idList<Integer> ids=new ArrayList<>();ids.add(1);ids.add(10);ids.add(16);userQueryVo.setIds(ids);userQueryVo.setUserCustom(userCustom);List<UserCustom> list=userMapper.findUserList(userQueryVo);System.out.println(list);}

转载自 http://www.cnblogs.com/Joanna-Yan/p/6908763.html

Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql相关推荐

  1. Spring+SpringMVC+MyBatis深入学习及搭建(十)——MyBatis逆向工程

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6973266.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(九)--My ...

  2. Spring+SpringMVC+MyBatis深入学习及搭建(十一)——SpringMVC架构

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6985816.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十)--My ...

  3. Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7098753.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十六)--S ...

  4. Spring+SpringMVC+MyBatis深入学习及搭建(十四)——SpringMVC和MyBatis整合

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7010363.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十三)--S ...

  5. Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发

    前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)--MyBatis的基础知识.MybatisFirst中存在大量重复的代码.这次简化下代码: 使用MyBatis开发Dao ...

  6. Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合

    1.整合思路 需要Spring通过单例方式管理SqlSessionFactory. Spring和MyBatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(Sp ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(一)——MyBatis的基础知识

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6812311.html 1.对原生态jdbc程序中问题总结 1.1 jdbc程序 需求:使用jdbc查询mys ...

  8. [Spring+SpringMVC+Mybatis]框架学习笔记(四):Spring实现AOP

    上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(三):Spring实现JDBC 下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(五):SpringA ...

  9. 【MyBatis】学习纪要六:动态SQL

    2019独角兽企业重金招聘Python工程师标准>>> 引言 动态SQL:Dynamic SQL. 本节我们来通过 MyBatis 的官方文档进行学习. Description(描述 ...

最新文章

  1. java 中启动线程的正确方式
  2. 【PP】工作中心(Work Center)
  3. css 固定内容显示范围,css之让文字在一定范围内显示,不超过固定的宽度和高度...
  4. pcb板材的tg是什么_做到这6点,PCB过回焊炉不会出现板弯及板翘!
  5. 如何给网页标题添加icon小图标
  6. if or函数套用_IF函数和OR函数的套用我想利用IF函数和 – 手机爱问
  7. SQL基本语法 case when 应用
  8. 数据可视化技术对于企业的重要性
  9. 【项目实施随笔】生产领料
  10. 《Javascript秘密花园》学习笔记(下)
  11. (一)PyQt5系列教程:使用PyQt5创建一个简单的demo
  12. BP神经网络预测实例(matlab代码,神经网络工具箱)
  13. Win10 台式机机箱前置耳机插孔没声音如何修复
  14. 网络负载率计算公式 linux,如何理解Linux CPU负载率的计算方式
  15. FreeSWITCH ODBC cdr
  16. 【010 Editor】010 Editor安装教程
  17. 从古代遗传下来的设计值得一看!
  18. 计算机二级网页暂时无法连接,小编教你二级网页打不开怎么解决
  19. 2020牛客暑期多校训练营(第九场) Groundhog and Gaming Time
  20. 《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作

热门文章

  1. NameError: name 'file' is not defined
  2. 31. 脱壳篇-什么是壳
  3. python实现根据经纬度画地图热力图
  4. 如何在Mac上用汇编语言写HelloWorld
  5. 【原创】大叔问题定位分享(12)Spark保存文本类型文件(text、csv、json等)到hdfs时为什么是压缩格式的...
  6. windows系统 安装MongoDB
  7. JS任务队列--笔记
  8. 说一下安卓的touch事件分发机制
  9. 玩玩IronPython
  10. 【转】各种树:trie树、B树、B-树、B+树、B*树