欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis實現(xiàn)動態(tài)SQL編寫詳細代碼示例

 更新時間:2023年05月20日 10:16:06   作者:全子兄  
這篇文章主要為大家詳細介紹了Mybatis中動態(tài)SQL的編寫使用,動態(tài)SQL技術是一種根據特定條件動態(tài)拼裝SQL語句的功能,它存在的意義是為了解決拼接SQL語句字符串時的痛點問題,感興趣想要詳細了解可以參考下文

介紹 

注解作用
@SelectProvider動態(tài)查詢SQL
@InsertProvider動態(tài)新增SQL
@UpdateProvider動態(tài)更新SQL
@DeleteProvider動態(tài)刪除SQL

@Select 與 @SelectProvider 只是在定義注解的方式上有所不同, 一個是靜態(tài)SQL,一個是動態(tài)SQL。

@SelectProvider 是 MyBatis 中的一個注解,用于指定一個類或者類的某個方法提供 SQL 查詢語句。該注解常用于動態(tài) SQL 的場景,例如根據不同的參數(shù)生成不同的查詢語句。 使用 @SelectProvider 注解的方式可以讓 MyBatis 在運行時根據注解指定的類或方法來生成對應的 SQL 查詢語句,從而實現(xiàn)動態(tài) SQL 功能。注解的語法如下:

@SelectProvider(type = XxxProvider.class, method = "xxxMethod")

其中,type 屬性表示提供 SQL 查詢語句的類,method 屬性表示類中提供查詢語句的方法。在查詢時,MyBatis 會調用指定類中的指定方法來生成 SQL 查詢語句,然后執(zhí)行該查詢語句并返回結果。 需要注意的是,提供 SQL 查詢語句的類需要實現(xiàn) org.apache.ibatis.builder.annotation.ProviderMethodResolver 接口,該接口中定義了查找提供 SQL 查詢的方法的邏輯。同時,提供查詢語句的方法需要返回一個字符串類型的 SQL 查詢語句。 使用 @SelectProvider 注解可以讓 MyBatis 的查詢語句更加靈活,適用于各種復雜的查詢場景。

1、@SelectProvider使用

1.TestMapper.java

public interface TestMapper {
    /**
     * 根據性別獲取老師信息
     *
     * @param sex
     * @return
     */
    @SelectProvider(type = Test.class, method = "list")
    List<Teacher> list(Integer sex);
}
 

2.Test.java

public class Test {
    /**
     * 根據性別獲取老師信息
     *
     * @param sex
     * @return
     */
    public String list(Integer sex) {
        return new SQL() {
            {
                SELECT("*");
                FROM("teacher");
                if (null != sex) {
                    WHERE(" 1 = 1 sex = " + sex);
                } else {
                    WHERE(" 1 = 1 ");
                }
                ORDER_BY("create_time desc");
            }
        }.toString();
    }
}

2、模擬Mybatis Plus

可以使用 SelectProvider 寫一個適用于所有表的查詢的方法。

1.BasicWrapper.java

import java.util.LinkedList;
import java.util.List;
public class BasicWrapper<T> {
    public String last;
    public Class<T> bean;
    public String table;
    public String[] field;
    public List<String> condition = new LinkedList<>();
    public String orderBy;
}

2.QueryWrapper.java

import java.util.Arrays;
import org.apache.ibatis.jdbc.SQL;
import cn.hutool.core.collection.CollectionUtil;
public class QueryWrapper<T> extends BasicWrapper {
    public QueryWrapper() {
    }
    public QueryWrapper(String table, String... field) {
        super.bean = null;
        super.table = table;
        super.field = (null == field || field.length == 0) ? new String[]{"*"} : field;
    }
    public String list(QueryWrapper wrapper) {
        Query query = wrapper.build();
        return new SQL() {
            {
                SELECT(query.getFields());
                FROM(query.getTable());
                WHERE(" 1 = 1 " + query.getCondition());
                ORDER_BY(query.getOrderBy());
            }
        }.toString();
    }
    public Query build() {
        Query query = new Query();
        query.setTable(super.table);
        query.setFields((null == super.field || super.field.length == 0) ? "*" : String.join(", ", Arrays.asList(super.field)));
        String condition = CollectionUtil.isEmpty(super.condition) ? " 1 = 1 " : String.join(" ", super.condition);
        String last = null == super.last ? "" : super.last;
        query.setCondition(condition + " " + last);
        return query;
    }
    public QueryWrapper orderBy(String sql) {
        super.orderBy = sql;
        return this;
    }
    public QueryWrapper orderBy(boolean condition, String sql) {
        if (condition) {
            super.orderBy = sql;
        }
        return this;
    }
    public QueryWrapper apply(String sql) {
        super.condition.add(" and (" + sql + ") ");
        return this;
    }
    public QueryWrapper apply(boolean condition, String sql) {
        if (condition) {
            super.condition.add(" and (" + sql + ") ");
        }
        return this;
    }
    public QueryWrapper eq(String filed, Object value) {
        super.condition.add(" and " + filed + " = " + value);
        return this;
    }
    public QueryWrapper eq(boolean condition, String filed, Object value) {
        if (condition) {
            if (value instanceof String) {
                super.condition.add(" and " + filed + " = '" + value + "'");
            } else {
                super.condition.add(" and " + filed + " = " + value);
            }
        }
        return this;
    }
    public QueryWrapper last(String value) {
        super.last = " " + value;
        return this;
    }
    public QueryWrapper last(boolean condition, String value) {
        if (condition) {
            super.last = " " + value;
        }
        return this;
    }
    public QueryWrapper like(String filed, Object value) {
        super.condition.add(" and " + filed + " like %" + value + "%");
        return this;
    }
    public QueryWrapper likeLeft(String filed, Object value) {
        super.condition.add(" and " + filed + " like %" + value + "%");
        return this;
    }
    public QueryWrapper likeRight(String filed, Object value) {
        super.condition.add(" and " + filed + " like %" + value + "%");
        return this;
    }
    public QueryWrapper like(boolean condition, String filed, Object value) {
        if (condition) {
            super.condition.add(" and " + filed + " like %" + value + "%");
        }
        return this;
    }
    public QueryWrapper likeLeft(boolean condition, String filed, Object value) {
        if (condition) {
            super.condition.add(" and " + filed + " like %" + value + "%");
        }
        return this;
    }
    public QueryWrapper likeRight(boolean condition, String filed, Object value) {
        if (condition) {
            super.condition.add(" and " + filed + " like %" + value + "%");
        }
        return this;
    }
}
 

3.Query.java

import lombok.Data;
@Data
public class Query {
    private String table;
    private String fields;
    private String orderBy;
    private String condition;
}
 

4.Test.java

 
import org.apache.ibatis.jdbc.SQL;
public class Test {
	public static void main(String agrs[]) {
		    QueryWrapper<Teacher> wrapper = new QueryWrapper<>("teacher");
            wrapper.eq("sex", 1);
            wrapper.eq(StringUtils.isNotBlank(name), "name", name);
            wrapper.orderBy("create_time desc");
            List<Teacher> list = processMapper.list(wrapper);
	}
}

到此這篇關于Mybatis實現(xiàn)動態(tài)SQL編寫詳細代碼示例的文章就介紹到這了,更多相關Mybatis動態(tài)SQL內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論