知识分类1知识分类1


单表查询之通用Mapper的使用,通用Mapper只能用来单表查询与操作

Select

  • 方法:List<T> select(T record);

相当于:where name=xx and age =xx
说明:根据实体中的属性值进行查询,查询条件使用等号

  • (常用)方法:T selectByPrimaryKey(Object key);

相当于:where id=xx
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

  • 方法:List<T> selectAll();

相当于:where 1=1
说明:查询全部结果,select(null)方法能达到同样的效果

  • 方法:T selectOne(T record);

说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常MyBatisSystemException ,查询条件使用等号

  • 方法:int selectCount(T record);

相当于:select count(1) from xxx where name=xx and age =xx
说明:根据实体中的属性查询总数,查询条件使用等号

  • (常用)方法:T selectOneByExample(``Object example``)

说明:根据Example条件进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

  • (常用)方法:List<T> selectByExample(Object example);

说明:根据Example条件进行查询

Insert

自增主键不需要设置id值

  • 方法:int insert(T record);

说明:保存一个实体,null的属性也会保存,不会使用数据库默认值

  • (常用)方法:int insertSelective(T record);

    1. <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id">
    2. insert into t_user
    3. <trim prefix="(" suffix=")" suffixOverrides=","> <!-- 增加(前缀,)后缀,自动忽略多余后缀 -->
    4. <if test="userName != null and '' != userName">
    5. user_name ,
    6. </if>
    7. <if test="phone != null and '' != phone">
    8. phone,
    9. </if>
    10. <if test="creatTime != null">
    11. creat_time,
    12. </if>
    13. <if test="isDelect != null">
    14. is_delect,
    15. </if>
    16. </trim>
    17. values
    18. <trim prefix="(" suffix=")" suffixOverrides=",">
    19. <if test="userName != null and '' != userName">
    20. #{userName} ,
    21. </if>
    22. <if test="phone != null and '' != phone">
    23. #{phone} ,
    24. </if>
    25. <if test="creatTime != null">
    26. #{author} ,
    27. </if>
    28. <if test="isDelect != null">
    29. #{isDelect} ,
    30. </if>
    31. </trim>
    32. </insert>

    说明:保存一个实体,null的属性不会保存,会使用数据库默认值

    Update

  • 方法:int updateByPrimaryKey(T record);

说明:根据主键更新实体全部字段,null值会被更新

  • (常用)方法:int updateByPrimaryKeySelective(T record);

说明:根据主键更新属性不为null的值

Delete(不常用,只进行伪删除)

  • 方法:int delete(T record);

说明:根据实体属性作为条件进行删除,查询条件使用等号

  • 方法:int deleteByPrimaryKey(Object key);

说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

Example方法

  • 方法:List<T> selectByExample(Object example);

说明:根据Example条件进行查询
重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列

  • 方法:int selectCountByExample(Object example);

说明:根据Example条件进行查询总数

  • 方法:int updateByExample(@Param("record") T record, @Param("example") Object example);

说明:根据Example条件更新实体record包含的全部属性,null值会被更新

  • (常用)方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);

说明:更改Example条件查询出的数据,更改内容为实体record包含的不是null的属性值

  • 方法:int deleteByExample(Object example);

说明:根据Example条件删除数据


代码演示:

第一种写法:


Example example = new Example(ODMInterviewerInformationT.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("applicantAccount",interviewer.getApplicantAccount());
if (!StringUtils.isEmpty(interviewer.getInterviewerType())){
    criteria.andEqualTo("interviewerType",interviewer.getInterviewerType());
}
if (!CollectionUtils.isEmpty(interviewer.getInterviewerTypeList())){
    criteria.andIn("interviewerType",interviewer.getInterviewerTypeList());
}
if (!StringUtils.isEmpty(interviewer.getInterviewLanguage())){
    criteria.andLike("interviewLanguage","%"+ interviewer.getInterviewLanguage() +"%");
}
criteria.setOrderByClause("resume_id desc,created_time desc limit 0,1");
List<ODMInterviewerInformationT> list = interviewerInformationTDao.selectByExample(example);

第二种写法:


Example example=Example.builder(OdrmResumeOperationHistoryT.class)
            .select("id","creatBy")
            .andWhere(Sqls.custom()
                .andEqualTo("resumeId",resumeId))
            .andWhere(Sqls.custom()
                .orEqualTo("operationType",ResumeOperatingType.RESUME_OPERATING_FREED.getOperatingType())
                .orEqualTo("operationType",ResumeOperatingType.RESUME_SLA_SCHEDULER.getOperatingType()))
            .orderByDesc("opratingTime")
        .build();
List<OdrmResumeOperationHistoryT> historyTS=operationHistoryTDao.selectByExample(example);
if(CollectionUtil.isNullOrEmpty(historyTS)){
        return null;
}

SpringBoot整合通用Mapper

SpringBoot整合通用Mapper


🚀资料学习与引用