On MyBatis Dynamic If statement query error problem and solution

Source: Internet
Author: User

Similar to an error in MyBatis

There is no getter for property named ' Username ' in ' class java.lang.String '

There are 2 solutions without removing the IF label
Workaround 1
In the. xml file, replace the username with _parameter, which applies to a single parameter, as follows

    <select id= "Selectuserbyname" parametertype= "String" resultmap= "user" >
        select
        <include refid= " Querycolumns "/> from
        z_user,z_basic
        <where>
            <if test=" Username! = null ">
                username Like CONCAT ('% ', #{username}, '% ')
            </if>
        </where>
    </select>
    <select id= "Selectuserbyname" parametertype= "String" resultmap= "user" >
        select
        <include refid= " Querycolumns "/> from
        z_user,z_basic
        <where>
            <if test=" _parameter! = null ">
                username Like CONCAT ('% ', #{username}, '% ')
            </if>
        </where>
    </select>
    <select id= " Selectalluser "resultmap=" user ">
        select
        <include refid=" Querycolumns "/>
        , Text,password,z _basic.id from
        z_user,z_basic
        where
        z_user.id=z_basic.z_basic.id
    </select>

Workaround 2
DECLARE @param ("username") in the Mapper interface, as follows

List<user> Selectuserbyname (@Param ("username") String username);

I hope the above two methods are useful to everyone

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.