Ibatis 简明教程
iBATIS - Debugging
在使用 iBATIS 时调试你的程序很容易。iBATIS 内置有日志记录支持,它与以下日志库协作,并按此顺序搜索它们。
It is easy to debug your program while working with iBATIS. iBATIS has built-in logging support and it works with the following logging libraries and searches for them in this order.
-
Jakarta Commons Logging (JCL).
-
Log4J
-
JDK logging
你可以将上面列出的任何库与 iBATIS 一起使用。
You can use any of the above listed libraries along with iBATIS.
Debugging with Log4J
假设你打算使用 Log4J 进行日志记录。在继续操作之前,你需要交叉检查以下几点 −
Assuming you are going to use Log4J for logging. Before proceeding, you need to cross-check the following points −
-
The Log4J JAR file (log4j-{version}.jar) should be in the CLASSPATH.
-
You have log4j.properties available in the CLASSPATH.
以下是 log4j.properties 文件。请注意,有些行已注释。如果你需要其他调试信息,你可以取消它们的注释。
Following is the log4j.properties file. Note that some of the lines are commented out. You can uncomment them if you need additional debugging information.
# Global logging configuration
log4j.rootLogger = ERROR, stdout
log4j.logger.com.ibatis = DEBUG
# shows SQL of prepared statements
#log4j.logger.java.sql.Connection = DEBUG
# shows parameters inserted into prepared statements
#log4j.logger.java.sql.PreparedStatement = DEBUG
# shows query results
#log4j.logger.java.sql.ResultSet = DEBUG
#log4j.logger.java.sql.Statement = DEBUG
# Console output
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %5p [%t] − %m%n
你可以在 Apaches 网站上找到 Log4J 的完整文档− Log4J Documentation 。
You can find the complete documentation for Log4J from Apaches site − Log4J Documentation.
iBATIS Debugging Example
下面的 Java 类是一个非常简单的例子,它初始化 Log4J 日志库并随后将它用于 Java 应用程序。我们将使用前面提到的位于 CLASSPATH 中的属性文件。
The following Java class is a very simple example that initializes and then uses the Log4J logging library for Java applications. We would use the above-mentioned property file which lies in CLASSPATH.
import org.apache.log4j.Logger;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class IbatisUpdate{
static Logger log = Logger.getLogger(IbatisUpdate.class.getName());
public static void main(String[] args) throws IOException,SQLException{
Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);
/* This would insert one record in Employee table. */
log.info("Going to update record.....");
Employee rec = new Employee();
rec.setId(1);
rec.setFirstName( "Roma");
smc.update("Employee.update", rec );
log.info("Record updated Successfully ");
log.debug("Going to read records.....");
List <Employee> ems = (List<Employee>)
smc.queryForList("Employee.getAll", null);
Employee em = null;
for (Employee e : ems) {
System.out.print(" " + e.getId());
System.out.print(" " + e.getFirstName());
System.out.print(" " + e.getLastName());
System.out.print(" " + e.getSalary());
em = e;
System.out.println("");
}
log.debug("Records Read Successfully ");
}
}
Compilation and Run
首先,确保在继续编译和执行之前你已适当地设置 PATH 和 CLASSPATH。
First of all, make sure you have set PATH and CLASSPATH appropriately before proceeding for compilation and execution.
-
Create Employee.xml as shown above.
-
Create Employee.java as shown above and compile it.
-
Create IbatisUpdate.java as shown above and compile it.
-
Create log4j.properties as shown above.
-
Execute IbatisUpdate binary to run the program.
你将得到以下结果。EMPLOYEE 表中的一条记录将被更新,然后从 EMPLOYEE 表中读取同一条记录。
You would get the following result. A record would be updated in the EMPLOYEE table and later, the same record would be read from the EMPLOYEE table.
DEBUG [main] - Created connection 28405330.
DEBUG [main] - Returned connection 28405330 to pool.
DEBUG [main] - Checked out connection 28405330 from pool.
DEBUG [main] - Returned connection 28405330 to pool.
1 Roma Ali 5000
2 Zara Ali 5000
3 Zara Ali 5000
Debug Methods
在上面的示例中,我们只使用了 info() 方法,但是你可以根据你的要求使用以下任何方法 −
In the above example, we used only info() method, however you can use any of the following methods as per your requirements −
public void trace(Object message);
public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);