• Mybatis-动态SQL


    • 目录

      需求:

      if标签的使用

      实例操作

      :循环遍历标签

      实例操作

      sql片段的抽取


    • 需求:

    • Mybatis映射配置文件中,前面所用的SQL都是比较简单的
    • 有时候业务逻辑复杂时,我们的SQL就是动态变化的,此时在前面学习的SQL就不能满足要求了
    • if标签的使用

    • :条件标签
    • 如果有动态条件,则使用该标签代替where关键字
    • :条件判断标签
    • 查询条件拼接
    • 实例操作

      1. //多条件查询
      2. public abstract List selectCondition(Student stu);
      1. <select id="selectCondition" resultType="student" parameterType="student">
      2. SELECT * FROM student
      3. <where>
      4. <if test="sid != null">
      5. sid = #{sid}
      6. if>
      7. <if test="name != null">
      8. AND name = #{name}
      9. if>
      10. <if test="age != null">
      11. AND age = #{age}
      12. if>
      13. where>
      14. select>
      1. @Test
      2. public void selectConditon() throws Exception{
      3. //1.加载核心配置文件
      4. InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
      5. //2.获取SqlSession工厂对象
      6. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      7. //3.通过工厂对象获取SqlSession
      8. SqlSession sqlSession = sqlSessionFactory.openSession(true);
      9. //4.获取StudentMapper接口的实现类对象
      10. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
      11. Student stu = new Student();
      12. stu.setSid(2);
      13. stu.setName("李四");
      14. //stu.setAge(24);
      15. List list = mapper.selectCondition(stu);
      16. for(Student student : list){
      17. System.out.println(student);
      18. }
      19. sqlSession.close();
      20. is.close();
      21. }
    • :循环遍历标签

    • 适用于多个参数或者的关系
    • 实例操作

      1. //根据多个id查询
      2. public abstract List selectByIds(List sids);
      1. <select id="selectByIds" resultType="student" parameterType="list">
      2. SELECT * FROM student
      3. <where>
      4. <foreach collection="list" open="sid IN (" close=")" item="sid" separator=",">
      5. #{sid}
      6. foreach>
      7. where>
      8. select>
      1. @Test
      2. public void selectByIds() throws Exception{
      3. //1.加载核心配置文件
      4. InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
      5. //2.获取SqlSession工厂对象
      6. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      7. //3.通过工厂对象获取SqlSession
      8. SqlSession sqlSession = sqlSessionFactory.openSession(true);
      9. //4.获取StudentMapper接口的实现类对象
      10. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
      11. List sids = new ArrayList<>();
      12. sids.add(1);
      13. sids.add(2);
      14. List list = mapper.selectByIds(sids);
      15. for(Student student : list){
      16. System.out.println(student);
      17. }
      18. sqlSession.close();
      19. is.close();
      20. }
    • sql片段的抽取

    • 我们可以将一些重复性的SQL语句进行抽取,以达到复用的效果
    • :抽取SQL语句的标签
    • 抽取的SQL语句
    • :引入SQL片段标签
    • <sql id="select">SELECT * FROM studentsql>
      1. <select id="selectAll" resultType="student">
      2. <include refid="select"/>
      3. select>
      4. <select id="selectByIds" resultType="student" parameterType="list">
      5. <include refid="select"/>
      6. <where>
      7. <foreach collection="list" open="sid IN (" close=")" item="sid" separator=",">
      8. #{sid}
      9. foreach>
      10. where>
      11. select>
  • 相关阅读:
    第十九章总结
    [附源码]SSM计算机毕业设计中小学微课学习系统JAVA
    TypeScript 函数与以及类的使用
    安装搭建PHP域名授权验证系统、盗版可以追踪双重鉴权、在线加密功能二次开发部署
    剧院建筑三维可视化综合管控平台提高安全管理效率
    网络常见面试题
    Bert不完全手册5. 推理提速?训练提速!内存压缩!Albert
    python数据结构与算法-07_哈希表
    融合一致性正则与流形正则的半监督深度学习算法
    C#禁用或启用任务管理器
  • 原文地址:https://blog.csdn.net/weixin_59624686/article/details/126170269