MyBatis递归查询菜单项

建库SQL(仿照医保模拟的数据)

CREATE TABLE `columns` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',
  `nameZH` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '栏目中文名称',
  `nameEN` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '栏目英文名称',
  `parentID` int(11) NOT NULL DEFAULT '0' COMMENT '栏目的父级栏目ID(0为一级标题,其他的标题就对应其父标题的id)',
  `marker` varchar(256) COLLATE utf8_bin DEFAULT NULL COMMENT '备注',
  `department` int(11) DEFAULT NULL COMMENT '资料准备责任科室ID',
  `isDelete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否被删除(0没有删除,1已被删除)',
  `column1` varchar(2) COLLATE utf8_bin DEFAULT NULL COMMENT '备用字段1',
  `column2` varchar(2) COLLATE utf8_bin DEFAULT NULL COMMENT '备用字段2',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

/*Data for the table `columns` */

insert  into `columns`(`id`,`nameZH`,`nameEN`,`parentID`,`marker`,`department`,`isDelete`,`column1`,`column2`) values
(1,'医保资讯11','Medical insurance information',0,NULL,NULL,0,NULL,NULL),
(2,'医保服务11','Medical insurance service',0,NULL,NULL,0,NULL,NULL),
(3,'意见反馈11','Feedback',0,NULL,NULL,0,NULL,NULL),
(4,'机构设置22','Institutional setup',1,NULL,NULL,0,NULL,NULL),
(5,'医保新闻22','Medical Insurance News',1,NULL,NULL,0,NULL,NULL),
(6,'政策法规22','Policies and regulations',1,NULL,NULL,0,NULL,NULL),
(7,'党建22','party building',1,NULL,NULL,0,NULL,NULL),
(8,'公示公告22','Public announcement',1,NULL,NULL,0,NULL,NULL),
(9,'经办流程','Handling process',2,NULL,NULL,0,NULL,NULL),
(10,'定点机构','Designated organization',2,NULL,NULL,0,NULL,NULL),
(11,'医药服务价格','Medical service price',2,NULL,NULL,0,NULL,NULL),
(12,'参保缴费指南','Insurance payment Guide',2,NULL,NULL,0,NULL,NULL),
(13,'网上办理(城乡居民参保登记)','Online processing(Insurance registration of urban and rural resi',2,NULL,NULL,0,NULL,NULL),
(14,'群众信箱','Mass mailbox',3,NULL,NULL,0,NULL,NULL),
(15,'交通地图','Traffic map',3,NULL,NULL,0,NULL,NULL),
(16,'联系我们','contact us',3,NULL,NULL,0,NULL,NULL),
(17,'机构职能','Institutional functions',4,'内容展示',NULL,0,NULL,NULL),
(18,'局机关科室','Department of Bureau',4,NULL,NULL,0,NULL,NULL),
(19,'医保服务中心','Medical insurance service center',4,NULL,NULL,0,NULL,NULL),
(20,'国家级','national level',6,'新闻类',NULL,0,NULL,NULL),
(21,'省级','provincial level',6,NULL,NULL,0,NULL,NULL),
(22,'市级','Municipal level',6,NULL,NULL,0,NULL,NULL),
(23,'定点医院','Designated hospital',10,'内容展示',NULL,0,NULL,NULL),
(24,'定点药店','Designated drugstore',10,NULL,NULL,0,NULL,NULL),
(25,'慢性病定点药店','Designated drugstores for chronic diseases',10,NULL,NULL,0,NULL,NULL),
(26,'居民首次参保','Residents first insured',13,NULL,NULL,0,NULL,NULL),
(27,'新生儿参保','Neonatal insurance',13,NULL,NULL,0,NULL,NULL),
(28,'参保暂停','Suspension of insurance',13,NULL,NULL,0,NULL,NULL),
(29,'参保恢复','Insured recovery',13,NULL,NULL,0,NULL,NULL),
(30,'参保终止','Termination of insurance',13,NULL,NULL,0,NULL,NULL),
(31,'信息变更','Information change',13,NULL,NULL,0,NULL,NULL);

java实体类

public class Columns {
    private Integer id;
    private String nameZH;
    private String nameEN;
    private Integer parentID;
    private String marker;
    private Integer department;
    private Boolean isDelete;
    private String column1;
    private String column2;
    private List<Columns> children;//这个集合不是数据库的字段,是用来存子类对象的
    //省略getter setter方法

Mapper.xml



<mapper namespace="xyz.nieqiang.webapp.mapper.ColumnsMapper">
	<resultMap id="BaseResultMap" type="xyz.nieqiang.webapp.entity.Columns">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="nameZH" jdbcType="VARCHAR" property="nameZH" />
		<result column="nameEN" jdbcType="VARCHAR" property="nameEN" />
		<result column="parentID" jdbcType="INTEGER" property="parentID" />
		<result column="marker" jdbcType="VARCHAR" property="marker" />
		<result column="department" jdbcType="INTEGER" property="department" />
		<result column="isDelete" jdbcType="BIT" property="isDelete" />
		<result column="column1" jdbcType="VARCHAR" property="column1" />
		<result column="column2" jdbcType="VARCHAR" property="column2" />
	resultMap>
	
	<resultMap id="ColumnTree" type="xyz.nieqiang.webapp.entity.Columns">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="nameZH" jdbcType="VARCHAR" property="nameZH" />
		<result column="nameEN" jdbcType="VARCHAR" property="nameEN" />
		<result column="parentID" jdbcType="INTEGER" property="parentID" />
		<result column="marker" jdbcType="VARCHAR" property="marker" />
		<result column="department" jdbcType="INTEGER" property="department" />
		<result column="isDelete" jdbcType="BIT" property="isDelete" />
		<result column="column1" jdbcType="VARCHAR" property="column1" />
		<result column="column2" jdbcType="VARCHAR" property="column2" />
		<collection property="children" ofType="xyz.nieqiang.webapp.entity.Columns" column="id" select="getFindAllByParentID"/>
	resultMap>
	
	<sql id="Base_Column_List">
		id, nameZH, nameEN, parentID, marker, department, isDelete, column1, column2
	sql>

	<select id="findAll" resultMap="ColumnTree">
		SELECT * FROM `columns` WHERE parentID = 0
	select>
	
	<select id="getFindAllByParentID" parameterType="java.lang.Integer" resultMap="ColumnTree">
		SELECT * FROM `columns` WHERE parentID = #{id}
	select>
mapper>

Mapper.java

@Mapper
public interface ColumnsMapper {
    List<Columns> findAll();
}

此时的findAll接口中的List集合就装的是递归出来的菜单项

你可能感兴趣的:(MyBatis,Springboot,笔记,java,spring,boot)