Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apijson 支持sql语句模版, 传递参数, 执行sql语句, 支持redis cluster、sentinel、single模式 #32

Merged
merged 8 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
907 changes: 907 additions & 0 deletions APIJSON-Java-Server/APIJSONDemo-MultiDataSource-Redis/README.md

Large diffs are not rendered by default.

196 changes: 196 additions & 0 deletions APIJSON-Java-Server/APIJSONDemo-MultiDataSource-Redis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.13</version>
<!-- <relativePath>./pom.xml</relativePath>-->
</parent>
<groupId>apijson.demo</groupId>
<artifactId>apijsondemo-multidatasource-redis</artifactId>
<version>6.0.0</version>

<name>apijsondemo-multidatasource-redis</name>
<description>Demo project for testing APIJSON server based on SpringBoot</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<commons-lang3.version>3.12.0</commons-lang3.version>
<druid.version>1.1.16</druid.version>
<mybatisplus.version>3.5.1</mybatisplus.version>
<mybatis-plus-support.version>2.3.3</mybatis-plus-support.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons.configuration.version>1.10</commons.configuration.version>
<guava.version>30.1.1-jre</guava.version>
<fastjson.version>1.2.72</fastjson.version>
<hutool.version>4.1.1</hutool.version>
<lombok.version>1.18.4</lombok.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons.io.version>2.5</commons.io.version>
<commons.codec.version>1.10</commons.codec.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons.configuration.version>1.10</commons.configuration.version>
<apijson.version>6.0.0</apijson.version>
<mysql.version>8.0.31</mysql.version>
<spring-context-support.version>5.3.18</spring-context-support.version>
<spring-boot-configuration-processor.version>2.6.6</spring-boot-configuration-processor.version>
<dynamic-datasource-spring-boot-starter.version>3.5.2</dynamic-datasource-spring-boot-starter.version>
<jedis.version>3.7.1</jedis.version>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- 需要的 APIJSON 相关依赖 -->
<dependency>
<groupId>com.github.Tencent</groupId>
<artifactId>APIJSON</artifactId>
<version>${apijson.version}</version>
</dependency>
<dependency>
<groupId>com.github.APIJSON</groupId>
<artifactId>apijson-framework</artifactId>
<version>${apijson.version}</version>
</dependency>

<!-- 需要用的数据库 JDBC 驱动 -->

<!-- Oracle, SQLServer 等其它数据库的 JDBC 驱动,可以在这里加上 Maven 依赖或 libs 目录放 Jar 包并依赖 -->

<!-- 需要用的 SpringBoot 框架,1.4.0 以上 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-context-support.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot-configuration-processor.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${dynamic-datasource-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
<exclusions>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-support</artifactId>
<version>${mybatis-plus-support.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons.codec.version}</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>${commons.configuration.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>apijson.demo.DemoApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<!-- APIJSON 必须用到的托管平台 -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package apijson.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataBaseConfig {
private String primary;

@Value("${spring.datasource.dynamic.primary}")
public void setPrimary(String primary) {
this.primary = primary;
}

public String getPrimary() {
return primary;
}

public static DataBaseConfig getInstence() {
return SpringContextUtils.getBean(DataBaseConfig.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package apijson.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataBaseUrlConfig {
private String primary;

@Value("${spring.datasource.dynamic.primary}")
public void setPrimary(String primary) {
this.primary = primary;
}

public String getPrimary() {
return primary;
}

public static DataBaseUrlConfig getInstence() {
return SpringContextUtils.getBean(DataBaseUrlConfig.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package apijson.demo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.extern.log4j.Log4j2;

@Log4j2
public class DataBaseUtil {

/**
* 根据url获取库名
* @param url
* @return
*/
public static String getLibname(String url) {
Pattern p = Pattern.compile("jdbc:(?<db>\\w+):.*((//)|@)(?<host>.+):(?<port>\\d+)(/|(;DatabaseName=)|:)(?<dbName>\\w+)\\??.*");
Matcher m = p.matcher(url);
if(m.find()) {
return m.group("dbName");
}
return null;
}

/***
* primary: master
* strict: false
* @param datasource: 匹配不成功, 自动匹配默认数据库
* @return
*/
public static javax.sql.DataSource getDataSource(String datasource) {
try {
return DynamicDataSource.getDetail(datasource).getDataSource(); // 数据源
} catch (Exception e) {
throw new IllegalArgumentException("动态数据源配置错误 " + datasource);
}
}

public static String getDruidUrl(String datasource) {
return DynamicDataSource.getDetail(datasource).getUrl(); // 数据库连接url
}

public static String getDruidSchema(String datasource) {
return getLibname(DynamicDataSource.getDetail(datasource).getUrl()); // 数据库名;
}

public static String getDruidDBAccount(String datasource) {
return DynamicDataSource.getDetail(datasource).getDbAccount(); // 数据库用户名
}

public static String getDruidDBPassword(String datasource) {
return DynamicDataSource.getDetail(datasource).getDbPassword(); // 数据库密码
}
}
Loading