iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Tk.mybatis零sql语句实现动态sql查询的方法(4种)
  • 237
分享到

Tk.mybatis零sql语句实现动态sql查询的方法(4种)

2024-04-02 19:04:59 237人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录实现方式:方式一:使用Example实现方式二:使用example.createCriteria实现方式三:使用Example.builder实现方式四:使用weekendsql

有时候,查询数据需要根据条件使用动态查询,这时候需要使用动态sql,通常我们会自己写动态sql来实现,比如:


<select id="findStudentByCondition" resultType="com.example.service.entity.Student">
    SELECT id, name, score FROM tbl_student
    <where>
      <if test="score !=null and score > 0">
        score > #{score}
      </if>
      <if test="name !=null and name != ''">
         <bind name="pattern" value=" '%' + name + '%' "/>
        AND name LIKE #{pattern}
      </if>
    </where>
    ORDER BY score DESc
  </select>

        这个sql是查询成绩大于90并且名字包含“i”的学生信息。但是有时候又加了一个条件,又要去改sql,改入参,有没有一种方式可以将写动态sql像写代码一样实现呢?如果你有这个想法,推荐你了解一下Tk.mybatis

      关于Tk.mybatis的介绍以及如何配置,本文暂不介绍,不了解的请自行百度,本文只介绍如何基于tk.mybatis实现不写sql语句也能实现动态sql查询。

实现方式:

1. 使用Example实现

2. 使用Example.createCriteria

3. 使用Example.builder实现

4. 使用WeekendSqls实现

方式一:使用Example实现



  @Test
  public void testSelectByExample() {
    Example example = new Example(Student.class);
    // 设置查询列
    example.selectProperties("id","name","score");
    // 动态sql
    example.and()
        .andGreaterThan("score",90)
        .andLike("name", "%i%");
    // 去重
    example.setDistinct(true);
    // 排序
    example.orderBy("score").desc();
    List<Student> students = studentMapper.selectByExample(example);
    System.out.println(students);
  }

方式二:使用example.createCriteria实现



  @Test
  public void testSelectByExampleCriteria() {
    Example example = new Example(Student.class);
    // 设置查询列
    example.selectProperties("id","name","score");
    // 动态查询
    example.createCriteria()
        .andGreaterThan("score",90)
        .andLike("name", "%i%");
    // 去重
    example.setDistinct(true);
    // 排序
    example.orderBy("score").desc();
    List<Student> students = studentMapper.selectByExample(example);
    System.out.println(students);
  }

方式三:使用Example.builder实现



  @Test
  public void testSelectByExampleBuilder() {
    Example example = Example.builder(Student.class)
        // 查询列
        .select("id","name","score")
        // 动态sql
        .where(Sqls.custom()
            .andGreaterThan("score",90)
            .andLike("name","%i%"))
        // 去重
        .distinct()
        // 排序
        .orderByDesc("score")
        .build();
    List<Student> students = studentMapper.selectByExample(example);
    System.out.println(students);
  }

方式四:使用weekendSqls实现



  @Test
  public void testSelectByWeekendSqls() {
    WeekendSqls<Student> sqls = WeekendSqls.custom();
    sqls = sqls
        .andGreaterThan(Student::getScore,90)
        .andLike(Student::getName,"%i%");
    List<Student> sysRoles = studentMapper.selectByExample(Example.builder(Student.class)
        .select("id","name","score")
        .where(sqls)
        .distinct()
        .orderByDesc("score")
        .build());
    System.out.println(sysRoles);
 
  }

 到此这篇关于Tk.mybatis零sql语句实现动态sql查询的方法(4种)的文章就介绍到这了,更多相关Tk.mybatis 动态sql查询内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Tk.mybatis零sql语句实现动态sql查询的方法(4种)

本文链接: https://www.lsjlt.com/news/158948.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作