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);
在这里还碰到一个问题就是报错: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的更多相关文章
- mybatis的注解开发之三种动态sql
		脚本sql XML配置方式的动态SQL我就不讲了,有兴趣可以自己了解,下面是用<script>的方式把它照搬过来,用注解来实现.适用于xml配置转换到注解配置 @Select(" ... 
- MyBatis学习(三)、动态SQL语句
		三.动态SQL语句 有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Orac ... 
- MyBatis基础入门《十七》动态SQL
		MyBatis基础入门<十七>动态SQL 描述: >> 完成多条件查询等逻辑实现 >> 用于实现动态SQL的元素主要有: > if > trim > ... 
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
		代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ... 
- 零基础学习java------36---------xml,MyBatis,入门程序,CURD练习(#{}和${}区别,模糊查询,添加本地约束文件) 全局配置文件中常用属性   动态Sql(掌握)
		一. xml 1. 文档的声明 2. 文档的约束,规定了当前文件中有的标签(属性),并且规定了标签层级关系 其叫html文档而言,语法要求更严格,标签成对出现(不是的话会报错) 3. 作用:数据格式 ... 
- Mybatis中的update动态SQL语句
		Mybatis中的CRUD操作(增删改查)中,简单的SQL操作比较直观,如查找操作: <select id="findBySrcId" resultMap="ent ... 
- 一、MyBatis基本使用,包括xml方式、注解方式、及动态SQL
		一.简介 发展历史:MyBatis 的前 身是 iBATIS.最初侧重于 密码软件的开发 , 后来发展成为一款基于 Java 的持久层框架. 定 位:MyBatis 是一款优秀的支持自定义 ... 
- Java数据持久层框架 MyBatis之API学习七(动态 SQL详解)
		对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ... 
- MyBatis知多少(25)动态SQL
		使用动态查询是MyBatis一个非常强大的功能.有时你已经改变WHERE子句条件的基础上你的参数对象的状态.在这种情况下的MyBatis提供了一组可以映射语句中使用,以提高SQL语句的重用性和灵活性的 ... 
随机推荐
- Bzoj2694/Bzoj4659:莫比乌斯反演
			Bzoj2694/Bzoj4659:莫比乌斯反演 先上题面:首先看到这数据范围显然是反演了,然而第三个限制条件十分不可做.于是我们暂且无视他,大不了补集转化算完再减是吧. 于是我们有:这里我们定义:于 ... 
- Nginx增加模块
			http://blog.csdn.net/loyachen/article/details/50902667 
- 【Go命令教程】13. go tool cgo
			cgo 也是一个 Go 语言自带的特殊工具.一般情况下,我们使用命令 go tool cgo 来运行它.这个工具可以使我们创建能够调用 C 语言代码的 Go 语言源码文件.这使得我们可以使用 Go 语 ... 
- delphi  SPCOMM的一些用法注意
			使用串口SPCOMM接收数据的时候0x11和0x13无法接受,从时间间隔上看来可以接收,但是无法显示.网上查错误得: --------------------------------------- ... 
- Promise,Generator(生成器),async(异步)函数
			Promise 是什么 Promise是异步编程的一种解决方案.Promise对象表示了异步操作的最终状态(完成或失败)和返回的结果. 其实我们在jQuery的ajax中已经见识了部分Promise的 ... 
- Windows Application Data拒绝访问打开方法?
			在Windows7操作系统,打开 Application Data等文件夹时,弹出位置不可用的警告窗口,提示拒绝访问.下面提供简单的解决方法,希望有用. 工具/原料 计算机. Windows7操作系统 ... 
- linux搭建C开发环境
			目前决大多 数的Linux用户对Linux的了解还处于比较低级的层次,他们可能会几条命令.会配几种服务.会用rpm来安装软件.会操作KDE/Gnome界机等等,但是当他们遇到一些需要编译安装的软件时, ... 
- DataGridView 在 WinForms中应用不同的单元格式
			/// <summary> /// Set the cell background colour to make the ups and downs more visible. /// & ... 
- WordPress主题开发:get_term_by和get_term_link
			学习目的: 某一个分类的名称.别名.和id都可以到后台自己去找,但这样找比较麻烦还容易看错,wordpress提供了下面两个函数get_term_by和get_term_link,只要提供别名.名称或 ... 
- String Matching(poj1580)
			/*String Matching Description It's easy to tell if two words are identical - just check the letters. ... 
