zoukankan      html  css  js  c++  java
  • Mybatis 在CS程序中的应用

    因为mybatis好使,所以几乎需要操作数据库的时候,我都会使用mybatis,而且在一个正式的项目中,同时存在BS和CS的程序,都使用的Mybatis,使用的相同mapper文件。


    如果是自己用的Mybatis,不需要考虑对配置文件加密,如果不是,那就需要考虑加密,这篇文章主要讲如何配置CS的Mybatis。


    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,这里就不详细介绍了(除非有需要)。


  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3125121.html
Copyright © 2011-2022 走看看