mybatis-plus

发布时间 2023-12-10 14:42:07作者: ジ绯色月下ぎ

查询:

        LambdaQueryWrapper<HarmBehavorInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HarmBehavorInfo::getOnlyId, dictId);
        wrapper.eq(HarmBehavorInfo::getDeleteFlag, 0);
        HarmBehavorInfo harmBehavorInfo = harmBehavorInfoMapper.selectOne(wrapper);
        
        Optional.ofNullable(harmBehavorInfo).orElseThrow(() -> new CustomException("500", "危害行为不存在!"));

查询:

       HarmBehavorInfo harmBehavorInfo = harmBehavorInfoMapper.selectOne(Wrappers.lambdaQuery(new HarmBehavorInfo())
                    .eq(HarmBehavorInfo::getOnlyId, e.getHarmBehavorId())
                    .eq(HarmBehavorInfo::getDeleteFlag, 0));
Optional.ofNullable(harmBehavorInfo).orElseThrow(()
-> new CustomException("500", "危害行为元素不存在!"));

mapper自定义sql:涉及批量的需要script标签  简单的增删改查 直接写sql

    @Select("<script> "+
            "select project_id,vehicle_id,count(distinct harm_behavor_id) as homeCount " +
            "from harm_behavor_detail " +
            "where delete_flag = '0' " +
            "and project_id in " +
            "<foreach item='item' index='index' collection='projectIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach> " +
            "group by project_id,vehicle_id " +
            "</script>")
    List<HomeCountVO> getHomeCount(@Param("projectIds") List<String> projectIds);

分页查询--配置:

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {
    /**
     * @Description 分页插件
     * @param
     * @exception
     * @return com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }


}

分页查询--使用:

// mapper
    @Select("select distinct rc.vehicle_id,rc.node_id,rc.project_idef.id as 'element_function_id' "+
    "from relational_component rc,system_info si, element_function ef "+
    "where rc.vehicle_id = #{vehicleId} " +
    "order by si.id asc,ef.id asc"
    )
    public IPage<Map<String, Object>> findHarmBehavorSystemFunc(Page page, String vehicleId);

//service
IPage<Map<String, Object>> list = harmBehavorSystemFuncMapper.findHarmBehavorSystemFunc(new Page(page, size), pageId);
if (ObjectUtils.isEmpty(list)){
    throw new CustomException(ApiResultType.QUERYRESULT_NULL);
}
List<Map<String, Object>> data = list.getRecords();

只需要传入Page参数,框架自动帮我们分页

 

 

mybatis-plus 执行sql  java代码中传递sql参数  long count = studentMapper.runSql("truncate table student");

@Mapper
public interface StudentMapper extends CommonMapper {

    /**
     * 执行更新sql
     *
     * @param sqlStr
     */
    @Select("${sqlStr}")
    Long runSql(@Param(value = "sqlStr") String sqlStr);

    /**
     * 执行
     * @return
     */
    @Select("${sqlStr}")
    List<Map<String, String>> runQuerySql(@Param(value = "sqlStr") String sqlStr);
}