Mapper
public interface ProvinceMapper {
public List<Province> getProvinceTree(Province province);
}
实体类
public class Province {
private String code;
private String belongCode;
private String provinceName;
@Transient // javax.persistence.Transient
private List<City> cityList;
... 省略set/get方法
}
public class City {
private String code;
private String belongCode;
private String cityName;
@Transient // javax.persistence.Transient
private List<County> countyList;
... 省略set/get方法
}
public class County {
private String code;
private String belongCode;
private String countyName;
... 省略set/get方法
}
第一种方式:在XML中多表关联查询
<?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.springboot.mapper.ProvinceMapper" >
<resultMap id="BaseProvinceResultMap" type="com.springboot.pojo.Province" >
<result column="code" property="code" jdbcType="VARCHAR" />
<result column="belong_code" property="belongCode" jdbcType="VARCHAR" />
<result column="province_name" property="provinceName" jdbcType="VARCHAR" />
<collection property="cityList" javaType="java.util.ArrayList" ofType="com.springboot.pojo.City" >
<result column="city_name" property="cityName" jdbcType="VARCHAR" />
<collection property="countyList" javaType="java.util.ArrayList" ofType="com.springboot.pojo.County" >
<result column="county_name" property="countyName" jdbcType="VARCHAR" />
</collection>
</collection>
</resultMap>
<select id="getProvinceTree" parameterType="com.springboot.pojo.Province" resultMap="BaseProvinceResultMap">
SELECT p2.province_name, city.city_name, county.county_name
FROM (SELECT p1.code, p1.province_name
FROM province p1
WHERE p1.belong_code = #{ belongCode }) p2
LEFT JOIN city
ON p2.code = city.belong_code
LEFT JOIN county
ON city.code = county.belong_code
</select>
</mapper>
第二种方式:使用mybatis的collection递归查询
<?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.springboot.mapper.ProvinceMapper" >
<resultMap id="BaseProvinceTreetMap" type="com.springboot.pojo.Province" >
<result column="code" property="code" jdbcType="VARCHAR" />
<result column="belong_code" property="belongCode" jdbcType="VARCHAR" />
<result column="province_name" property="provinceName" jdbcType="VARCHAR" />
<collection column="code" property="cityList" javaType="java.util.ArrayList" ofType="com.springboot.pojo.City" select="getCityTree" />
</resultMap>
<resultMap id="BaseCityTreeMap" type="com.springboot.pojo.City" >
<result column="code" property="code" jdbcType="VARCHAR" />
<result column="belong_code" property="belongCode" jdbcType="VARCHAR" />
<result column="city_name" property="cityName" jdbcType="VARCHAR" />
<collection column="code" property="countyList" javaType="java.util.ArrayList" ofType="com.springboot.pojo.County" select="getCountyTree" />
</resultMap>
<resultMap id="BaseCountyTreeMap" type="com.springboot.pojo.County" >
<result column="code" property="code" jdbcType="VARCHAR" />
<result column="belong_code" property="belongCode" jdbcType="VARCHAR" />
<result column="county_name" property="countyName" jdbcType="VARCHAR" />
</resultMap>
<select id="getProvinceTree" parameterType="com.springboot.pojo.Province" resultMap="BaseProvinceTreetMap">
SELECT code,belong_code,province_name FROM province WHERE belong_code = #{ belongCode }
</select>
<select id="getCityTree" resultMap="BaseCityTreeMap">
SELECT code,belong_code,city_name FROM city WHERE belong_code = #{ belongCode }
</select>
<select id="getCountyTree" resultMap="BaseCountyTreeMap">
SELECT code, belong_code, county_name FROM county WHERE belong_code = #{ belongCode }
</select>
</mapper>