mybatis一对多xml配置
Posted 彬彬无
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis一对多xml配置相关的知识,希望对你有一定的参考价值。
1.一的配置
<resultMap id="accListResultMap" type="com.hex.dop.pms.model.PMAccount">
<result column="PK_ID" property="pkId" jdbcType="VARCHAR"/>
<result column="ACCOUNT" property="account" jdbcType="VARCHAR"/>
<result column="ACCOUNTBANK" property="accountBank" jdbcType="VARCHAR"/>
<result column="ACCOUNTBANKNAME" property="accountBankName" jdbcType="VARCHAR"/>
...
<result column="APPLYDATE" property="applyDate" jdbcType="TIMESTAMP"/>
<result column="STATUS" property="status" jdbcType="VARCHAR"/>
<collection property="expresss" column="PK_ID" select="getExpress"/>
</resultMap>
2.多的配置
<resultMap id="expressListResultMap" type="com.hex.dop.pms.model.Express">
<result column="ID" property="id" jdbcType="VARCHAR"></result>
<result column="SENDER" property="sender" jdbcType="VARCHAR"></result>
<result column="CONSIGNEE" property="consignee" jdbcType="VARCHAR"></result>
<result column="REMARK" property="remark" jdbcType="VARCHAR"></result>
<result column="UPONTIME" property="uponTime" jdbcType="TIMESTAMP"></result>
<result column="EXPRESSNUMBER" property="expressNumber" jdbcType="VARCHAR"></result>
<result column="EXPRESSCOMPANY" property="expressCompany" jdbcType="VARCHAR"></result>
</resultMap>
3.上面的collection标签保证了PK_ID相关联
<!--根据id查询快递信息-->
<select id="getExpress" parameterType="java.util.Map" resultMap="expressListResultMap">
select E.ID,E.SENDER,E.CONSIGNEE,E.REMARK,E.UPONTIME,E.EXPRESSNUMBER,E.EXPRESSCOMPANY
from SYW_ACPMS_EXPRESS E
where E.PK_ID=#{pkId}
</select>
4.PMAccount.java
private String pkId; //唯一编号
private List<Express> expresss = null;//每一个账户对应有多个快递
public List<Express> getExpresss() {
if(expresss==null){//如果为空,返回一个空的集合
return new ArrayList<Express>();
}
return expresss;
}
public void setExpresss(List<Express> expresss) {
this.expresss = expresss;
}
Express.java
private String pkId;//账户主键,建立逻辑上的连接关系
5.control层:
/*
* 根据id查出快递信息返回
* */
@RequestMapping(value ="/express")
@ResponseBody
public Map getAccExpress(@RequestBody PMAccount acc){
Map map = new HashMap();
try{
acc = this.accountManagerService.getAcc(acc.getPkId());//将account传入,查询快递信息
List list = acc.getExpresss();//获得快递集合
map.put("data",list);//map返回的是数组,所以必须在前台使用encode都的对象[‘data‘]才行
}catch(Exception e){
e.printStackTrace();
}
return map;
}
service层:
public PMAccount getAcc(String id){
return this.pmAccountMapper.getAcc(id);
}
dao层:
public PMAccount getAcc(String id){
Map map = new HashMap<String,String>();
map.put("pkId", id);//这里是将前台传入的值放入map中
PMAccount acc = (PMAccount)sqlSessionTemplate.selectList("com.hex.dop.pms.dao.acc.PMAccountMapper.getAcc", map).get(0);
return acc;
}
control层直到调用dao层后,返回出查询的账户对象,然后账户对象中获得快递对象(然后传递pkid,自动调用collection标签中select中的语句,都是collection标签的作用),这样便获得查询出的快递对象,当然还需要acc.getExpresss()从账户对象中取出来。
以上是关于mybatis一对多xml配置的主要内容,如果未能解决你的问题,请参考以下文章