浅析Mybatis 在CS程序中的应用
时间:2023-06-24 08:44:15
因为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,这里就不详细介绍了(除非有需要)。
标签:Mybatis,CS程序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Java字符串比较的三种方法
2023-05-13 12:26:51
Java实战之基于swing的QQ邮件收发功能实现
2023-11-15 01:34:26
Android利用HorizontalScrollView仿ViewPager设计简单相册
2022-07-29 12:28:33
![](https://img.aspxhome.com/file/2023/5/96755_0s.png)
Java增加自定义注解进行校验入参详解
2023-01-05 13:34:25
Flutter利用注解生成可自定义的路由的实现
2022-08-28 15:12:16
Android 自定义View实现多节点进度条功能
2022-05-04 17:13:37
![](https://img.aspxhome.com/file/2023/5/98665_0s.png)
C#图像处理的多种方法
2022-04-24 01:11:36
![](https://img.aspxhome.com/file/2023/9/89779_0s.jpg)
J2EE Servlet上传文件到服务器并相应显示功能的实现代码
2023-12-05 16:36:14
![](https://img.aspxhome.com/file/2023/9/96049_0s.png)
Kotlin基础通关之字符串与数字类型
2023-06-22 13:27:35
java入门概念个人理解之package与import浅析
2021-07-09 10:18:55
Android中使用Matrix控制图形变换和制作倒影效果的方法
2022-11-23 07:35:23
![](https://img.aspxhome.com/file/2023/8/137558_0s.png)
SpringBoot+Shiro+LayUI权限管理系统项目源码
2021-07-13 16:35:19
![](https://img.aspxhome.com/file/2023/6/95976_0s.png)
如何在C#中使用指针
2022-07-02 16:09:47
![](https://img.aspxhome.com/file/2023/1/111241_0s.png)
C#中利用Lotus notes公共邮箱发送邮件的方法
2023-10-02 03:00:12
Android垂直切换的圆角Banner与垂直指示器相关介绍与应用详解
2023-01-09 02:59:15
![](https://img.aspxhome.com/file/2023/5/137885_0s.gif)
java实现微信扫码登录第三方网站功能(原理和代码)
2023-08-04 00:44:11
![](https://img.aspxhome.com/file/2023/3/93233_0s.png)
Java面试题冲刺第十八天--Spring框架3
2023-04-17 22:44:29
![](https://img.aspxhome.com/file/2023/4/96124_0s.jpg)
java开发工作中对InheritableThreadLocal使用思考
2023-11-24 21:46:44
![](https://img.aspxhome.com/file/2023/6/59186_0s.jpg)
C# 添加对System.Configuration.dll文件的引用操作
2022-03-05 22:20:31
![](https://img.aspxhome.com/file/2023/7/69807_0s.jpg)
Mybatis中where标签与if标签结合使用详细说明
2021-07-27 08:15:53