1.导入需要的jar包,链接地址:https://github.com/zhangliqingyun/jarlist/tree/master/ibatis
2.配置ibatis操作数据库,sqlMapConfig.properties配置文件
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:xe
username=jygc
password=jygc
3.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>
<properties resource="sqlMapConfig.properties" />
<!-- Statement namespaces are required for Abator -->
<settings useStatementNamespaces="true" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"></property>
<property name="JDBC.ConnectionURL" value="${url}"></property>
<property name="JDBC.Username" value="${username}"></property>
<property name="JDBC.Password" value="${password}" />
</dataSource>
</transactionManager>
<!-- SQL Map XML files should be listed here -->
<sqlMap resource="com/rdtsw/xml/xxfw_SqlMap.xml" />
</sqlMapConfig>
4.Xxfw_SqlMap.xml文件配置
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="xxfw">
<sql id="paginationStart">
<![CDATA[
select * from (select row_.*, rownum rownum_ from (
]]>
</sql>
<sql id="paginationEnd">
<![CDATA[
)row_ where rownum<=#limit# ) where rownum_>#start#
]]>
</sql>
</sqlMap>
5.SqlMapconfig.java文件
public class SqlMapconfig {
private static final SqlMapClient sqlMapper;
static {
try {
Reader reader;
String resource = "SqlMapConfig.xml";
System.out.println(resource);
reader = Resources.getResourceAsReader(resource);
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(
"Error initializing MyAppSqlconfig class.Cause:" + e);
}
}
public static SqlMapClient getSqlMapInstance() {
return sqlMapper;
}
public static void startTransaction() throws SQLException{
sqlMapper.startTransaction() ;
}
public static void endTransaction() throws SQLException{
sqlMapper.endTransaction();
}
public static void commitTransaction() throws SQLException{
sqlMapper.commitTransaction();
sqlMapper.endTransaction();
}
public static SqlMapClient getSqlMapClient() {
return sqlMapper;
}
}
6.通过接口创建实现类
private static XxfwDao xxfwDao = new XxfwDaoImpl(SqlMapconfig.getSqlMapInstance());
7.写接口实现类
public class XxfwDaoImpl implements XxfwDao{
private SqlMapClient sqlMapClient;
public XxfwDaoImpl(SqlMapClient sqlMapClient){
this.sqlMapClient = sqlMapClient;
}
}