多对一、一对多
表:多方表添加外键
对象:一方对象(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);
}
}
}
}