Java教程

JAVA开发数据源系统经验汇总

本文主要是介绍JAVA开发数据源系统经验汇总,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

    • 前言
    • 1.驼峰转换
    • 2.获取数据库表列表
    • 3.本地数据库hutool的db工具类
    • 4.主流数据库枚举工具
    • 5.去掉所有的\t \n
    • 6.根据“.”通配多级目录数据结构

前言

最近在做数据源相关的系统,持续更新系统中所用的相关技术问题。

1.驼峰转换

原地址:https://blog.csdn.net/kanglong129/article/details/98218871

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>21.0</version>
</dependency>
import org.junit.Test;
 
import com.google.common.base.CaseFormat;
 
public class GuavaTester {
 
    @Test
    public void test() {
        System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));//testData
        System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));//testData
        System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));//TestData
 
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));//testdata
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "TestData"));//test_data
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));//test-data
 
    }
}

2.获取数据库表列表

需求:查询该数据源的列表及描述
首先通过hutools的数据源工具类,获取到Db对象

Connection connection = db.getConnection();
DatabaseMetaData metaData = connection.getMetadata();
ResultSet rs = metaData.getTables(null,null,null,new String[]{"TABLE"})
List<Map<String,Object>> list = new ArrayList<>();
while(rs.next()){
	Map<String,Object> map = new HashMap<>();
	map.put("tableName",rs.getString("TABLE_NAME"));
	map.put("tableDesc",rs.getString("REMARKS"));
	list.add(map);
}
// 省略关闭conn和rs代码

3.本地数据库hutool的db工具类

import cn.hutool.db.Db;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class LocalDataSourceUtils {

    @Value("${spring.datasource.url}")
    public String url;
    @Value("${spring.datasource.username}")
    public String username;
    @Value("${spring.datasource.password}")
    public String password;
    @Value("${spring.datasource.driver-class-name}")
    public String driverClassName;

    private static Db db;


    /**
     * 获取数据源
     * @return
     * @throws Exception
     */
    public Db getDb() throws Exception{
        if(null == db || db.getConnection().isClosed()){
            DruidDataSource ds = new DruidDataSource();
            ds.setUsername(username);
            ds.setPassword(password);
            ds.setDriverClassName(driverClassName);
            ds.setUrl(url);
            Db tmDb = Db.use(ds,driverClassName);
            db = tmDb;
        }
        return db;
    }

}

4.主流数据库枚举工具

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum DataSourceDbType {

    MYSQL("mysql","com.mysql.jdbc.Driver","Mysql_5~6","jdbc:mysql://IP:端口/数据库名?useUnicode=true&characterEncoding=utf-8"),
    MYSQL_8("mysql-new","com.mysql.cj.jdbc.Driver","Mysql_8+","jdbc:mysql://IP:端口/数据库名?useUnicode=true&characterEncoding=utf-8"),
    ORACLE("oracle","oracle.jdbc.driver.OracleDriver","Oracle","jdbc:oracle:thin:@IP:端口:实例名"),
    ORACLE_NEW("oracle-new","oracle.jdbc.OracleDriver","Oracle_new","jdbc:oracle:thin:@IP:端口:实例名"),
    POSTGRE("postgre","org.postgresql.Driver","Postgre","jdbc:postgresql://IP:端口/数据库名"),
    SQLITE3("sqlite3","org.sqlite.JDBC","Sqlite","jdbc:sqlite:/文件地址/数据库名.db"),
    H2("h2","org.h2.Driver","H2","尚未支持!"),
    SQL_SERVER("sqlserver","com.microsoft.jdbc.sqlserver.SQLServerDriver","SqlServer 2000(目前仅支持测试)","jdbc:sqlserver://IP:端口;DatabaseName=数据库名"),
    SQL_SERVER_2("sqlserver2","com.microsoft.sqlserver.jdbc.SQLServerDriver","SqlServer 2005+","jdbc:sqlserver://IP:端口;DatabaseName=数据库名");

    public String KEY;
    public String DRIVER;
    public String LABEL;
    public String URL;

    DataSourceDbType(String KEY,String DRIVER,String LABEL,String URL){
        this.KEY = KEY;
        this.DRIVER = DRIVER;
        this.LABEL = LABEL;
        this.URL = URL;
    }

    public static DataSourceDbType get(String key) {
        DataSourceDbType[] values = DataSourceDbType.values();
        for (DataSourceDbType object : values) {
            if (object.KEY.equals(key)) {
                return object;
            }
        }
        return null;
    }


    /**
     * 获取一个简单的List
     * @return
     */
    public static List<Map<String,Object>> getSimpleList(){
        List<Map<String,Object>> resultList = new ArrayList<>();
        DataSourceDbType[] values = DataSourceDbType.values();
        for (DataSourceDbType object : values) {
            Map<String,Object> item = new HashMap<>();
            item.put("label",object.LABEL);
            item.put("value",object.KEY);
            item.put("driver", object.DRIVER);
            item.put("url",object.URL);
            resultList.add(item);
        }
        return resultList;
    }
}

5.去掉所有的\t \n

String s = "你的数据";
s = s.replace("\\n","");
s = s.replace("\\r","");
s = s.replace("\\","");

6.根据“.”通配多级目录数据结构

===========================================
outKey = name
mappingPath = data.user.name
item = new HashMap();
map = {
	data:{
		user:{
			name:"张三"
		}
	}
}
==========================================

private void getMappingValue(String outKey,String mappingPath,Map<String,Object> item,Map map){
	String[] path = mappingPath.split("\\.");
	Object next = map;
	for(String key : path){
		next = map.get(key);
	}
	item.put(outKey,next);
}

==========================================
return
item = {name:"张三"}

持续更新,最后一次更新时间:2021年10月20日20:35:30

这篇关于JAVA开发数据源系统经验汇总的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!