The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

Ibatis - Programming with the SQL Map Framework


Now that we are all configured and mapped, all we need to do is code it in our Java application. The first step is to configure the SQL Map. This is very simply a matter of loading our SQL Map configuration XML file that we created before. To simplify loading the XML file, we can make use of the Resources class included with the framework.

String resource = “com/ibatis/example/sqlMap-config.xml”;
Reader reader = Resources.getResourceAsReader (resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

The SqlMapClient object is a long-lived, thread safe service object. For a given run of an application, you only need to instantiate/configure it once. This makes it a good candidate for a static member of a base class (e.g. base DAO class), or if you prefer to have it more centrally configured and globally available, you could wrap it up in a convenience class of your own. Here is an example of a convenience class you could write:

public MyAppSqlConfig {
private static final SqlMapClient sqlMap;
static {
try {
String resource = “com/ibatis/example/sqlMap-config.xml”;
Reader reader = Resources.getResourceAsReader (resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
// If you get an error at this point, it doesn’t matter what it was. It is going to be
// unrecoverable and we will want the app to blow up hard so we are aware of the
// problem. You should always log such errors and re-throw them in such a way that
// you can be made immediately aware of the problem.
e.printStackTrace();
throw new RuntimeException (“Error initializing MyAppSqlConfig class. Cause: ” + e);
}
}
public static SqlMapClient getSqlMapInstance () {
return sqlMap;
}
}
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com