@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);
So the whole sentence is written dead, if I want to according to PageNo and pagesize is empty to determine whether the need for paging, how to do?
If you use XML to configure it, you can use
<when test=‘startNo!=null and pageSize != null ‘>
LIMIT #{startNo},#{pageSize}
</when>
What if this is done with @select?
Method: Surround with a script tag and write as XML syntax
@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);
Project examples
@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);
There is also a problem here is error: caused By:org.apache.ibatis.reflection.ReflectionException:There is no getter for property named ' Status ' in ' Class Java.lang.Interger '
Cause: The problem here is that the parameters defined in the DAO method are inconsistent with the attributes defined in the entity.
Solution: The DAO layer plus @Param ("userId") annotations can be (add @Param ("status") in the instance)
Public list<dictitem> Selectkeybyuserid (@Param ("userid") long userId);
MyBatis How to spell dynamic SQL in @Select annotations