SpringBoot整合Mybatisplus的常用查询方法

最近在学习MybatisPlus是总结了一些常见的方法,记录下来方便之后使用:

BaseMapper源码展示:

public interface BaseMapper {
    
    /**
     * 

* 根据 ID 查询 *

* *
@param id 主键ID */ T selectById(Serializable id); /** *

* 查询(根据ID 批量查询) *

* *
@param idList 主键ID列表(不能为 null 以及 empty) */ List selectBatchIds(@Param(Constants.COLLECTION) Collectionextends Serializable> idList); /** *

* 查询(根据 columnMap 条件) *

* *
@param columnMap 表字段 map 对象 */ List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap); }

1.根据ID查询实体类信息,例如:

@Test
    public void selectById(){
        User user = userMapper.selectById(1088250446457389058L);
        System.out.println(user);
    }

结果:

User(id=1088250446457389058, name=李艺伟, age=28, [email protected], managerId=1088248166370832385, createTime=2019-02-14T08:31:16, remark=null)

2.根据ID批量查询,例如:

@Test
    public void selectIds(){
        List idList = Arrays.asList(1088248166370832385L,
                1094592041087729666L,1094590409767661570L);
        List userList = userMapper.selectBatchIds(idList);
        userList.forEach(System.out :: println);
    }

结果:

User(id=1088248166370832385, name=王天风, age=25, [email protected], managerId=1087982257332887553, createTime=2019-02-05T11:12:22, remark=null)
User(id=1094590409767661570, name=张雨琪, age=31, [email protected], managerId=1088248166370832385, createTime=2019-01-14T09:15:15, remark=null)
User(id=1094592041087729666, name=刘红雨, age=32, [email protected], managerId=1088248166370832385, createTime=2019-01-14T09:48:16, remark=null)

3.根据 columnMap 条件查询,例如:

@Test
    public void selectByMap(){
        //map.put("name","王天风")
        //map.put("age",30)
        //where name="王天风" and age=30
        Map columnMap = new HashMap<>();
        //columnMap.put("name","王天风");
        //列age为数据库中的列名,不是实体类中的属性名
        columnMap.put("age",27);
        List userList = userMapper.selectByMap(columnMap);
        userList.forEach(System.out::println);
    }

结果:

User(id=1212614062686031873, name=向南, age=27, [email protected], managerId=1088248166370832385, createTime=2020-01-02T13:58:16, remark=null)
User(id=1212615304313184258, name=向东, age=27, [email protected], managerId=1088248166370832385, createTime=2020-01-02T14:03:12, remark=null)
User(id=1212616053072060417, name=向西, age=27, [email protected], managerId=1088248166370832385, createTime=2020-01-02T14:06:10, remark=null)

你可能感兴趣的:(SpringBoot整合Mybatisplus的常用查询方法)