MyBatis How to spell dynamic SQL in @Select annotations

Source: Internet
Author: User
Tags script tag


 
@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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.