mybatis 一对多

多对一、一对多

表:多方表添加外键

对象:一方对象(List)

         :多方对象(一方对象属性)

映射信息:

一方:<resultMap>

<association  resultMap></associatioan>

多方

<resultMap》

《collection  resultMap><collection>

------------------------------------------------------------

package com.entity;


import java.util.List;


public class Districe {


/*// did   NUMBER(5) not null,
  dname VARCHAR2(50)*/
private   long did;
private String dname;
private List<Street>  st;



public List<Street> getSt() {
return st;
}
public void setSt(List<Street> st) {
this.st = st;
}
public Districe(long did, String dname) {
super();
this.did = did;
this.dname = dname;
}
public Districe() {
super();
---------------------------------------





}

-------------------------------------------------

package com.entity;


public class Street {

/* id          NUMBER(5) not null,
  name        VARCHAR2(50),
  district_id NUMBER(5)*/
private   long id;
private String name;
private long   district_id;
private Districe dis;
public Street(long id, String name, long district_id, Districe dis) {
super();
this.id = id;
this.name = name;
this.district_id = district_id;
this.dis = dis;
}
public Street() {
super();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

}

.........................................................<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper  namespace="com.dao.StreetDao">
 <resultMap type="Street" id="streetmap">
        <id property="id" column="id"/>
        <result property="name" column="name"/>  
        <result property="district_id" column="district_id"/>  
        <association property="dis"  resultMap="districemap"></association>
      
   </resultMap>


<resultMap type="Districe" id="districemap">
        <id property="did" column="did" />
        <result property="dname" column="dname" />  
       <collection property="st" resultMap="streetmap"></collection>
</resultMap> 




<select id="getAllStreet"  resultMap="streetmap">
select s.*,t.*
from street s,DISTRICT t
where s.district_id=t.did


</select>
<!--  public  List<Districe>  getAllDis();-->
<select id="getAllDis" resultMap="districemap">
select s.*,t.*
from street s,DISTRICT t
where s.district_id=t.did
</select>




</mapper>..................................................................

package com.dao;


import java.util.List;


import com.entity.Districe;
import com.entity.Street;


public interface StreetDao {

public List<Street>  getAllStreet();

public  List<Districe>  getAllDis();


}

---------------------------------------------------

package com.impl;


import java.util.List;


import com.dao.IHouseDao;
import com.dao.StreetDao;
import com.entity.Districe;
import com.entity.House;
import com.entity.Street;
import com.util.MybatisUtil;


public class StreetDaoImpl  implements  StreetDao {


@Override
public List<Street> getAllStreet() {
// TODO Auto-generated method stub
StreetDao dao=MybatisUtil.getSqlSessetion1().getMapper(StreetDao.class);
List<Street> streets=dao.getAllStreet();
return streets;

}


@Override
public List<Districe> getAllDis() {
StreetDao dao=MybatisUtil.getSqlSessetion1().getMapper(StreetDao.class);
List<Districe> dis=dao.getAllDis();
return dis;
}



}

-----------------------------------------------------------------------------

package com.test;


import java.util.List;


import com.dao.StreetDao;
import com.entity.Districe;
import com.entity.Street;
import com.impl.StreetDaoImpl;


public class StreetsDemo {
public static void main(String[] args) {
StreetDao dao=new StreetDaoImpl();
/* List<Street>  sts=dao.getAllStreet();
for (Street street : sts) {
System.out.println(street);
}*/
List<Districe>  dis= dao.getAllDis();
for (Districe districe : dis) {


for (Street street :  districe.getSt()) {
System.out.println(street);
}
}

}





}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值