1.配置MyBatisConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 驼峰式命名 -->
<!-- <setting name="logImpl" value="STDOUT_LOGGING"/>-->
<setting name="cacheEnabled" value="true"/>
<!-- cacheEnabled是二是级缓存的总开关,置为false代表关闭二级缓存 -->
<setting name="localCacheScope" value="STATEMENT"/>
<!-- localCacheScope是本地缓存(一级缓存)的作用域,只有两种取值:SESSION和STATEMENT,取STATEMENT意味着关闭一级缓存-->
<setting name="callSettersOnNulls" value="true"/>
<!-- null 也要这个key -->
</settings>
<environments default="development">
<!-- 默认-->
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="poolPingEnabled" value="true"/>
<property name="poolPingConnectionsNotUsedFor" value="30000"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/DimLabelMapper.xml"/>
</mappers>
</configuration>
2.配置db.properties文件
单个文件配置
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://121.0.0.1:3306/stu
username=root
password=wHJ4MpJeWJndDqRhe/Fc4Q==
多个配置
development.driver=com.mysql.cj.jdbc.Driver
development.url=jdbc:mysql://121.0.0.1:3306/stu
development.username=root
development.password=wHJ4MpJeWJndDqRhe/Fc4Q==
development1.driver=com.mysql.cj.jdbc.Driver
development1.url=jjdbc:mysql://121.0.0.1:3306/stu
development1.username=root
development1.password=wHJ4MpJeWJndDqRhe/Fc4Q==
development2.driver=com.mysql.cj.jdbc.Driver
development2.url=jdbc:mysql://121.0.0.1:3306/stu
development2.username=root
development2.password=wHJ4MpJeWJndDqRhe/Fc4Q==
3.获取session
单个文件配置
public static SqlSession openSession(String resource) {
SqlSessionFactory sqlSessionFactory = null;
try {
InputStream inputStream = Resources.getResourceAsStream("xml");
Properties properties = Resources.getResourceAsProperties("properties");
properties.setProperty("password", AESUtil.decrypt(properties.get("password")));
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
} catch (Exception e) {
e.printStackTrace();
log.error("默认数据库连接异常!!!");
}
return sqlSessionFactory.openSession();
}
多个配置(使用完成后,需要关闭所有的session)
public static Map<String, SqlSession> openSessionOther(String resource){
Map<String, SqlSession> sessionMap = new HashMap<>();
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
Map<String, Properties> map = parseProperties(resource);
for (String key : map.keySet()) {
Properties properties =map.get(key);
properties.setProperty("password", AESUtil.decrypt(properties.get("password")));
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(MYBATIS_CFG), properties);
sessionMap.put(key, factory.openSession());
}
} catch (Exception e) {
e.printStackTrace();
log.error("其他数据库连接异常!!!");
}
return sessionMap;
}
public static Map<String, Properties> parseProperties(String resource) throws Exception{
Map<String, Properties> map = new HashMap<>();
Properties properties = Resources.getResourceAsProperties(resource);
for (String key : properties.stringPropertyNames()) {
String[] dataName = key.split("\\.");
if (dataName.length == 2) {
if(StringUtils.isNull(map.get(dataName[0]))) {
map.put(dataName[0], new Properties());
}
}
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
String[] dataName = String.valueOf(entry.getKey()).split("\\.");
if (dataName.length == 2) {
Properties p = map.get(dataName[0]);
if (StringUtils.isNotNull(p)) {
p.setProperty(dataName[1],String.valueOf(entry.getValue()));
}
}
}
return map;
}
数据库加密文件 AES加密方式
package utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
/**
* @Description: 加密AES
* @Author zhou
* @Date 2024/5/29 - 14:38
*/
public class AESUtil {
public static final String sKey = "111;AAAA1234++==";
/**
* 加密
* @param crypt 输入密码
* @return 返回加密数据
*/
public static String encrypt(Object crypt){
String sSrc = String.valueOf(crypt);
byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
SecretKeySpec sKeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = null;//"算法/模式/补码方式"
byte[] encrypted = new byte[0];
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
encrypted = cipher.doFinal(sSrc.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
/**
* 解密
* @param crypt 需要解密的字符串
* @return 解密后的数据
*/
public static String decrypt(Object crypt){
String sSrc = String.valueOf(crypt);
// 判断Key是否正确
byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
SecretKeySpec sKeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = null;//"算法/模式/补码方式"
byte[] original = new byte[0];
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
original = cipher.doFinal(encrypted1);
} catch (Exception e) {
e.printStackTrace();
}
return new String(original,StandardCharsets.UTF_8);
}
public static void main(String[] args) {
// 需要加密的字串
String cSrc = "123456";
// 加密
String enString = encrypt(cSrc);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = decrypt(enString);
System.out.println("解密后的字串是:" + DeString);
}
}