Mybatis——example使用案例

本文介绍了Mybatis中Example类的使用,包括如何构建不同的查询条件,如基础查询、排序以及OR条件的组合。通过示例展示了如何生成对应的SQL语句,帮助理解Example类在实际开发中的应用。
摘要由CSDN通过智能技术生成

1、案例:

@Test
public void getEmpListByExample(){
    //1.创建Example对象
    Example example = new Example(Employee.class);
    //2.通过Example对象创建Criteria对象
    Example.Criteria criteria01 = example.createCriteria();
    //5.执行查询
    List<Employee> empList = employeeService.getEmpListByExample(example);

    for (Employee employee : empList) {
        System.out.println(employee);
    }
}

对应SQL:
SELECT emp_id,emp_name,emp_salary,emp_age FROM tabple_emp

2、案例:

@Test
public void getEmpListByExample(){
    //1.创建Example对象
    Example example = new Example(Employee.class);
    //排序
    example.setOrderByClause("emp_salary desc");
    //去重
    example.setDistinct(true);

    //2.通过Example对象创建Criteria对象
    Example.Criteria criteria01 = example.createCriteria();
    criteria01.andGreaterThan("empSalary",4000)
            .andGreaterThan("empAge",38);

    //5.执行查询
    List<Employee>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值