栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis Example简单使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis Example简单使用

mybatis Example 特性

Example - 新增

场景:订单模块。查询条件:订单号|订单时间|状态|明细|订单名称

简单的增删改业务,不需要涉及查询SQL。直接用Example,Po搞定,上代码 ↓。

OrderPo po=new OrderPo();
po.setOrderNo('xxx123456');
orderMappingMapper.insert(po)

Example - 查询

等同查询 - andEqualTo

Example example=new Example(OrderPo.class);
Example.Criteria criteria=example.createCriteria();
criteria.andEqualTo("orderNo",request.getOrderNo());
List list=orderMappingMapper.selectByExample(example);

复制代码

时间查询 - andGreaterThanOrEqualTo - 大于等于

criteria.andGreaterThanOrEqualTo("orderTimeStart",request.getOrderTimeStart()); // >=orderTimeStart  

复制代码

时间查询 - andLessThan - 小于

criteria.andLessThan("orderTimeEnd",request.getOrderTimeEnd());//  

复制代码

明细 ids in - andIn

criteria.andIn("recIds",request.getOrderDetails());//相当于sql recIds in(xxx,xxx,xxx)

复制代码

模糊查询 - andCondition

String condition =" and orderName like = '%"+request.getOrderName()+"%'";
criteria.andCondition(condition);

复制代码

排序 - setOrderByClause

example.setOrderByClause(" create_time desc ");//时间降序排序

Example - 修改

更新不为空的值

Example example = new Example(OrderPo.class);
example.createCriteria().andEqualTo("orderNo", request.getOrderNo());//订单号为request.getOrderNo()
OrderPo po=new OrderPo();
po.setModifyTime(new Date());
po.setModifyUser("admin");
po.setModifyUserName("admin");
po.setStatus("1");//已审核
orderMappingMapper.updateByExampleSelective(po,example);

复制代码

也可以通过主键更新

OrderPo po=new OrderPo();
po.setOrderNo(rquest.getOrderNo());//主键
po.setModifyTime(new Date());
po.setModifyUser("admin");
po.setModifyUserName("admin");
po.setStatus("1");//已审核
orderMappingMapper.updateByPrimaryKeySelective(po);

复制代码
Example - 删除

删除同样可以根据条件删除

Example example = new Example(OrderPo.class);
example.createCriteria().andEqualTo("status", "1");//删除已审核的数据
orderMappingMapper.deleteByExample(po);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1013930.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号