因為mybatis好使
Mybatis的XML配置文件正常如下
<?xml version="
<!DOCTYPE configuration
PUBLIC "
"
<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
</mappers>
</configuration>
為了防止數據庫用戶名密碼洩漏
例如
根據XML生成Mybatis的SqlSessionFactory
public class MyBatis {
private static final String CONFIG = "basic
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()
} finally{
try {
inputStream
} catch (Exception e) {
}
}
}
public static InputStream getXMLIS(){
InputStream inputStream = null;
try {
//對資源進行加密
BufferedReader reader = new BufferedReader(new FileReader(new File(Config
String str = null;
StringBuffer sbBuffer = new StringBuffer();
while((str=reader
sbBuffer
}
EncrypDES encrypDES = new EncrypDES();
String result = encrypDES
inputStream = new ByteArrayInputStream(result
return inputStream;
} catch (Exception e) {
}
return null;
}
public SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
public static MyBatis getInstance(){
return instance;
}
}
這裡的data文件是在src下
代碼中的EncrypDES是一個使用DES的加密解密類
代碼中的Config
public static String getRealPath() throws Exception {
String realPath = Config
java
realPath = file
realPath = java
return realPath;
}
getRealPath()返回的值賦給LOCATION
上面代碼的主要流程
通過Mybatis類就可以在程序的任意地方進行調用了
除了使用XML方式配置Mybatis外
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20548.html