淺析Mybatis 在CS程式中的應用_java

來源:互聯網
上載者:User

因為mybatis好使,所以幾乎需要操作資料庫的時候,我都會使用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>

為了防止資料庫使用者名稱密碼泄漏,我將XML進行雙向加密,變成了一個位元組檔案,而且檔案名稱尾碼隨意。
例如:basic.data,內容局部如下:

根據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("資料庫資訊配置失敗!");
   }
   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類就可以在程式的任意地方進行調用了。

除了使用XML方式配置Mybatis外,還可以完全使用JAVA代碼進行配置,這種方式比較麻煩,需要建立一個DataSource,然後用Mybatis配置類載入所有需要的mapper.class,這裡就不詳細介紹了(除非有需要)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.