MyBatis的JAVA类中,@Select中的sql根据条件执行不同的查询条件

来源:互联网 发布:种植牙 知乎 编辑:程序博客网 时间:2024/05/17 19:58
[java] view plain copy
  1. package cn.erongcai.hrplatform.dao.demand;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.ibatis.annotations.Mapper;  
  6. import org.apache.ibatis.annotations.Param;  
  7. import org.apache.ibatis.annotations.Select;  
  8.   
  9. import com.baomidou.mybatisplus.mapper.BaseMapper;  
  10.   
  11. import cn.erongcai.hrplatform.model.demand.DemandComment;  
  12.   
  13. @Mapper  
  14. public interface DemandCommentMapper extends BaseMapper<DemandComment>{  
  15.       
  16.     @Select("SELECT "  
  17.             + "a.id as 'id',a.create_date as 'createDate',a.content as 'content',"  
  18.             + "a.parent_id as 'parentId',a.first_comment_id as 'firstCommentId',"  
  19.             + "b.id as 'fromUser.id',b.realname as 'fromUser.realname',b.avatar as 'fromUser.avatar',"  
  20.             + "c.id as 'toUser.id',c.realname as 'toUser.realname',c.avatar as 'toUser.avatar' "  
  21.             + "FROM t_demand_comment a "  
  22.             + "LEFT JOIN t_user b ON b.id = a.from_uid "  
  23.             + "LEFT JOIN t_user c ON c.id = a.to_uid "  
  24.             + "WHERE a.demand_id = #{demandId} "  
  25.             + "ORDER BY a.create_date ASC"  
  26.             + "LIMIT #{startNo},#{pageSize}")  
  27.     public List<DemandComment> listDemandComment(@Param("demandId") Long demandId, @Param("startNo") Integer pageNo, @Param("pageSize") Integer pageSize);  
  28. }  

这样整个语句是写死的,如果我想根据pageNo与pageSize是否为空来判断是否需要分页,该怎么做呢?

如果使用xml来配置的话可以用

[html] view plain copy
  1. <when test='startNo!=null and pageSize != null '>  
  2.   LIMIT #{startNo},#{pageSize}  
  3. </when>  
如果是用@Select 这种该如何做呢?

方法:用script标签包围,然后像xml语法一样书写

[java] view plain copy
  1. package cn.erongcai.hrplatform.dao.demand;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.ibatis.annotations.Mapper;  
  6. import org.apache.ibatis.annotations.Param;  
  7. import org.apache.ibatis.annotations.Select;  
  8.   
  9. import com.baomidou.mybatisplus.mapper.BaseMapper;  
  10.   
  11. import cn.erongcai.hrplatform.model.demand.DemandComment;  
  12.   
  13. @Mapper  
  14. public interface DemandCommentMapper extends BaseMapper<DemandComment>{  
  15.       
  16.     @Select("<script>"  
  17.             + "SELECT "  
  18.             + "a.id as 'id',a.create_date as 'createDate',a.content as 'content',"  
  19.             + "a.parent_id as 'parentId',a.first_comment_id as 'firstCommentId',"  
  20.             + "b.id as 'fromUser.id',b.realname as 'fromUser.realname',b.avatar as 'fromUser.avatar',"  
  21.             + "c.id as 'toUser.id',c.realname as 'toUser.realname',c.avatar as 'toUser.avatar' "  
  22.             + "FROM t_demand_comment a "  
  23.             + "LEFT JOIN t_user b ON b.id = a.from_uid "  
  24.             + "LEFT JOIN t_user c ON c.id = a.to_uid "  
  25.             + "WHERE a.demand_id = #{demandId} "  
  26.             + "ORDER BY a.create_date ASC "  
  27.             + "<if test='startNo!=null and pageSize != null '>"  
  28.             + "LIMIT #{startNo},#{pageSize}"  
  29.             + "</if>"  
  30.             + "</script>")  
  31.     public List<DemandComment> listDemandComment(@Param("demandId") Long demandId, @Param("startNo") Integer pageNo, @Param("pageSize") Integer pageSize);  

  1. }  


原文地址:http://blog.csdn.net/qq_32786873/article/details/78297551