摘要:配置文件一般是使用保存配置文件內(nèi)容然后在配置文件中進(jìn)行讀取在文件下新建文件內(nèi)容如下數(shù)據(jù)庫配置文件然后接著把文件放入源碼包中配置文件讀取數(shù)據(jù)庫配置文件定義別名自定義數(shù)據(jù)處理定義數(shù)據(jù)庫信息事物管理
properties配置文件
一般是使用properties保存配置文件內(nèi)容,然后在mybatis配置文件中進(jìn)行讀取
在resource文件下新建db.properties文件
內(nèi)容如下
# 數(shù)據(jù)庫配置文件 driver = com.mysql.cj.jdbc.Driver url = jdbc:mysql:// /mybatis username = password =
然后,接著把文件放入源碼包中
配置mybatis-config.xml文件
目錄結(jié)構(gòu)如下
生產(chǎn)環(huán)境的數(shù)據(jù)庫密碼都為加密密碼,需要在使用的時候,把加密密碼解密成為明文
先創(chuàng)建數(shù)據(jù)庫密碼類
package com.ming.Util; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.*; import java.util.Base64; public class Decode { /** * 生成秘鑰 * @param * @return */ public static String generateDecode() throws UnsupportedEncodingException { KeyGenerator keyGen = null;//密鑰生成器 try { keyGen = KeyGenerator.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } keyGen.init(56);//初始化密鑰生成器 SecretKey secretKey = keyGen.generateKey();//生成密鑰 byte[] key = secretKey.getEncoded();//密鑰字節(jié)數(shù)組 // 進(jìn)行base64編碼 String encodedKey = Base64.getEncoder().encodeToString(key); return encodedKey; } /** * 進(jìn)行加密 * @param string * @param key * @return */ public static String encryptionDecode(String string, String key){ //System.out.println(System.getenv("KEYWORDES")); SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "DES");//恢復(fù)密鑰 Cipher cipher = null;//Cipher完成加密或解密工作類 try { cipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } try { cipher.init(Cipher.ENCRYPT_MODE, secretKey);//對Cipher初始化,加密模式 } catch (InvalidKeyException e) { e.printStackTrace(); } byte[] cipherByte = null; try { cipherByte = cipher.doFinal(Base64.getDecoder().decode(string));//加密data } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return Base64.getEncoder().encodeToString(cipherByte); } public static String decryptDecode(String string, String key){ SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "DES");//恢復(fù)密鑰 Cipher cipher = null;//Cipher完成加密或解密工作類 try { cipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } try { cipher.init(Cipher.DECRYPT_MODE, secretKey);//對Cipher初始化,解密模式 } catch (InvalidKeyException e) { e.printStackTrace(); } byte[] cipherByte = new byte[0];//解密data try { cipherByte = cipher.doFinal(Base64.getDecoder().decode(string)); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return Base64.getEncoder().encodeToString(cipherByte); } }
該類有三個方法,為加密data,解密data,生成key
然后編輯操作系統(tǒng)環(huán)境變量
達(dá)到輸入
? ~ echo $KEYWORDES
可以輸出環(huán)境變量
接著再次修改SqlSessionFactoryUtil類
package com.ming.Util; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.Properties; /** * @author ming * 構(gòu)建SqlSessionFactory * 由于數(shù)據(jù)庫連接是寶貴的,需要對數(shù)據(jù)庫連接統(tǒng)一管理,所以使用單例進(jìn)行管理 * 這里的單利使用的雙重鎖 * SqlSessionFactory為線程不安全類型需要加鎖,確保同一時刻,只有一個線程可以使用該對象 */ public class SqlSessionFactoryUtil { /** * SqlSessionFactory對象 */ private static SqlSessionFactory sqlSessionFactory = null; /** * 類線程鎖 */ private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class; /** * 日志管理類 */ private static final Logger logger = LogManager.getLogger(); /** * 單例 */ private SqlSessionFactoryUtil(){ } /** * @return SqlSessionFactory * 初始化SqlSessionFactory對象 */ public static SqlSessionFactory initSqlSessionFactory(){ // 獲得輸入流 InputStream cfgStream = null; // 閱讀流 Reader cfgReader = null; InputStream proStream = null; Reader proReader = null; // 持久化屬性集 Properties properties = null; try{ // 配置文件流 cfgStream = Resources.getResourceAsStream("mybatis-config.xml"); // 獲得閱讀流 cfgReader = new InputStreamReader(cfgStream); // 讀入屬性文件 proStream = Resources.getResourceAsStream("db.properties"); proReader = new InputStreamReader(proStream); // 持久化屬性集 properties = new Properties(); // 流轉(zhuǎn)載進(jìn)入屬性集合 properties.load(proReader); }catch (Exception e){ logger.error(e); } if(sqlSessionFactory == null){ synchronized (CLASS_LOCK){ sqlSessionFactory = new SqlSessionFactoryBuilder().build(cfgReader, properties); } } return sqlSessionFactory; } /** * 打開SqlSession * @return SqlSession */ public static SqlSession openSqlSesion(){ // 判空處理 if(sqlSessionFactory == null){ initSqlSessionFactory(); } return sqlSessionFactory.openSession(); } }
接著,再次對密碼進(jìn)行加密,在讀取的時候,對閱讀流的結(jié)果集進(jìn)行持久化設(shè)置
先對db.properties數(shù)據(jù)庫密碼進(jìn)行加密
更改以后配置文件如下
# 數(shù)據(jù)庫配置文件 driver = com.mysql.cj.jdbc.Driver url = jdbc:mysql://47.94.95.84:32786/mybatis username = mybatis password = 8GgwaJCtTXLGItiYF9c4mg==
接著再次更改Util類
package com.ming.Util; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.Properties; /** * @author ming * 構(gòu)建SqlSessionFactory * 由于數(shù)據(jù)庫連接是寶貴的,需要對數(shù)據(jù)庫連接統(tǒng)一管理,所以使用單例進(jìn)行管理 * 這里的單利使用的雙重鎖 * SqlSessionFactory為線程不安全類型需要加鎖,確保同一時刻,只有一個線程可以使用該對象 */ public class SqlSessionFactoryUtil { /** * SqlSessionFactory對象 */ private static SqlSessionFactory sqlSessionFactory = null; /** * 類線程鎖 */ private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class; /** * 日志管理類 */ private static final Logger logger = LogManager.getLogger(); /** * 單例 */ private SqlSessionFactoryUtil(){ } /** * @return SqlSessionFactory * 初始化SqlSessionFactory對象 */ public static SqlSessionFactory initSqlSessionFactory(){ // 獲得輸入流 InputStream cfgStream = null; // 閱讀流 Reader cfgReader = null; InputStream proStream = null; Reader proReader = null; // 持久化屬性集 Properties properties = null; try{ // 配置文件流 cfgStream = Resources.getResourceAsStream("mybatis-config.xml"); // 獲得閱讀流 cfgReader = new InputStreamReader(cfgStream); // 讀入屬性文件 proStream = Resources.getResourceAsStream("db.properties"); proReader = new InputStreamReader(proStream); // 持久化屬性集 properties = new Properties(); // 流裝載進(jìn)入屬性集合 properties.load(proReader); // 獲取當(dāng)前系統(tǒng)ENV String key = System.getenv("KEYWORDES"); // 進(jìn)行解密 properties.setProperty("password", Decode.decryptDecode(properties.getProperty("password"), key)); }catch (Exception e){ logger.error(e); } if(sqlSessionFactory == null){ synchronized (CLASS_LOCK){ sqlSessionFactory = new SqlSessionFactoryBuilder().build(cfgReader, properties); } } return sqlSessionFactory; } /** * 打開SqlSession * @return SqlSession */ public static SqlSession openSqlSesion(){ // 判空處理 if(sqlSessionFactory == null){ initSqlSessionFactory(); } return sqlSessionFactory.openSession(); } }
書寫單元測試
package com.ming.Util; import org.junit.Test; import static org.junit.Assert.*; public class SqlSessionFactoryUtilTest { @Test public void initSqlSessionFactory() { } @Test public void openSqlSesion() { SqlSessionFactoryUtil.openSqlSesion(); } }
目前的目錄結(jié)構(gòu)
此時執(zhí)行單元測試,可以發(fā)現(xiàn)單元測試已經(jīng)通過
控制臺打印出log信息
2019-04-11 17:17:37.357 [DEBUG] org.apache.ibatis.logging.LogFactory.setImplementation(LogFactory.java:105) - Logging initialized using "class org.apache.ibatis.logging.log4j2.Log4j2Impl" adapter. 2019-04-11 17:17:37.403 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:334) - PooledDataSource forcefully closed/removed all connections. 2019-04-11 17:17:37.403 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:334) - PooledDataSource forcefully closed/removed all connections. 2019-04-11 17:17:37.404 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:334) - PooledDataSource forcefully closed/removed all connections. 2019-04-11 17:17:37.404 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:334) - PooledDataSource forcefully closed/removed all connections. Process finished with exit code 0
發(fā)現(xiàn)錯誤,修改加密類
package com.ming.Util; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.*; import java.util.Base64; public class Decode { /** * 生成秘鑰 * @param * @return */ public static String generateDecode() throws UnsupportedEncodingException { KeyGenerator keyGen = null;//密鑰生成器 try { keyGen = KeyGenerator.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } keyGen.init(56);//初始化密鑰生成器 SecretKey secretKey = keyGen.generateKey();//生成密鑰 byte[] key = secretKey.getEncoded();//密鑰字節(jié)數(shù)組 // 進(jìn)行base64編碼 String encodedKey = Base64.getEncoder().encodeToString(key); return encodedKey; } /** * 進(jìn)行加密 * @param string * @param key * @return */ public static String encryptionDecode(String string, String key){ SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "DES");//恢復(fù)密鑰 Cipher cipher = null;//Cipher完成加密或解密工作類 try { cipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } try { cipher.init(Cipher.ENCRYPT_MODE, secretKey);//對Cipher初始化,加密模式 } catch (InvalidKeyException e) { e.printStackTrace(); } byte[] cipherByte = null; try { cipherByte = cipher.doFinal(string.getBytes());//加密data } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return Base64.getEncoder().encodeToString(cipherByte); } /** * 進(jìn)行解密 * @param string * @param key * @return */ public static String decryptDecode(String string, String key){ SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "DES");//恢復(fù)密鑰 Cipher cipher = null;//Cipher完成加密或解密工作類 try { cipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } try { cipher.init(Cipher.DECRYPT_MODE, secretKey);//對Cipher初始化,解密模式 } catch (InvalidKeyException e) { e.printStackTrace(); } byte[] cipherByte = new byte[0];//解密data try { cipherByte = cipher.doFinal(Base64.getDecoder().decode(string)); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return new String(cipherByte); } }
再次運(yùn)行,可以發(fā)現(xiàn)已經(jīng)成功執(zhí)行sql語句
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/77583.html
摘要:這里使用的是數(shù)據(jù)庫啟動類上加上注解在啟動類中添加對包掃描掃描多個包下的可以有以下幾種方法掃描會自動加載相關(guān)配置,數(shù)據(jù)源就會自動注入到中,會自動注入到中,可以直接使用。有配置文件下的使用掃描多個包下的可以有以下幾種方法掃描 Spring-Boot 學(xué)習(xí)筆記 1 Spring-Boot 介紹 1.1 什么是Spring-Boot Spring-Boot是由Pivotal團(tuán)隊(duì)提供的全新框架...
摘要:項(xiàng)目介紹在之前的整合項(xiàng)目之后,更加完善功能,之前的代碼不予展示與介紹,想了解的請參考整合項(xiàng)目項(xiàng)目代碼獲取功能新增用戶注冊登錄錯誤次數(shù)限制使用作緩存注解配置引入數(shù)據(jù)校驗(yàn)使用統(tǒng)一異常處理配置項(xiàng)目結(jié)構(gòu)代碼控制層,以下展示注冊和登錄功能會跳到我們自 shiro項(xiàng)目介紹 在之前的shiro整合項(xiàng)目之后,更加完善shiro功能,之前的代碼不予展示與介紹,想了解的請參考shiro整合項(xiàng)目項(xiàng)目代碼獲取...
摘要:通過整合及可以實(shí)現(xiàn)數(shù)據(jù)庫查詢后將數(shù)據(jù)持久化。但是可能出現(xiàn)幻像讀這是花費(fèi)最高代價(jià)但是最可靠的事務(wù)隔離級別。事務(wù)被處理為順序執(zhí)行。 所需技術(shù):spring、mybatis、druid、flyway、logback、nodejs、html、css3 ;目標(biāo):創(chuàng)建一個業(yè)務(wù)框架,后端采用spring+mybatis,中間層采用node,前端html5,css3等; showImg(https:/...
摘要:概述本章學(xué)習(xí)查詢的一對一關(guān)系的多種實(shí)現(xiàn)方式。本系列文章是基于版本??偨Y(jié)本文介紹了一對一映射的三種方法,希望對大家有幫助。最后創(chuàng)建了群方便大家交流,可掃描加入,同時也可加我,共同學(xué)習(xí)共同進(jìn)步,謝謝 概述 本章學(xué)習(xí)Mybatis查詢的一對一關(guān)系的多種實(shí)現(xiàn)方式。 本系列文章是基于Mybatis 3.4.6 版本。 創(chuàng)建表 創(chuàng)建測試使用的數(shù)據(jù)庫表,使用用戶表和用戶身份證表進(jìn)行測試,用戶表與身份...
摘要:概述上一篇針對進(jìn)行了入門體驗(yàn),本節(jié)將繼續(xù)介紹基本映射單表查詢。本系列文章是基于版本??偨Y(jié)本節(jié)實(shí)例了基本查詢及映射,這些也是實(shí)際工作中用的最多的功能,雖然簡單,日積月累會構(gòu)建起我們的知識結(jié)構(gòu)和體系,一步一步來。。。 概述 上一篇針對Mybatis進(jìn)行了入門體驗(yàn),本節(jié)將繼續(xù)介紹Mybatis基本映射--單表查詢。 本系列文章是基于Mybatis 3.4.6 版本。 創(chuàng)建表 針對于上一節(jié),我...
閱讀 5342·2021-09-07 09:58
閱讀 850·2019-08-30 15:55
閱讀 3046·2019-08-30 15:55
閱讀 986·2019-08-30 15:53
閱讀 1625·2019-08-29 12:57
閱讀 1959·2019-08-26 13:46
閱讀 628·2019-08-26 11:00
閱讀 3720·2019-08-23 15:42