tkmybatis通用mapper实现在使用Example进行查询的几种方式

如下列举四种方式,但是不止四种哦。其中weekend方式需要升级jdk到1.8及以上。废话不代码!首先定义数据库表映射类:publicclassMybatisDemo{privateLongid;privateLongcount;privateStringname;publicLonggetId(){…

大家好,又见面了,我是你们的朋友全栈君。

如下列举四种方式,但是不止四种哦。

其中weekend方式需要升级jdk到1.8及以上。

废话不代码!

首先定义数据库表映射类:

public class MybatisDemo {
    private Long id;
    private Long count;
    private String name;

    public Long getId() {
        return id;
    }
    public Long getCount() {
        return count;
    }
    public String getName() {
        return name;
    }

//    setter……
}

此处省略了数据库表映射和set方法。

接下来就是实现example查询的几种方式,核心代码如下:

方式一:普通Example方式(从and方法开始可以实现动态sql拼接)

    Example example = new Example(CandidateBrandEntity.class);
    example
      //.selectProperties("cabId","cabName")
        .and().andEqualTo("cabDeleted",0)
        .andLike("cabName","%d%");

    // 排序
    example.orderBy("cabCreatedTime")
        /*.desc()*/
          .orderBy("cabId").desc();

    // 获得结果
    List<CandidateBrandEntity> brands = brandEntityMapper.selectByExample(example);

方式二:Criteria方式(可使用criteria完成动态sql拼接)

Example example = new Example(MybatisDemo.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("count", 0)
        .andLike("name", "%d%");
example.orderBy("count")
        //.desc()
        .orderBy("name").desc();
List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(example);

方式三:Example.builder 方式(其中where从句中内容可以拿出来进行动态sql拼接)

Example example = Example.builder(MybatisDemo.class)
        .select("cabId","cabName")
        .where(Sqls.custom().andEqualTo("count", 0)
        .andLike("name", "%d%"))
        .orderByDesc("count","name")
        .build();
List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(example);

方式四:Example.builder + Weekend方式,优势:不用输入属性名,避免数据库有变动或输入错误就会出错

//获得seekendsql
WeekendSqls<MybatisDemo> sqls = WeekendSqls.<MybatisDemo>custom();

//可进行动态sql拼接
sqls = sqls.andEqualTo(MybatisDemo::getCount,0).andLike(MybatisDemo::getName,"%d%");

//获得结果
List<MybatisDemo> demos = mybatisDemoMapper.selectByExample(Example.builder(MybatisDemo.class).where(sqls).orderByDesc("count","name").build());

 

 

参考内容:https://github.com/abel533/Mapper/wiki/6.example

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/127756.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • ES6数组新方法[通俗易懂]

    ES6数组新方法[通俗易懂]ES6数组新方法目录ES6数组新方法1.`forEach()`和`map()`2.`filter()`3.`reduce()`4.`some()`5.`every()`6.`Array.from()`7.`Array.of()`8.`copyWithin()`9.`find()`和`findIndex()`10.`fill()`11.`entries()`,`keys()`和`values()`12.`includes()`13.`flat()`,`flatMap()`

    2022年6月1日
  • 4月idea激活码_通用破解码「建议收藏」

    4月idea激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
  • Map – LinkedHashSet & LinkedHashMap 源码解析「建议收藏」

    Map – LinkedHashSet & LinkedHashMap 源码解析「建议收藏」总体介绍如果你已看过前面关于HashSet和HashMap,以及TreeSet和TreeMap的讲解,一定能够想到本文将要讲解的LinkedHashSet和LinkedHashMap其实也是一回事。LinkedHashSet和LinkedHashMap在Java里也有着相同的实现,前者仅仅是对后者做了一层包装,也就是说LinkedHashSet里面有一个LinkedHashMap(适配器模式)。因此本文将重点分析LinkedHashMap。LinkedHashMap实现了Map接口,即允许放入key

    2022年10月12日
  • python模块list 转json字符串_python 列表 字典转json[通俗易懂]

    python模块list 转json字符串_python 列表 字典转json[通俗易懂]一、Dictionary转为JSON将dict转为JSON,这里利用包jsonimportjsonaItem={}aItem[“id”]=”2203″aItem[“title”]=”title”aItem[“subTitle”]=”subtitle”bItem={}bItem[“id”]=”2842″bItem[“title”]=”b标题”bItem[“subTi…

    2022年10月18日
  • pycharm界面怎么调成中文版(pycharm怎么破解)

    文章目录汉化方法官方汉化与第三方对比Java的idea在更新2020.1时就更新了官方汉化,当时Pycharm还没用出现汉化,但这两天提示我更新2020.1.1的时候,我发现pycharm也出现了官方汉化,在此建议你要是想用官方汉化,先把你的pycharm升级为最新版本。汉化方法打开pycharm左上角的file(文件)>选择settings(设置)>打开plugins(插件)>搜索chinese(中文插件),下载好后重启pycharm。官方汉化与第三方对比这里使用

    2022年4月10日
  • Action Bar_pull the bar

    Action Bar_pull the barAction Bar 的相关操作

    2022年4月21日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号