深入了解ibatis源码----简单ibatis示例代码

搭建源码环境:

1.创建sql数据库。

CREATE TABLE USER_ACCOUNT (
USERID INT(3) NOT NULL AUTO_INCREMENT,
USERNAME VARCHAR(10) NOT NULL,
PASSSWORD VARCHAR(30) NOT NULL,
GROUPNAME VARCHAR(10),
PRIMARY KEY (USERID)
); CREATE TABLE USER_ACCOUNT (
USERID INT(3) NOT NULL AUTO_INCREMENT,
USERNAME VARCHAR(10) NOT NULL,
PASSSWORD VARCHAR(30) NOT NULL,
GROUPNAME VARCHAR(10),
PRIMARY KEY (USERID)
);

2. 写主测试代码

import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.Resources;
import java.io.Reader;
import java.util.List;
public class Main {
public static void main(String arg[]) throws Exception {
String resource = "SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader (resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
List list = sqlMap.queryForList("getAllUsers", "EMPLOYEE");
System.out.println("Selected " + list.size() + " records.");
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}

3.将jar包加入到工程,并把源码复制到src目录下

4.src目录下加入配置文件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="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost/test"/>
<property name="JDBC.Username"
value="root"/>
<property name="JDBC.Password"
value="wangwei"/>
</dataSource>
</transactionManager>
<sqlMap resource="SqlMap.xml" />
</sqlMapConfig>

5.加入映射文件SqlMap.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>
<select id="getAllUsers" parameterClass="string"
resultClass="hashmap">
SELECT * FROM USER_ACCOUNT WHERE GROUPNAME = #groupName#
</select>
</sqlMap>

6.单步debug代码

List list = sqlMap.queryForList("getAllUsers", "EMPLOYEE");

调用SqlMapClientImpl.java

  public List queryForList(String id, Object paramObject) throws SQLException {
return getLocalSqlMapSession().queryForList(id, paramObject);
}
  protected SqlMapSessionImpl getLocalSqlMapSession() {
    SqlMapSessionImpl sqlMapSession = localSqlMapSession.get();
    if (sqlMapSession == null || sqlMapSession.isClosed()) {
      sqlMapSession = new SqlMapSessionImpl(this);
      localSqlMapSession.set(sqlMapSession);
    }
    return sqlMapSession;
  }

调用:SqlMapSessionImpl.java

  public List queryForList(String id, Object paramObject) throws SQLException {
return delegate.queryForList(sessionScope, id, paramObject);
}

调用SqlMapExecutorDelegate.java

  public List queryForList(SessionScope sessionScope, String id, Object paramObject) throws SQLException {
return queryForList(sessionScope, id, paramObject, SqlExecutor.NO_SKIPPED_RESULTS, SqlExecutor.NO_MAXIMUM_RESULTS);
}
  public List queryForList(SessionScope sessionScope, String id, Object paramObject, int skip, int max) throws SQLException {
    List list = null;     MappedStatement ms = getMappedStatement(id);
    Transaction trans = getTransaction(sessionScope);
    boolean autoStart = trans == null;     try {
      trans = autoStartTransaction(sessionScope, autoStart, trans);       StatementScope statementScope = beginStatementScope(sessionScope, ms);
      try {
        list = ms.executeQueryForList(statementScope, trans, paramObject, skip, max);
      } finally {
        endStatementScope(statementScope);
      }       autoCommitTransaction(sessionScope, autoStart);
    } finally {
      autoEndTransaction(sessionScope, autoStart);
    }     return list;
  }

至此返回结果(完成)

上一篇:java新特性--05--JDK9


下一篇:使用CSS里的user-select属性控制用户在页面上选中的内容