模糊查詢(僅針對后端)

只是后臺完成 沒有跟前臺連接

需求 :可通過姓名、電話、郵件來模糊查詢

1.mapper.xml

注意 對象"com.catgo.admin.entity.User" 這個包名是Java文件夾往下

<!--  模糊查詢  -->
//parameterType:參數(shù)類型 因為在這兒傳的是一個User對象
//resultType:返回的結(jié)果類型 最后通過一個對象里的字段 返回的也是一個對象
    <select id="selectLike" parameterType="com.catgo.admin.entity.User" resultType="com.catgo.admin.entity.User">
        select * from tb_user
        <include refid="where_sql"></include>
    </select>//把查詢條件擴(kuò)展出來
    <sql id="where_sql">
        <where>
            <if test="username!=null">
                username like concat('%',#{username},'%')
            </if>
            <if test="phone!=null">
                and phone like concat('%',#{phone},'%')
            </if>
            <if test="email!=null">
                and email like concat('%',#{email},'%')
            </if>
        </where>
    </sql>
</mapper>
注意符號
  // `#{username}` 也可用兩個單引號引起來  是最左上角的符號
  //不是‘’ 不是回車旁邊的符號 切記 加不加看情況 有時加了還報錯
  //加``區(qū)別于數(shù)據(jù)庫里的關(guān)鍵字 相當(dāng)于是我自己寫的一個字段

2.mapper的接口

mapper的接口

3.mapper測試類

3.1 由于數(shù)據(jù)庫里的字段設(shè)置了不能為空

數(shù)據(jù)庫里的字段
mapper測試類

4.service 目前能想到的業(yè)務(wù)只有判斷此用戶是否存在

service接口

4.1serviceImpl

serviceImpl

5.controller

controller

6.PostMan測試

6.1 由于寫法寫的json數(shù)據(jù)

PostMan測試

7.(額外)關(guān)于Result部分 寫在公共類的結(jié)果返回正確與否的通用方法

公共類的Result部分
package com.catgo.common.core;

import lombok.Data;

@Data
public class Result<T> {
    private String code;
    private String message;
    private T data;

    /**
     * 構(gòu)造函數(shù)
     */
    private Result(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private Result(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public static Result fail(String message) {
        return new Result("1111", message);
    }

    public static Result ok(String message) {
        return new Result("0000", message);
    }

    public static Result ok(Object data) {
        return new Result("0000", "success", data);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容