Tree structure entity class:
@data
@Equalsandhashcode (callsuper = false)
@Accessors (chain = true)
@Tablename ("Area_info")
public class areainfo {
// Administrative area number
@Tableid (value = "Area_no", Type = IDType.id_Worker_Str)
Private String Areano;
// Administrative area name
@Tablefield ("Area_name")
Private String Areaname;
// Father Area Number
@Tablefield ("Parent_area_no")
Private String Parentareano;
// Remark
@Tablefield ("Remark")
Private String Remark;
// Status (0-invalid; 1-valid)
@Tablefield ("STATUS")
Private String Status;
// child elements
@Tablefield (exist = false)
Private list <MCSAREAINFO> Children;
}
database table mechanism is as follows:
Create table `rea_info` (
`rea_no` varchar (32) not null comments 'administrative area number',
`rea_name` varchar (128) default null comment 'administrative area name',
`Parent_area_no` varchar (32) default null comments
`Remark` varchar (512) default null comments 'remarks',
`Status` varchar (8) default null comments (0-invalid; 1-valid) ',, valid)',
Primary Key (`rea_no`)
) ENGINE = Innodb Default Charset = UTF8 Comment = 'Area Table';
java dao is as follows:
@mapper
Public Interface Areainfodao Extends Basemapper <reainfo> {
// Get the area tree menu
Public list <reainfo> getareainfotree (String Parentareano);
}
mybatis SQL as follows:
<resultMap type="com.AreaInfo" id="areaInfoResult">
<id column="AREA_NO" property="areaNo"/>
<result column="AREA_NAME" property="areaName"/>
<result column="STATUS" property="status"/>
<collection column="AREA_NO" property="children" ofType="com.AreaInfo"
select="getAreaInfoTree"></collection> </resultMap>
<select id="getAreaInfoTree" resultMap="areaInfoResult" parameterType="string">
select AREA_NO,AREA_NAME,PARENT_AREA_NO,STATUS from AREA_INFO where
PARENT_AREA_NO=#{parentAreaNo}
</select>
Instructions:This query method is only suitable for a small amount of data query. If the amount of data is too large, the pressure of the database is particularly large, so it is not recommended to use it. For a large amount of tree data, you can use a lazy loading method or directly query the list and hand it over to the previous paragraph to the previous paragraph. Do it yourself.