Mybatis實現(xiàn)動態(tài)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 的場景,例如根據(jù)不同的參數(shù)生成不同的查詢語句。 使用 @SelectProvider
注解的方式可以讓 MyBatis 在運行時根據(jù)注解指定的類或方法來生成對應(yīng)的 SQL 查詢語句,從而實現(xiàn)動態(tài) SQL 功能。注解的語法如下:
@SelectProvider(type = XxxProvider.class, method = "xxxMethod")
其中,type
屬性表示提供 SQL 查詢語句的類,method
屬性表示類中提供查詢語句的方法。在查詢時,MyBatis 會調(diào)用指定類中的指定方法來生成 SQL 查詢語句,然后執(zhí)行該查詢語句并返回結(jié)果。 需要注意的是,提供 SQL 查詢語句的類需要實現(xiàn) org.apache.ibatis.builder.annotation.ProviderMethodResolver
接口,該接口中定義了查找提供 SQL 查詢的方法的邏輯。同時,提供查詢語句的方法需要返回一個字符串類型的 SQL 查詢語句。 使用 @SelectProvider
注解可以讓 MyBatis 的查詢語句更加靈活,適用于各種復(fù)雜的查詢場景。
1、@SelectProvider使用
1.TestMapper.java
public interface TestMapper { /** * 根據(jù)性別獲取老師信息 * * @param sex * @return */ @SelectProvider(type = Test.class, method = "list") List<Teacher> list(Integer sex); }
2.Test.java
public class Test { /** * 根據(jù)性別獲取老師信息 * * @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); } }
到此這篇關(guān)于Mybatis實現(xiàn)動態(tài)SQL編寫詳細代碼示例的文章就介紹到這了,更多相關(guān)Mybatis動態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sentinel Dashboard限流規(guī)則保存方式
這篇文章主要介紹了Sentinel Dashboard限流規(guī)則保存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06spring boot RestTemplate 發(fā)送get請求的踩坑及解決
這篇文章主要介紹了spring boot RestTemplate 發(fā)送get請求的踩坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08SpringBoot實現(xiàn)根據(jù)手機號獲取歸屬地
這篇文章主要為大家詳細介紹了SpringBoot如何實現(xiàn)根據(jù)手機號獲取歸屬地,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12java集合中HashSet LinkedHashSet TreeSet三者異同面試精講
這篇文章主要為大家介紹了java集合中HashSet LinkedHashSet TreeSet三者異同面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10JAVA發(fā)送http get/post請求,調(diào)用http接口、方法詳解
這篇文章主要介紹了Java發(fā)送http get/post請求調(diào)用接口/方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04