mybatis-plus

查询:

        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);
}
复制代码

 

posted @   ジ绯色月下ぎ  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示