mybatis @Select注解中如何拼写动态sql

package cn.erongcai.hrplatform.dao.demand;
 
import java.util.List;
 
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
 
import com.baomidou.mybatisplus.mapper.BaseMapper;
 
import cn.erongcai.hrplatform.model.demand.DemandComment;
 
@Mapper
public interface DemandCommentMapper extends BaseMapper<DemandComment>{
	
	@Select("SELECT "
			+ "a.id as 'id',a.create_date as 'createDate',a.content as 'content',"
			+ "a.parent_id as 'parentId',a.first_comment_id as 'firstCommentId',"
			+ "b.id as 'fromUser.id',b.realname as 'fromUser.realname',b.avatar as 'fromUser.avatar',"
			+ "c.id as 'toUser.id',c.realname as 'toUser.realname',c.avatar as 'toUser.avatar' "
			+ "FROM t_demand_comment a "
			+ "LEFT JOIN t_user b ON b.id = a.from_uid "
			+ "LEFT JOIN t_user c ON c.id = a.to_uid "
			+ "WHERE a.demand_id = #{demandId} "
			+ "ORDER BY a.create_date ASC"
			+ "LIMIT #{startNo},#{pageSize}")
	public List<DemandComment> listDemandComment(@Param("demandId") Long demandId, @Param("startNo") Integer pageNo, @Param("pageSize") Integer pageSize);
}

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

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

<when test='startNo!=null and pageSize != null '>
  LIMIT #{startNo},#{pageSize}
</when>

如果是用@Select 这种该如何做呢?

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

package cn.erongcai.hrplatform.dao.demand;
 
import java.util.List;
 
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
 
import com.baomidou.mybatisplus.mapper.BaseMapper;
 
import cn.erongcai.hrplatform.model.demand.DemandComment;
 
@Mapper
public interface DemandCommentMapper extends BaseMapper<DemandComment>{
	
	@Select("<script>"
			+ "SELECT "
			+ "a.id as 'id',a.create_date as 'createDate',a.content as 'content',"
			+ "a.parent_id as 'parentId',a.first_comment_id as 'firstCommentId',"
			+ "b.id as 'fromUser.id',b.realname as 'fromUser.realname',b.avatar as 'fromUser.avatar',"
			+ "c.id as 'toUser.id',c.realname as 'toUser.realname',c.avatar as 'toUser.avatar' "
			+ "FROM t_demand_comment a "
			+ "LEFT JOIN t_user b ON b.id = a.from_uid "
			+ "LEFT JOIN t_user c ON c.id = a.to_uid "
			+ "WHERE a.demand_id = #{demandId} "
			+ "ORDER BY a.create_date ASC "
			+ "<if test='startNo!=null and pageSize != null '>"
			+ "LIMIT #{startNo},#{pageSize}"
			+ "</if>"
			+ "</script>")
	public List<DemandComment> listDemandComment(@Param("demandId") Long demandId, @Param("startNo") Integer pageNo, @Param("pageSize") Integer pageSize);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值