一年前寫過一篇關於省市區三級聯動的,當時的那篇 省 市 區 分爲了三張表,但現實工作中,往往是單張表 ,然後自己查自己.
現在我以這樣的情況重寫寫一篇.
先看看錶結構
這裏我們關注主鍵Id,名稱以及父級Id
在這裏我們能看到,我們表中 將 省 市 區/縣 都寫在了一張表內,通過父級Id建立連接.
實體類根據Mybatis逆向工程來生成,除了表中屬性外 添加一個額外的屬性 List childrenList;
public class ShoppingArea {
//主鍵Id
private Long id;
private Date addtime;
private Boolean deletestatus;
private String areaname;
private Integer level;
private Integer sequence;
//父級Id
private Long parentId;
private Boolean common;
//子級列表 非表屬性
private List<ShoppingArea> childrenList;
//getter
//setter
}
在逆向工程生成的Dao層接口mapper.java如下:
public interface ShoppingAreaMapper {
int deleteByPrimaryKey(Long id);
int insert(ShoppingArea record);
int insertSelective(ShoppingArea record);
ShoppingArea selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(ShoppingArea record);
int updateByPrimaryKey(ShoppingArea record);
//根據父級id找子級
List<ShoppingArea> selectByParentId(Long parentId);
//查詢所有
List<ShoppingArea> selectAll();
}
mapper.xml如下:
<mapper namespace="com.test.dao.ShoppingAreaMapper" >
<resultMap id="BaseResultMap" type="com.test.entity.ShoppingArea" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="addTime" property="addtime" jdbcType="TIMESTAMP" />
<result column="deleteStatus" property="deletestatus" jdbcType="BIT" />
<result column="areaName" property="areaname" jdbcType="VARCHAR" />
<result column="level" property="level" jdbcType="INTEGER" />
<result column="sequence" property="sequence" jdbcType="INTEGER" />
<result column="parent_id" property="parentId" jdbcType="BIGINT" />
<result column="common" property="common" jdbcType="BIT" />
</resultMap>
<resultMap id="BaseResultMapProvenice" type="com.test.entity.ShoppingArea" extends="BaseResultMap" >
<collection property="childrenList" ofType="com.test.entity.ShoppingArea" column="id"
select="com.test.dao.ShoppingAreaMapper.selectByParentIdOfCity" javaType="java.util.ArrayList" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="addTime" property="addtime" jdbcType="TIMESTAMP" />
<result column="deleteStatus" property="deletestatus" jdbcType="BIT" />
<result column="areaName" property="areaname" jdbcType="VARCHAR" />
<result column="level" property="level" jdbcType="INTEGER" />
<result column="sequence" property="sequence" jdbcType="INTEGER" />
<result column="parent_id" property="parentId" jdbcType="BIGINT" />
<result column="common" property="common" jdbcType="BIT" />
</collection>
</resultMap>
<resultMap id="BaseResultMapCity" type="com.test.entity.ShoppingArea" extends="BaseResultMap" >
<collection property="childrenList" ofType="com.test.entity.ShoppingArea" column="id"
select="com.test.dao.ShoppingAreaMapper.selectByParentIdOfArea" javaType="java.util.ArrayList" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="addTime" property="addtime" jdbcType="TIMESTAMP" />
<result column="deleteStatus" property="deletestatus" jdbcType="BIT" />
<result column="areaName" property="areaname" jdbcType="VARCHAR" />
<result column="level" property="level" jdbcType="INTEGER" />
<result column="sequence" property="sequence" jdbcType="INTEGER" />
<result column="parent_id" property="parentId" jdbcType="BIGINT" />
<result column="common" property="common" jdbcType="BIT" />
</collection>
</resultMap>
<sql id="Base_Column_List" >
id, addTime, deleteStatus, areaName, level, sequence, parent_id, common
</sql>
<select id="selectAll" resultMap="BaseResultMapProvenice" >
select
<include refid="Base_Column_List" />
from shopping_area
where parent_id is null
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMapProvenice" >
select
<include refid="Base_Column_List" />
from shopping_area
where id = #{id,jdbcType=BIGINT}
</select>
<select id="selectByParentIdOfCity" resultMap="BaseResultMapCity" >
select
<include refid="Base_Column_List" />
from shopping_area
where parent_id = #{parentId,jdbcType=BIGINT}
</select>
<select id="selectByParentIdOfArea" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from shopping_area
where parent_id = #{parentId,jdbcType=BIGINT}
</select>
看上圖即可.
測試代碼:
結果如下:
打斷點,直接看生成的結果
查詢所有:
獲取單個省級實體