您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# MyBatis中Example如何使用
## 一、Example概述
Example是MyBatis Generator(MBG)自动生成的一种查询辅助类,主要作用是通过面向对象的方式构建动态SQL查询条件,避免手动编写复杂WHERE子句。它特别适用于单表查询场景,能显著提升开发效率。
## 二、Example核心类结构
### 1. 主要组成部分
```java
// 典型Example类结构
public class UserExample {
protected String orderByClause; // 排序字段
protected boolean distinct; // 是否去重
protected List<Criteria> oredCriteria; // 条件集合
// Criteria内部类
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
// 各种条件方法...
}
public static class Criteria extends GeneratedCriteria {
// 具体实现...
}
}
UserExample example = new UserExample();
// 创建条件对象
UserExample.Criteria criteria = example.createCriteria();
// 等值查询
criteria.andUsernameEqualTo("admin");
// 范围查询
criteria.andAgeBetween(18, 30);
// IN查询
criteria.andIdIn(Arrays.asList(1, 2, 3));
// 模糊查询
criteria.andNameLike("%张%");
// 第一个条件组
UserExample.Criteria criteria1 = example.createCriteria();
criteria1.andStatusEqualTo(1);
// 第二个条件组(OR连接)
UserExample.Criteria criteria2 = example.createCriteria();
criteria2.andIsVipEqualTo(true);
example.or(criteria2);
// 设置排序
example.setOrderByClause("create_time DESC, id ASC");
// 分页(需配合PageHelper)
PageHelper.startPage(1, 10);
List<User> users = userMapper.selectByExample(example);
// 嵌套条件
example.createCriteria()
.andAgeGreaterThan(18)
.andGenderEqualTo("M")
.andCreateTimeBetween(startDate, endDate);
// 添加自定义SQL条件
criteria.andCondition("salary > (select avg(salary) from user)");
// 使用函数
criteria.andCondition("date_format(create_time,'%Y-%m')='2023-01'");
虽然Example主要用于单表查询,但可通过子查询模拟关联:
criteria.andDeptIdIn(
deptMapper.selectByExample(
new DeptExample().createCriteria()
.andNameLike("技术%")
).stream().map(Dept::getId).collect(Collectors.toList())
);
public List<User> searchUsers(String username, Integer minAge,
Integer maxAge, String sortField) {
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
if(StringUtils.isNotBlank(username)){
criteria.andUsernameLike("%"+username+"%");
}
if(minAge != null){
criteria.andAgeGreaterThanOrEqualTo(minAge);
}
if(maxAge != null){
criteria.andAgeLessThanOrEqualTo(maxAge);
}
if(StringUtils.isNotBlank(sortField)){
example.setOrderByClause(sortField + " DESC");
}
return userMapper.selectByExample(example);
}
User record = new User();
record.setStatus(0); // 待更新字段
UserExample example = new UserExample();
example.createCriteria()
.andLastLoginTimeLessThan(DateUtils.addMonths(new Date(), -6));
userMapper.updateByExampleSelective(record, example); // 只更新非null字段
Q:如何实现WHERE (A AND B) OR (C AND D)结构?
UserExample example = new UserExample();
// 第一组:A AND B
Criteria criteria1 = example.createCriteria();
criteria1.andAEqualTo(value1);
criteria1.andBEqualTo(value2);
// 第二组:C AND D(OR连接)
Criteria criteria2 = example.createCriteria();
criteria2.andCEqualTo(value3);
criteria2.andDEqualTo(value4);
example.or(criteria2);
Q:如何清空已有条件?
example.clear(); // 重置所有条件
通过合理使用Example,可以大幅减少样板代码,使查询逻辑更加清晰。但对于复杂业务场景,建议结合XML映射文件实现更灵活的SQL控制。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。