在 MyBatis Plus 中,使用 Map 来实现数据库操作

假设我们有一个数据库表 Student,包含字段 id、name 和 age,下面是使用 MyBatis Plus 和 Map 实现增删改查的示例:

1、插入数据:

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public void insertStudent() {
        Map<String, Object> studentMap = new HashMap<>();
        studentMap.put("name", "John");
        studentMap.put("age", 20);

        // 插入数据
        studentMapper.insert(studentMap);
    }
}

2、查询数据:

@Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public List<Map<String, Object>> getStudents() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "age").eq("age", 20);

        // 查询数据
        return studentMapper.selectMaps(queryWrapper);
    }
}

3、更新数据:

@Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public void updateStudent(Long id) {
        Map<String, Object> updateMap = new HashMap<>();
        updateMap.put("name", "Updated Name");
        updateMap.put("age", 25);

        // 更新数据
        studentMapper.updateById(id, updateMap);
    }
}

4、删除数据:

@Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public void deleteStudent(Long id) {
        // 删除数据
        studentMapper.deleteById(id);
    }
}

 

5、分页查询

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public Page<Map<String, Object>> getStudentsByPage(int pageNum, int pageSize) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "age").eq("age", 20);

        // 构建分页对象
        Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);

        // 分页查询
        return studentMapper.selectMapsPage(page, queryWrapper);
    }
}

 

以下是一些常见的 QueryWrapper 方法:

  1. 查询条件设置:

    • eq:等于查询,用于设置字段等于某个值的查询条件。
    • ne:不等于查询,用于设置字段不等于某个值的查询条件。
    • gt:大于查询,用于设置字段大于某个值的查询条件。
    • lt:小于查询,用于设置字段小于某个值的查询条件。
    • ge:大于等于查询,用于设置字段大于等于某个值的查询条件。
    • le:小于等于查询,用于设置字段小于等于某个值的查询条件。
    • like:模糊查询,用于设置字段模糊匹配某个字符串的查询条件。
    • in:in 查询,用于设置字段值在某个集合中的查询条件。
    • notIn:not in 查询,用于设置字段值不在某个集合中的查询条件。
    • isNull:空值查询,用于设置字段值为 null 的查询条件。
    • isNotNull:非空值查询,用于设置字段值不为 null 的查询条件。
    • between:范围查询,用于设置字段在某个范围内的查询条件。
    • andor:用于设置复杂的 AND 和 OR 条件组合。
  2. 排序设置:

    • orderByAsc:设置查询结果的升序排序方式。
    • orderByDesc:设置查询结果的降序排序方式。
  3. 分组设置:

    • groupBy:用于设置查询结果的分组字段。
  4. 分页设置:

    • last:设置查询条件的 SQL 片段,可以用于自定义特殊查询条件。
posted @ 2023-08-01 09:53  小张在搬砖  阅读(1247)  评论(0编辑  收藏  举报