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

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

其中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 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 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 demos = mybatisDemoMapper.selectByExample(example);

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

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

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

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

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值