Spring JdbcTemplate实现自定义动态sql拼接功能
需求描述:
sql 需要能满足支持动态拼接,包含 查询字段、查询表、关联表、查询条件、关联表的查询条件、排序、分组、去重等
实现步骤:
1,创建表及导入测试数据
CREATE TABLE YES_DEV.T11 ( ID BINARY_BIGINT NOT NULL, NAME VARCHAR(10), XX BINARY_BIGINT, CONSTRAINT _PK_SYS_25_63 PRIMARY KEY (ID) ); CREATE TABLE YES_DEV.T111 ( ID BINARY_INTEGER NOT NULL, NAME NUMBER ); INSERT INTO YES_DEV.T11 (ID,NAME,XX) VALUES (11,'123',11), (9,'9',9), (8,'8',8), (7,'7',7), (6,'6',6), (5,'5',5), (4,'4',4), (3,'3',3), (2,'2',2), (1,'1',1); INSERT INTO YES_DEV.T111 (ID,NAME) VALUES (1,123);
2,创建项目并引入 pom依赖
4.0.0
org.example
testMybatis
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.5.8
15
15
UTF-8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
com.huawei.gauss
com.huawei.gauss.jdbc.ZenithDriver
1.2.1
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
junit
junit
test
com.baomidou
mybatis-plus-boot-starter
3.4.2
commons-lang
commons-lang
2.6
commons-collections
commons-collections
3.2.2
com.google.guava
guava
30.1-jre
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
3,编写 application.properties文件
spring.datasource.url=jdbc:zenith:@xxxx:xxx spring.datasource.username=xxx spring.datasource.password=xxxx spring.datasource.driver-class-name=com.huawei.gauss.jdbc.inner.GaussDriver mybatis.mapper-locations=classpath:mapper/*.xml org.apache.springframework.jdbc.core.JdbcTemplate = debug
4,核心类之 Column
package com.example.dao.sql;
import com.example.utils.SqlUtils;
import lombok.Getter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@Getter
public class Column {
private final String name;
private final List tableAliases = new ArrayList();
private Column(String name, List tableAliases) {
this.name = name;
if (CollectionUtils.isNotEmpty(tableAliases)) {
this.tableAliases.addAll(tableAliases);
}
}
public static Column of(String name, String... tableAliases) {
if (ArrayUtils.isNotEmpty(tableAliases)) {
tableAliases = Arrays.stream(tableAliases).filter(Objects::nonNull).toArray(String[]::new);
}
if (ArrayUtils.isEmpty(tableAliases)) {
String tableAlias = StringUtils.substringBefore(name, ".").trim();
if (SqlUtils.isValidAlias(tableAlias)) {
tableAliases = new String[] {tableAlias};
}
}
return new Column(name, ArrayUtils.isEmpty(tableAliases) ? null : Arrays.asList(tableAliases));
}
public String getTableAlias() {
return tableAliases.isEmpty() ? null : tableAliases.get(0);
}
}
5,核心类之 Table
package com.example.dao.sql;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.example.utils.SqlUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Table {
private String joinType;
private boolean weak;
private boolean optimizable = true;
private Map> joinPredicateConditions = new ArrayList();
private final List filterConditions = new ArrayList();
private final List> getJoinPredicateConditions() {
return joinPredicateConditions;
}
public List getFilterConditions() {
return filterConditions;
}
public List> getParameterTempData() {
return parameterTempData;
}
public void setParameterTempData(Map
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
