发布于 2016-08-08 22:48:33 | 136 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的MyBatis教程,程序狗速度看过来!

MyBatis 基于Java的持久层框架

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。


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

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务