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 sqlMap PUBLIC "-//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">
<![CDATA[select * from Account]]>
</select>
<select id="getOrdersByAccountId" resultMap="orderResultMap">
<![CDATA[
select o.* from Account a,orderinfo o where o.accountId = a.accountId and a.accountId=#value# ]]>
</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 block
e.printStackTrace();
}
分享到:
相关推荐
ibatis 一对多关系映射 ibatis 一对多关系映射ibatis 一对多关系映射
ibatis实战之一对多关联源代码 文章地址:http://blog.csdn.net/itmyhome1990/article/details/30050183
IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习
ibatis配置多表关联(一对一、一对多、多对多
6.iBatis2学习笔记:一对多映射(双向).doc 7.iBatis2学习笔记:多对多映射(双向) .doc 8.iBatis2学习笔记:总结与思考.doc 9.iBatis2实体状态图解.doc 10.iBatis insert操作陷阱.doc 每章都有小例子。 呵呵,希望...
利用ibatis实现一对一、多对一的示例代码。每种关系用两种方式实现,具体的讲解可看我的博客http://blog.csdn.net/duwenchao1986/article/details/8565386
一份非常详细的讲解文档,从建表到Ibatis配置文件,再到dao代码全部都有
NULL 博文链接:https://keer2345.iteye.com/blog/673391
NULL 博文链接:https://self4j.iteye.com/blog/906319
ibatis的的增删改查和一对一、一对多查询 ibatis的的增删改查和一对一、一对多查询 ibatis的的增删改查和一对一、一对多查询 完成的项目
ibatis 完美例子 一对多 批处理 事务 和 spring struts2集成 ,一朵多 插入1万条数据,不到2秒,备注不包含类库
Ibatis一对一映射提示,需要学习的同学请关注,谢谢。
ibatis开发指南 ibatis资料文档 ibatis2.x_jar包 ibatis源码 ibatis实践项目
ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记
ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南ibatis2指南
ibatis学习 ibatis总结 ibatis ibatis ibatis
iBATIS入门学习
ibatis学习总结,oracle 学习总结,大数据量处理
iBATIS学习总结
ibatis 一对多关系_longjia_zt_新浪博客.mht