iBATIS step by step
2008-01-05 20:09:16 来源:WEB开发网一些说明,
1.例子使用eclipse进行开发,所以附件的包里有eclipse的.classpath和.PRoject文件,为了简单清楚,我尽量简化了相关的配置
2.附件带有所有需要的jar并在win2k+jdk1.4.2和fedora core 1 + jdk1.4.2上测试通过
下面正式开始....
首先建立一个简单的bean:Account
private int id;
private String firstName;
private String lastName;
private String emailAddress;
...
getters & setters
然后写一个xml文件,就是sql mapping的配置了,比如叫Account.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sql-map
PUBLIC "-//iBATIS.com//DTD SQL Map 1.0//EN"
"http://www.ibatis.com/dtd/sql-map.dtd";>
<sql-map name="Account">
<!--随便写了几个例子-->
<mapped-statement name="getAccountByName" result-class="man.argan.ibatis.domain.Account">
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>
进入讨论组讨论。
这样我们需要的和业务相关的 配置就这么多了,在程序里面,我们就可以进行使用了
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", "%阿%");
println("getAccountByName\t-->list: " + list);
当然,我们还需要一些关于数据库的配置,比较简单:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sql-map-config
PUBLIC "-//iBATIS.com//DTD SQL Map Config 1.0//EN"
更多精彩
赞助商链接