mybatisPlus注解版动态拼接sql

前言:前段时间不是在实习嘛,公司用的是注解开发不用xml的,特此记录注解版动态拼接sql,还有使用过程中踩坑集合。先记录1.0版本,以后遇到别的在完善就是。

其实就是在xml那种格式下面最外面一层用< /script > 标签包住就行了,其余部分用英文双引号包住,表达式用单引号包住就ok了,@Param(“idMin”) Integer idMin与#{idMin}对应。不过我这里如果idMin为null那么between null and 20是查不出数值的,< /script > 标签是重点,这种sql业务逻辑读者以后自己改。

/**
 * @author zzh
 * @since 2021-01-26
 */
public interface GoodsMapper extends BaseMapper<Goods> {
    
    
    @Select("<script>"
            + "select * from goods where id between"
            + "<if test='#{idMin}!=null'>" + "#{idMin} and "+"</if>"
            + "<if test='#{idMax}!=null'>" + "#{idMax}" + "</if>"
            + "</script>")
    public List<Goods> select(@Param("idMin") Integer idMin, @Param("idMax") Integer idMax);
}

猜你喜欢

转载自blog.csdn.net/qq_42875345/article/details/113271330
今日推荐