淺析Mybatis 在CS程序中的應用
因為mybatis好使,所以幾乎需要操作數(shù)據(jù)庫的時候,我都會使用mybatis,而且在一個正式的項目中,同時存在BS和CS的程序,都使用的Mybatis,使用的相同mapper文件。
Mybatis的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>
<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" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/isea/dao/YouMapper.xml" />
</mappers>
</configuration>
為了防止數(shù)據(jù)庫用戶名密碼泄漏,我將XML進行雙向加密,變成了一個字節(jié)文件,而且文件名后綴隨意。
例如:basic.data,內(nèi)容局部如下:
根據(jù)XML生成Mybatis的SqlSessionFactory,代碼如下:
public class MyBatis {
private static final String CONFIG = "basic.data";
private SqlSessionFactory sqlSessionFactory;
private static MyBatis instance = new MyBatis();
private MyBatis(){
InputStream inputStream = null;
try {
inputStream = getXMLIS();
if(inputStream==null){
throw new RuntimeException("數(shù)據(jù)庫信息配置失敗!");
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} finally{
try {
inputStream.close();
} catch (Exception e) {
}
}
}
public static InputStream getXMLIS(){
InputStream inputStream = null;
try {
//對資源進行加密,解密后處理
BufferedReader reader = new BufferedReader(new FileReader(new File(Config.LOCATION+"/"+CONFIG)));
String str = null;
StringBuffer sbBuffer = new StringBuffer();
while((str=reader.readLine())!=null){
sbBuffer.append(str);
}
EncrypDES encrypDES = new EncrypDES();
String result = encrypDES.Decryptor(sbBuffer.toString());
inputStream = new ByteArrayInputStream(result.getBytes());
return inputStream;
} catch (Exception e) {
}
return null;
}
public SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
public static MyBatis getInstance(){
return instance;
}
}
這里的data文件是在src下。
代碼中的EncrypDES是一個使用DES的加密解密類。
代碼中的Config.LOCATION代碼如下:
public static String getRealPath() throws Exception {
String realPath = Config.class.getClassLoader().getResource("").getFile();
java.io.File file = new java.io.File(realPath);
realPath = file.getAbsolutePath();
realPath = java.net.URLDecoder.decode(realPath, "utf-8");
return realPath;
}
getRealPath()返回的值賦給LOCATION.
上面代碼的主要流程:讀取data文件,解密,以流的形式返回給mybatis.
通過Mybatis類就可以在程序的任意地方進行調(diào)用了。
除了使用XML方式配置Mybatis外,還可以完全使用JAVA代碼進行配置,這種方式比較麻煩,需要創(chuàng)建一個DataSource,然后用Mybatis配置類加載所有需要的mapper.class,這里就不詳細介紹了(除非有需要)。
相關文章
SpringBoot使用ExceptionHandler做異常處理
這篇文章主要介紹了SpringBoot使用ExceptionHandler做異常處理,這篇文章通過多種方法案例來介紹該項技術的使用,需要的朋友可以參考下2021-06-06java接收文件流+response.body()調(diào)用兩次問題(分別接收文件和對象)
這篇文章主要介紹了java接收文件流+response.body()調(diào)用兩次問題(分別接收文件和對象),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06SpringBoot中EasyExcel實現(xiàn)execl導入導出
本文主要介紹了SpringBoot中EasyExcel實現(xiàn)execl導入導出,實現(xiàn)了如何準備環(huán)境、創(chuàng)建實體類、自定義轉換器以及編寫導入邏輯的步驟和示例代碼,感興趣的可以了解下2023-06-06mybatis如何實現(xiàn)in傳入數(shù)組查詢
這篇文章主要介紹了mybatis如何實現(xiàn)in傳入數(shù)組查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot同時支持HTTPS與HTTP的實現(xiàn)示例
本文主要介紹了SpringBoot同時支持HTTPS與HTTP的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07Spring?BeanFactory容器的構建和使用示例詳解
BeanFactory是Spring框架中的一部分,它提供了IoC(控制反轉)的實現(xiàn)機制,下面小編就來和大家簡單聊聊BeanFactory容器的構建和使用示例吧2023-07-07SpringBoot中HttpSessionListener的簡單使用方式
這篇文章主要介紹了SpringBoot中HttpSessionListener的簡單使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03