双表联动
作者:互联网
一、后端代码
-
service层
// 左表返回的内容
public QdbResult queryDb(Long id,Integer page, Integer limit) throws Exception {
SrcDataSource srcDataSource = srcDataSourceMapper.selectById(id);
SimpleDataSource source = dataSource(srcDataSource.getSourceType(),
srcDataSource.getJdbcUrl(), srcDataSource.getJdbcUser(), srcDataSource.getJdbcPwd());
// 根据url,user,pwd,type获得该数据库内的所有表的表名和字段信息
DBMetadataUtils metaUtils = new DBMetadataUtils(source);
DatabaseConfig config = metaUtils.getDefaultConfig();
List<QdbSource> l = new ArrayList<>();
List<IntrospectedTable> list = metaUtils.introspectTables(config);
for (IntrospectedTable table : list) {
QdbSource qdbSource = new QdbSource();
qdbSource.setId(id);
qdbSource.setName(table.getName());
List<QdbField> lq = new ArrayList<>();
for (IntrospectedColumn column : table.getAllColumns()) {
QdbField qdbField = new QdbField();
qdbField.setFildName(column.getName());
qdbField.setFildType(column.getJdbcTypeName());
qdbField.setFildLength(column.getLength());
qdbField.setFildScale(column.getScale());
qdbField.setRemarks(column.getRemarks());
qdbField.setIsPk(column.isPk());
qdbField.setIsNullable(column.isNullable());
qdbField.setDefaultValue(column.getDefaultValue());
lq.add(qdbField);
}
qdbSource.setList(lq);
l.add(qdbSource);
}
QdbResult result = new QdbResult();
result.setCode(0);
result.setCount(10l);
result.setMsg("成功");
result.setData(l);
return result;
}
// 右表返回的内容
public QdbFieldResult queryField(Long id, String name, Integer page, Integer limit) throws Exception {
SrcDataSource srcDataSource = srcDataSourceMapper.selectById(id);
SimpleDataSource source = dataSource(srcDataSource.getSourceType(),
srcDataSource.getJdbcUrl(), srcDataSource.getJdbcUser(), srcDataSource.getJdbcPwd());
// 根据url,user,pwd,type获得该数据库内的所有表的表名和字段信息
DBMetadataUtils metaUtils = new DBMetadataUtils(source);
DatabaseConfig config = metaUtils.getDefaultConfig();
List<QdbField> list1 =new ArrayList<>();
List<IntrospectedTable> list = metaUtils.introspectTables(config);
for (IntrospectedTable table : list) {
if (name.equals(table.getName())){
for (IntrospectedColumn column : table.getAllColumns()) {
QdbField qdbField = new QdbField();
qdbField.setFildName(column.getName());
qdbField.setFildType(column.getJdbcTypeName());
qdbField.setFildLength(column.getLength());
qdbField.setFildScale(column.getScale());
qdbField.setRemarks(column.getRemarks());
qdbField.setIsPk(column.isPk());
qdbField.setIsNullable(column.isNullable());
qdbField.setDefaultValue(column.getDefaultValue());
list1.add(qdbField);
}
}
}
QdbFieldResult result = new QdbFieldResult();
result.setCode(0);
result.setCount(10l);
result.setMsg("成功");
result.setData(list1);
return result;
}
-
VO层
@Data
public class QdbResult implements Serializable {
private static final long serialVersionUID = 1L;
private Integer code;
private String msg;
private Long count;
private List<QdbSource> data;
}
@Data
public class QdbSource implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private List<QdbField> list;
} -
实体类
@Data
//表中的字段信息
public class QdbField implements Serializable {
private static final long serialVersionUID = 1L;
private String fildName;
private String fildType;
private Integer fildLength;
private Integer fildScale;
private String remarks;
private Boolean isPk;
private Boolean isNullable;
private String defaultValue;
}
-
控制层
/**
* 跳转查看库表库表
*
* @author Jacob
* @Date 2021-07-01
*/
@RequestMapping("/checkDb")
public String checkDb() {
return PREFIX + "/qdbDataSource2.html";
}
// 左表返回表名
@RequestMapping("/checkDbLeft")
@ResponseBody
public QdbResult checkDbLeft(Long id,Integer page, Integer limit) {
try {
QdbResult qdbResult =srcDataSourceService.queryDb(id,page,limit);
return qdbResult;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// 右表根据左表的表明返回相应的字段信息
@RequestMapping("/checkDbRight")
@ResponseBody
public QdbFieldResult checkDbRight(Long id, String name, Integer page, Integer limit) {
try {
QdbFieldResult result=srcDataSourceService.queryField(id,name,page,limit);
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} -
mapper层略
二、前端代码
-
左表js
layui.use(['table', 'admin', 'ax'], function () {
var $ = layui.$;
var table = layui.table;
var $ax = layui.ax;
var admin = layui.admin;
var QdbDataSourceTable = {
tableId: "qdbDataSourceTable"
};
QdbDataSourceTable.initColumn = function () {
return [[
{type: 'checkbox'},
{field: 'id',sort: true,hide: true,title: 'id'},
{field: 'name', sort: true, title: '库表名称'},
{align: 'center', toolbar: '#tableBar', title: '操作'}
]];
};
// 渲染表格
var tableResult = table.render({
elem: '#' + QdbDataSourceTable.tableId,
url: Feng.ctxPath + "/srcDataSource/checkDbLeft?id=" + Feng.getUrlParam("id"),
page: true,
height: "full-158",
cellMinWidth: 100,
cols: QdbDataSourceTable.initColumn()
});
-
右表js
var QdbFieldSourceTable = {
tableId: "qdbFieldSourceTable"
};
QdbFieldSourceTable.initColumn = function () {
return [[
{type: 'checkbox'},
{field: 'fildName', sort: true, title: '字段名'},
{field: 'fildType', sort: true, title: '数据类型'},
{field: 'fildLength', sort: true, title: '字段长度'},
{field: 'fildScale', sort: true, title: '小数位数'},
{field: 'remarks', sort: true, title: '备注'},
{field: 'isPk', sort: true, title: '是否主键'},
{field: 'isNullable', sort: true, title: '是否可用'},
{field: 'defaultValue', sort: true, title: '默认值'},
]];
};
var tableResult2 = table.render({
elem: '#' + QdbFieldSourceTable.tableId,
url: Feng.ctxPath + "/srcDataSource/checkDbRight?id="+1+"&name=sys_dict",
page: true,
height: "full-158",
cellMinWidth: 100,
cols: QdbFieldSourceTable.initColumn()
});
/*
* 查看字段
* */
QdbDataSourceTable.checkField1 = function (data) {
table.reload('qdbFieldSourceTable',{
url: Feng.ctxPath + "/srcDataSource/checkDbRight?",
page:{ curr:1 },
where:{
'id':data.id,
'name':data.name
}
});
};
// 工具条点击事件
table.on('tool(' + QdbDataSourceTable.tableId + ')', function (obj) {
var data = obj.data;
var layEvent = obj.event;
if (layEvent === 'checkField') {
QdbDataSourceTable.checkField1(data);
}
});三、实现效果
标签:result,column,private,srcDataSource,双表,联动,id,qdbField 来源: https://www.cnblogs.com/hippo-dolphin/p/14966280.html