一、问题描述
将若依框架搭建好之后需要将自己的树形结构表以树形的方式呈现出来,使用"代码生成"将代码生成出来。结果点击改路由显示报错:
Error attempting to get column 'parent_id' from result set. Cause: java.sql.SQLDataException: Cannot determine value type from string 'root' ; Cannot determine value type from string 'root'; nested exception is java.sql.SQLDataException: Cannot determine value type from string 'root'
于是我就参考若依项目的其它已有的表(如:sys_menu),将根节点id改为'0'
接着就出现另外一个报错:
Error attempting to get column 'parent_id' from result set. Cause: java.sql.SQLDataException: Value '10,000,000,000,000,004,069' is outside of valid range for type java.lang.Long ; Value '10,000,000,000,000,004,069' is outside of valid range for type java.lang.Long; nested exception is java.sql.SQLDataException: Value '10,000,000,000,000,004,069' is outside of valid range for type java.lang.Long
二、解决方法
通过观察发现实体类会继承一个叫TreeEntity的类,如下:
package com.ruoyi.common.core.web.domain;
import java.util.ArrayList;
import java.util.List;
/**
* Tree基类
*
* @author ruoyi
*/
public class TreeEntity extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 父菜单名称 */
private String parentName;
/** 父菜单ID */
private Long parentId;
/** 显示顺序 */
private Integer orderNum;
/** 祖级列表 */
private String ancestors;
/** 子部门 */
private List<?> children = new ArrayList<>();
public String getParentName()
{
return parentName;
}
public void setParentName(String parentName)
{
this.parentName = parentName;
}
public Long getParentId()
{
return parentId;
}
public void setParentId(Long parentId)
{
this.parentId = parentId;
}
public Integer getOrderNum()
{
return orderNum;
}
public void setOrderNum(Integer orderNum)
{
this.orderNum = orderNum;
}
public String getAncestors()
{
return ancestors;
}
public void setAncestors(String ancestors)
{
this.ancestors = ancestors;
}
public List<?> getChildren()
{
return children;
}
public void setChildren(List<?> children)
{
this.children = children;
}
}
发现其中的父菜单id是Long类型,而我引入的表的父id是String类型,且长度超过了Long的范围(猜测)。于是我就索性单独重新写了一个类,将父菜单id改为String类型,如下:
package com.ruoyi.common.core.web.domain;
import java.util.ArrayList;
import java.util.List;
/**
* @author 立即推
* @version 1.0
*/
public class TreeStringIdEntity extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 父菜单名称 */
private String parentName;
/** 父菜单ID */
private String parentId;
/** 显示顺序 */
private Integer orderNum;
/** 祖级列表 */
private String ancestors;
/** 子部门 */
private List<?> children = new ArrayList<>();
public String getParentName()
{
return parentName;
}
public void setParentName(String parentName)
{
this.parentName = parentName;
}
public String getParentId()
{
return parentId;
}
public void setParentId(String parentId)
{
this.parentId = parentId;
}
public Integer getOrderNum()
{
return orderNum;
}
public void setOrderNum(Integer orderNum)
{
this.orderNum = orderNum;
}
public String getAncestors()
{
return ancestors;
}
public void setAncestors(String ancestors)
{
this.ancestors = ancestors;
}
public List<?> getChildren()
{
return children;
}
public void setChildren(List<?> children)
{
this.children = children;
}
}
然后让实体类继承此类TreeStringIdEntity,最终解决问题