Today, STEP BY Step is taken today, first uses a simplest example to illustrate the flexibility and convenience of IBATIS.
Some instructions, 1. Examples were developed using Eclipse, so there is an Eclipse .classpath and .project files in the package, I try to simplify the relevant configuration 2. Annex with all the needed JAR and in Win2k JDK1.4.2 and Fedora Core 1 JDK1.4.2 Test By official start .... First create a simple bean: Account
Private int ID; private string firstname; private string emailaddress; ... getters & setters
Then write an XML file, which is the configuration of SQL mapping, such as called Account.xml.
XML Version = "1.0" Encoding = "UTF-8"?> select ACC_ID as id, ACC_FIRST_NAME as firstName, ACC_LAST_NAME as lastName, ACC_EMAIL as emailAddress from ACCOUNT where ACC_FIRST_NAME like # value # OR ACC_LAST_NAME like # value # mapped-statement> .... SQL-MAP> This is so much such as the business-related configuration, in the program, we can use it.
Account account = null; account = new Account (); account = (Account) sqlMap.executeQueryForObject ( "getAccountIdAndName", new Integer (1), account); println ( "getAccountIdAndName / t -> Account:" account); account = (Account) sqlMap.executeQueryForObject ( "getAccountEmail", new Integer (4)); println ( "getAccountEmail / t -> Account:" account); List list = sqlMap.executeQueryForList ( "getAccountByName", "% A % "); Println (" GetAccountByname / T -> list: " list); of course, we also need some of the database configuration, relatively simple:
XML Version = "1.0" encoding = "UTF-8"?> Datasource> sql-map-config> This profile is actually equivalent to a total control configuration of SQL Map The entrance we read is here, iBatis provides a very convenient way of reading: public class sqlmapconfig {protected static final sqlmap sqlmap; static {try {// - acquire configuration information Reader Reader = Resources .getResourceceasReader "Man / Argan / Ibatis / SQLMAP / MAPS / SQLMAPConfig.xml"); / / SQL MAP SQLM is constructed from the configuration information AP = xmlsqlmapbuilder.buildsqlmap (reader);} catch (exception e) {// If the read configuration information is wrong, you can't process it, you have to throw the Throw new NestedRuntimeException ("initialize the SQL Map Config error:" E, E);} } Public static sqlmap getsqlmap () {return sqlmap;}} Basically, we need to do this, after doing these work, we can write a method to test, this time, you can try it. The SQL statement in the configuration file is running, you can easily experience the power of iBatis and flexible. Click here to download all programs.