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


    @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语法一样书写

    @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);
    

    项目实例

    @Select("<script>"
                +"select * from mi_taobao where 1=1"
                +"<if test='status != null'>"
                +"and status = #{status}"
                +"</if>"
                +"</script>")
        public List<Taobao> getTaobao(@Param("status") Integer status);
    

    转载自:https://blog.csdn.net/qq_32786873/article/details/78297551

  • 相关阅读:
    互动留言赠书:《Oracle高性能系统实战大全》
    超融合硬件损坏导致Oracle RAC异常恢复实录
    架构师不得不了解的硬件知识
    加班做的可视化被老板嫌弃,是因为你不会用这些数据工具
    iOS开发之七:常用控件--UISlider、UISegmentedControl、UIPageControl的使用
    iOS开发之六:常用控件--UIImageView的使用
    iOS开发之五:常用控件--UITextField的使用
    Objective-C实现常用的4种排序算法
    C语言实现4种常用排序
    面试常用的4种数组排序
  • 原文地址:https://www.cnblogs.com/DiZhang/p/12544806.html
Copyright © 2020-2023  润新知