mybatis 里面怎么按照条件查询和查询全部数据 这两个在同一接口里面

新手上路,请多包涵

mybatis 里面怎么按照条件查询和查询全部数据 这两个在同一接口里面

阅读 2.3k
3 个回答

写为动态sql即可。
例如:传id就是查询单条,不传就是全部

    <select id="selectTouristGenderYearhList" resultType="com.zdxf.domain.TouristGenderDO">
        select * from pw_tourist_gender_year
        <where>
            1=1
            <if test="id != null">
                and id = #{id}
            </if>
        </where>
    </select>

动态sql,像最常见的if,choose when都行

    <select id="selectAllByUsernameAndSearch" resultMap="BaseResultMap">
        SELECT
            <include refid="columns" />
        FROM as_database
        <where>
            username = #{username}
            <if test="search != null and search != ''">
                AND (`name` LIKE CONCAT('%', #{search}, '#') OR `description` LIKE CONCAT('%', #{search}, '%'))
            </if>
        </where>
        <if test="order != null and order != ''">
            ORDER BY #{order}
            <choose>
                <when test="directing == 'DESC'">
                    DESC
                </when>
                <otherwise>
                    ASC
                </otherwise>
            </choose>
        </if>
        LIMIT #{offset},#{limit}
    </select>

参考MyBatis动态SQL。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题