A brief analysis of the application of Mybatis in CS program

  • 2020-04-01 02:10:07
  • OfStack

Because mybatis is good, I use mybatis almost when I need to operate the database, and in a formal project, there are both BS and CS programs, both using mybatis, using the same mapper file.

The normal XML configuration file of Mybatis is as follows:


<?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>

To prevent database username and password leaks, I encrypted the XML in both directions, turning it into a byte file with arbitrary filename suffixes.
For example: basic.data, the content parts are as follows:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/20130607155118046.jpg ">

Generate SqlSessionFactory of Mybatis according to XML, the code is as follows:


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(" Database information configuration failed !");
   }
   sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  } finally{
   try {
    inputStream.close();
   } catch (Exception e) {
   }
  }
 }

 public static InputStream getXMLIS(){
  InputStream inputStream = null;
  try {
   //Resource encryption, decryption after processing
   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;
 }
}

The data file here is under SRC.
The EncrypDES in the code is an encryption and decryption class that USES DES.
The config.location code in the code is as follows:

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;
 }

The value returned by getRealPath() is assigned to the LOCATION.

The main flow of the above code: Read the data file, decrypt it, and return it to mybatis as a stream.
The Mybatis class can be called from anywhere in the program.

In addition to configuring Mybatis in XML, you can also configure it entirely using JAVA code, which is a cumbersome way of creating a DataSource and then using the Mybatis configuration class to load all the required mapper.classes, which I won't go into here (unless required).


Related articles: