ibatis学习(2)一对多关系
ibatis一对多关系配置文件写法上节已经写了ibatis的相关配置文件和spring的集成,此处不多加赘述,主要看看配置文件该如何写。
1、新建两个类,一个Account包含多个OrderInfo
package ibatis.one.many;import java.util.List;public class Account {private int accountId;private String accountNo;private List orderList;public int getAccountId() {return accountId;}public void setAccountId(int accountId) {this.accountId = accountId;}public String getAccountNo() {return accountNo;}public void setAccountNo(String accountNo) {this.accountNo = accountNo;}public List getOrderList() {return orderList;}public void setOrderList(List orderList) {this.orderList = orderList;}}
package ibatis.one.many;import java.util.List;public class OrderInfo {private int orderId;private int accountId;private String orederName;public int getOrderId() {return orderId;}public void setOrderId(int orderId) {this.orderId = orderId;}public String getOrederName() {return orederName;}public void setOrederName(String orederName) {this.orederName = orederName;}public int getAccountId() {return accountId;}public void setAccountId(int accountId) {this.accountId = accountId;}}
2、account.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMapPUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN""http://www.ibatis.com/dtd/sql-map-2.dtd"><sqlMap namespace="account"><typeAlias alias="Account" type="ibatis.one.many.Account" /><typeAlias alias="OrderInfo" type="ibatis.one.many.OrderInfo" /><resultMap class="Account" id="accountResultMap"><result property="accountId" column="accountId" /><result property="accountNo" column="accountNo" /><result property="orderList" column="accountId"select="getOrdersByAccountId" /></resultMap><resultMap class="OrderInfo" id="orderResultMap"><result property="orderId" column="orderId" /><result property="orederName" column="orederName" /><result property="accountId" column="accountId" /></resultMap><select id="getAccounts" resultMap="accountResultMap"><!]></select><select id="getOrdersByAccountId" resultMap="orderResultMap"><!]></select></sqlMap>
3、测试类中main()中的内容:
try {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");SqlMapClient sqlMap=(SqlMapClient) ctx.getBean("sqlMapClient");sqlMap.flushDataCache();System.out.println("sqlMap=="+sqlMap);List accountList = sqlMap.queryForList("getAccounts",1);System.out.println("accountList=="+accountList.size());for(int i=0;i<accountList.size();i++){Account account = (Account) accountList.get(i);System.out.println(account.getAccountId()+"......."+account.getAccountNo());List orderList = account.getOrderList();System.out.println("orderList==="+orderList);for(int j=0;j<orderList.size();j++){OrderInfo orderInfo = (OrderInfo) orderList.get(j);System.out.println("orderInfo=="+orderInfo);System.out.println(orderInfo.getOrderId()+"....."+orderInfo.getOrederName());}}//Account account=(Account)sqlMap.queryForObject("getAccountInfoList", 1);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}
页:
[1]