淺析Mybatis 在CS程序中的應(yīng)用
因?yàn)閙ybatis好使,所以幾乎需要操作數(shù)據(jù)庫的時(shí)候,我都會(huì)使用mybatis,而且在一個(gè)正式的項(xiàng)目中,同時(shí)存在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進(jìn)行雙向加密,變成了一個(gè)字節(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 {
//對(duì)資源進(jìn)行加密,解密后處理
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是一個(gè)使用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類就可以在程序的任意地方進(jìn)行調(diào)用了。
除了使用XML方式配置Mybatis外,還可以完全使用JAVA代碼進(jìn)行配置,這種方式比較麻煩,需要?jiǎng)?chuàng)建一個(gè)DataSource,然后用Mybatis配置類加載所有需要的mapper.class,這里就不詳細(xì)介紹了(除非有需要)。
- MyBatis入門學(xué)習(xí)教程(一)-MyBatis快速入門
- MyBatis學(xué)習(xí)筆記(二)之關(guān)聯(lián)關(guān)系
- 深入淺析mybatis oracle BLOB類型字段保存與讀取
- 解決springmvc+mybatis+mysql中文亂碼問題
- Java簡單實(shí)現(xiàn)SpringMVC+MyBatis分頁插件
- mybatis的動(dòng)態(tài)sql詳解(精)
- SpringMVC+MyBatis聲明式事務(wù)管理
- java利用mybatis攔截器統(tǒng)計(jì)sql執(zhí)行時(shí)間示例
- MyBatis高級(jí)映射學(xué)習(xí)教程
相關(guān)文章
Springboot配置過濾器實(shí)現(xiàn)過程解析
這篇文章主要介紹了Springboot配置過濾器實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問題的解決方法
這篇文章主要給大家介紹了關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
Java中BM(Boyer-Moore)算法的圖解與實(shí)現(xiàn)
本文主要介紹了兩個(gè)大的部分,第一部分通過圖解的方式講解BM算法,第二部分則代碼實(shí)現(xiàn)一個(gè)簡易的BM算法,感興趣的小伙伴可以學(xué)習(xí)一下2022-05-05
SpringBoot中的@CacheEvict 注解的實(shí)現(xiàn)
本文主要介紹了SpringBoot中的@CacheEvict注解的實(shí)現(xiàn),@CacheEvict 注解用于清空緩存,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03
springboot-2.3.x最新版源碼閱讀環(huán)境搭建(基于gradle構(gòu)建)
這篇文章主要介紹了springboot-2.3.x最新版源碼閱讀環(huán)境搭建(基于gradle構(gòu)建),需要的朋友可以參考下2020-08-08
利用Spring IOC技術(shù)實(shí)現(xiàn)用戶登錄驗(yàn)證機(jī)制
這篇文章主要為大家詳細(xì)介紹了Spring IOC技術(shù)實(shí)現(xiàn)用戶登錄驗(yàn)證機(jī)制的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

