@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的更多相关文章

  1. mybatis的注解开发之三种动态sql

    脚本sql XML配置方式的动态SQL我就不讲了,有兴趣可以自己了解,下面是用<script>的方式把它照搬过来,用注解来实现.适用于xml配置转换到注解配置 @Select(" ...

  2. MyBatis学习(三)、动态SQL语句

    三.动态SQL语句 有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Orac ...

  3. MyBatis基础入门《十七》动态SQL

    MyBatis基础入门<十七>动态SQL 描述: >> 完成多条件查询等逻辑实现 >> 用于实现动态SQL的元素主要有: > if > trim > ...

  4. SpringBoot使用Mybatis注解开发教程-分页-动态sql

    代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ...

  5. 零基础学习java------36---------xml,MyBatis,入门程序,CURD练习(#{}和${}区别,模糊查询,添加本地约束文件) 全局配置文件中常用属性 动态Sql(掌握)

    一. xml  1. 文档的声明 2. 文档的约束,规定了当前文件中有的标签(属性),并且规定了标签层级关系 其叫html文档而言,语法要求更严格,标签成对出现(不是的话会报错) 3. 作用:数据格式 ...

  6. Mybatis中的update动态SQL语句

    Mybatis中的CRUD操作(增删改查)中,简单的SQL操作比较直观,如查找操作: <select id="findBySrcId" resultMap="ent ...

  7. 一、MyBatis基本使用,包括xml方式、注解方式、及动态SQL

    一.简介 发展历史:MyBatis 的前 身是 iBATIS.最初侧重于 密码软件的开发 , 后来发展成为一款基于 Java 的持久层框架. 定      位:MyBatis 是一款优秀的支持自定义 ...

  8. Java数据持久层框架 MyBatis之API学习七(动态 SQL详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  9. MyBatis知多少(25)动态SQL

    使用动态查询是MyBatis一个非常强大的功能.有时你已经改变WHERE子句条件的基础上你的参数对象的状态.在这种情况下的MyBatis提供了一组可以映射语句中使用,以提高SQL语句的重用性和灵活性的 ...

随机推荐

  1. NOI.AC NOIP模拟赛 第六场 游记

    NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...

  2. Ubuntu下实现软路由(转)

    参考:http://www.openwrt.pro/post-292.html 个人看法: 1.实现路由在Linux下必须要用到iptables进行转发,这才是路由核心. 2.我觉得对于Linux来说 ...

  3. 孕龙逻辑分析仪 ZeroPlus Logic Analyzer

    Voltage Translation for Analog to Digital Interface ADC http://openschemes.com/2010/03/23/zeroplus-l ...

  4. linux socket编程示例

    #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include &l ...

  5. On premise TFS Git OAuth clone failed on agent

    利用TFS2015的跨平台生成代理,在mac osx 上生成xamarin.ios项目,agent费劲九牛二虎之力搞定了(参见http://www.cnblogs.com/zjoch/p/581101 ...

  6. Android 之窗口小部件高级篇--App Widget 之 RemoteViews

    Android 之窗口小部件高级篇--App Widget 之 RemoteViews 在之前的一篇博文(Android 之窗口小部件详解--App Widget)中,已经介绍了App Widget的 ...

  7. arcengine Annotation研究的一些学习资料(转)FeatureWeight

    转自chanyinhelv原文Annotation研究的一些学习资料 下面是我最近对Annotation研究的一些学习资料,收集于此,供大家学习之用. 一.Annotation要素类介绍 在GeoDa ...

  8. .NET:Assembly.CodeBase vs. Assembly.Location

    The CodeBase is a URL to the place where the file was found, while the Location is the path from whe ...

  9. 发展受阻第一至四季/全集Arrested迅雷下载

    发展受阻 第一至四季 Arrested Development 1-4 (2013)本季看点:<发展受阻>讲述了一个很有钱的Bluth家庭的故事,主要聚焦家族里的各种古怪事和相互算计带来的 ...

  10. Android之Activity与fragment完整生命周期图

    转自:https://github.com/xxv/android-lifecycle