java后台树形结构展示---懒加载

一、数据库设计

java后台树形结构展示---懒加载_第1张图片

 

 

 二、实体类:entity

import com.joyoung.cloud.security.common.validatedGroup.Add;
import com.joyoung.cloud.security.common.validatedGroup.Modify;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.persistence.Id;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
* 公司表(SysCompany)实体类
*
* @author yangyuwei
* @since 2019-06-06 13:45:23
*/
@ApiModel(value = "公司类")
@Data
@Accessors(chain = true)
public class SysCompany implements Serializable {
private static final long serialVersionUID = 600959061181555399L;
@ApiModelProperty(value = "主键Id")
@Id
@Null(message = "{add_id}", groups = {Add.class})
@NotNull(message = "{modify_id}", groups = {Modify.class})
private String id;
//上级公司id
@ApiModelProperty(value = "Id")
private String parentId;
//公司名称
@ApiModelProperty(value = "公司名称")
@NotBlank(message = "{name_null}")
@Pattern(regexp = "^[\\s\\S]{1,20}$", message = "{name_rule}")
private String coName;
//公司简称
@ApiModelProperty(value = "公司简称")
private String coAbbr;
//公司编号
@ApiModelProperty(value = "公司编号")
private String coCode;
//公司地址
@ApiModelProperty(value = "公司地址")
private String coAddr;
//邮编
@ApiModelProperty(value = "邮编")
@Pattern(regexp = "^$|[0-9]{6}$", message = "{zipCode_error}")
private String zipCode;
//法人名称
@ApiModelProperty(value = "法人名称")
private String legalName;
//法定代表人
@ApiModelProperty(value = "法定代表人")
private String legalRep;
//税号
@ApiModelProperty(value = "税号")
private String taxId;
//电话号码("XXX-XXXXXXX""XXXX-XXXXXXXX""XXX-XXXXXXX""XXX-XXXXXXXX""XXXXXXX""XXXXXXXX)
// @Pattern(regexp = "^(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{7,14}$", message = "请输入正确的电话号码")
@ApiModelProperty(value = "电话号码")
private String tel;
//开户银行名称
@ApiModelProperty(value ="开户银行名称")
private String bankName;
//开户银行账号
@ApiModelProperty(value = "开户银行账号")
private String bankAcct;
//创建人id
private String crtUser;
//创建人名称
private String crtName;
//创建时间
private Date crtTime;
//最后修改人id
private String updUser;
//最后修改人名称
private String updName;
//最后修改时间
private Date updTime;
//备注
private String remark;
//逻辑删除标识:1=删除;0=未删除
private Integer delFlag;

private String attr1;

private String attr2;

private String attr3;

/*****************************以下为添加字段*********************************/
@Transient
private Boolean hasChecked;
@Transient
private Boolean hasChildren;
@Transient
private List children = new LinkedList();

}

三、controller
@ApiOperation(value = "懒加载查询列表")
@GetMapping("/children")
public List getChildren(SysCompany sysCompany) {
return service.getChildren(sysCompany);
}
四、service

@Override
public List getChildren(SysCompany sysCompany) {
List companyList = dao.selectCompanyInfo(sysCompany);
if (StringUtils.isNotBlank(sysCompany.getCoName())) {
return getTree(sysCompany);
}
return companyList;
}


/**
这里一般将此方法放在entity里面
*/
private List getTree(SysCompany sysCompany) {
Map map = dao.selectByCompany(sysCompany);
List tree = new ArrayList<>();
map.forEach((id, obj) -> {
if (map.containsKey(obj.getParentId())) {
map.get(obj.getParentId()).getChildren().add(obj);
} else {
tree.add(obj);
}
});
return tree;
}
五、dao
public List selectCompanyInfo(SysCompany sysCompany);

六、mapper
<sql id="sys_param">
c.id,
c.parent_id,
c.co_name,
c.co_abbr,
c.co_code,
c.co_addr,
c.zip_code,
c.legal_name,
c.legal_rep,
c.tax_id,
c.tel,
c.bank_name,
c.bank_acct,
c.crt_user,
c.crt_name,
c.crt_time,
c.upd_user,
c.upd_name,
c.upd_time,
c.remark,
c.del_flag
sql>
<select id="selectCompanyInfo" resultMap="SysCompanyMap"
parameterType="com.joyoung.cloud.security.common.entity.admin.SysCompany">
SELECT
<include refid="sys_param"/>,EXISTS (SELECT 1 from sys_company sc where sc.parent_id=c.id and sc.del_flag=0) as
hasChildren
FROM
sys_company c
WHERE
c.del_flag = 0
<if test="coName!=null">
and c.co_name like concat ('%',#{coName},'%')
if>
<if test="parentId !=null and parentId !=''">
and c.parent_id = #{parentId}
if>
<if test="parentId == null || parentId == ''">
and c.parent_id is null
if>
select>
 

你可能感兴趣的:(java后台树形结构展示---懒加载)