局部更新,批量删除,分页查询

结构

image

1. 局部更新(Partial Update)

代码实现

EmployeeMapper.java 中:

@UpdateProvider(type = EmployeeMapper.SQLProvider.class, method = "update")
int patch(Employee employee);

class SQLProvider {
    public String update(Employee employee) {
        StringBuffer sql = new StringBuffer("update employee set ");
        if (!StringUtils.isEmpty(employee.getName())) {
            sql.append("name=#{name},");
        }
        if (!StringUtils.isEmpty(employee.getSal())) {
            sql.append("sal=#{sal},");
        }
        if (!StringUtils.isEmpty(employee.getSex())) {
            sql.append("sex=#{sex},");
        }
        String substring = sql.substring(0, sql.length() - 1);
        substring += " where id=#{id}";
        return substring;
    }
}

2. 批量删除(Batch Delete)

代码实现

EmployeeMapper.java 中:

@DeleteProvider(type = EmployeeMapper.SQLProvider.class, method = "deleteByIds")
int deleteByIds(Long[] ids);

class SQLProvider {
    public String deleteByIds(Long[] ids){
        StringBuffer sql = new StringBuffer("delete from employee where id in(");
        for (Long id : ids) {
            sql.append(id).append(",");
        }
        String substring = sql.substring(0, sql.length()-1);
        substring+")";
        return substring;
    }
}

实现原理

  1. SQL构建
    • 首先构建 delete from employee where id in( 开头的SQL
    • 然后遍历ID数组,将每个ID添加到SQL中
    • 最后移除末尾的逗号,并添加闭合括号 )

3. 分页查询(Pagination)

代码实现

第一步:创建分页查询对象

@Data
public class EmployeeQO {
    private String name;           // 姓名查询条件
    private BigDecimal salStart;   // 薪资起始值
    private BigDecimal salEnd;     // 薪资结束值
    private int pageSize = 10;     // 每页显示条数
    private int currentPage = 1;   // 当前页码
    
    // 计算起始位置
    public int getStart() {
        return (currentPage - 1) * pageSize;
    }
}

第二步:修改Mapper接口

@SelectProvider(type = EmployeeMapper.SQLProvider.class, method = "queryByCondition")
List<Employee> queryByCondition(EmployeeQO qo);

@SelectProvider(type = EmployeeMapper.SQLProvider.class, method = "queryCountByCondition")
int queryCountByCondition(EmployeeQO qo);

第三步:修改SQLProvider类

public String queryByCondition(EmployeeQO qo) {
    StringBuffer sql = new StringBuffer("select * from employee where 1=1");
    // 添加查询条件
    if (!StringUtils.isEmpty(qo.getName())) {
        sql.append(" and name like concat('%',#{name},'%')");
    }
    if (qo.getSalStart() != null) {
        sql.append(" and sal>=#{salStart}");
    }
    if (qo.getSalEnd() != null) {
        sql.append(" and sal<=#{salEnd}");
    }
    // 添加分页
    sql.append(" limit #{start}, #{pageSize}");
    return sql.toString();
}

public String queryCountByCondition(EmployeeQO qo) {
    StringBuffer sql = new StringBuffer("select count(*) from employee where 1=1");
    // 添加相同的查询条件
    if (!StringUtils.isEmpty(qo.getName())) {
        sql.append(" and name like concat('%',#{name},'%')");
    }
    if (qo.getSalStart() != null) {
        sql.append(" and sal>=#{salStart}");
    }
    if (qo.getSalEnd() != null) {
        sql.append(" and sal<=#{salEnd}");
    }
    return sql.toString();
}

实现原理

  1. 分页参数

    • pageSize:每页显示的记录数
    • currentPage:当前页码
    • start:计算起始位置,公式为 (currentPage - 1) * pageSize
  2. SQL构建

    • 构建基础查询条件
    • 添加 limit #{start}, #{pageSize} 子句实现分页
    • 另外需要一个查询总记录数的方法,用于计算总页数
  3. 分页查询流程

    • 计算总记录数
    • 计算总页数 totalPages = (totalCount + pageSize - 1) / pageSize
    • 查询当前页的数据
    • 返回数据和分页信息

分页查询的核心公式

  1. 起始位置start = (currentPage - 1) * pageSize
  2. 总页数totalPages = (totalCount + pageSize - 1) / pageSize

4. 总结

局部更新

  • 使用 @UpdateProvider 动态生成SQL
  • 根据字段是否为空决定是否包含在更新语句中
  • 必须包含主键字段用于定位记录

批量删除

  • 使用 @DeleteProvider 动态生成SQL
  • 使用 in 子句包含多个ID
  • 一次操作删除多条记录

分页查询

  • 使用 limit 子句实现分页
  • 需要计算起始位置和总页数
  • 同时查询数据和总记录数

posted on 2026-01-30 18:42  LVjiani  阅读(5)  评论(0)    收藏  举报

导航