因为项目需要需要使用IBATIS作为数据持久层,于是就着手搭建个简单的IBATIS的开发环境,具体详情如下:
1.需要:ibatis-2.3.0.677.jar(ibatis依赖包)和ojdbc14.jar(oracle驱动包)两个包即可。目录如下图:
2.在classpath路径下放:SqlMapConfig.xml
内容入
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:oracle:thin:@10.153.195.6:1523:ahcrmb"/>
<property name="JDBC.Username" value="DBCONTACTKF"/>
<property name="JDBC.Password" value="DBCONTACTKF"/>
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>
<sqlMap resource="ibatismap/Comm.xml"/>
</sqlMapConfig>
3.在Comm.xml编辑:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Comm">
<!-- test sql -->
<select id="test" resultClass="int">
SELECT 100 FROM DUAL
</select>
</sqlMap>
4.编写测试用java代码:
package cn.com.sitech.kf.ibatis;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class SqlMapClientConfig {
private static Logger log = Logger.getLogger(SqlMapClientConfig.class);
private static SqlMapClient sqlMap = null;
static {
Reader reader = null;
try {
String resource = "SqlMapConfig.xml";
reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
log.error("Error intializing SqlMapClientConfig ", e);
throw new RuntimeException(
"Error initializing BaseIbatisDao class. Cause: " + e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static SqlMapClient getSqlMapInstance() {
return sqlMap;
}
public static void main(String[] args) {
Integer in;
try {
in = (Integer) sqlMap.queryForObject("test");
System.out.println(in.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行即可完成;