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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isea533

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值