码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-12-04 13:23:26      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:HERE   技术   use   img   order by   sql   item   uid   语句   

@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);
技术图片

  在这里还碰到一个问题就是报错:Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘status‘ in ‘class java.lang.Interger‘

  出现原因:这里出现的问题是在DAO方法中定义的参数 与 实体中定义的属性不一致 导致的。

  解决方案:dao层加@Param("userId")注解即可(实例中就是加上@Param("status"))

  public List<DictItem> selectKeyByUserId(@Param("userId") long userId);

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

标签:HERE   技术   use   img   order by   sql   item   uid   语句   

原文地址:https://www.cnblogs.com/zhuyeshen/p/11981993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!