1.前端完善
前端增加join关系输入框,并且只有在选择select的时候才显示到页面上。效果如下:
新增变量代表是否选择select
data(){
isSelect:false
}
单选组添加isSelect响应改变方法@change=“changeType”
<el-radio-group v-model="ruleForm.type" @change="changeType">
<el-radio label="insert"></el-radio>
<el-radio label="select"></el-radio>
</el-radio-group>
changeType(val){
if(val === 'select'){
this.isSelect = true;
}else {
this.isSelect = false;
}
}
join关系输入框通过v-if显示或隐藏
<el-form-item label="join关系" v-if="this.isSelect === true">
<el-input style="width: 800px" size="large" v-model="ruleForm.relation" ></el-input>
</el-form-item>
2.JSON格式添加字段
添加flag字段表示是否有join条件,添加join字段保存join条件。示例如下:
{"dbname":"student","chartsname":["tijian","HHA"],"metas":{"tijian":{"id":"int","sid":"int","des":"string"},
"HHA":{"SS":"TINYINT"}},"type":"select","key":"这里需填写密钥","flag":true,"join":"hh=hh"}
3.后端处理
public List<List<Object>> selectDataJ(String text){
UserJson userJson = JSONObject.parseObject(text,UserJson.class);
List<List<Object>> result = new ArrayList<>();
List<Object> lm = new ArrayList<>();//生成excel的列名
String str = "";//select ?
String cnames = "";//from ?
String condition = userJson.getJoin();//where?
for (List<PDetail> pDetails : this.chartnames2) {
for (PDetail pDetail : pDetails) {
lm.add(pDetail.getChartname() + "." + pDetail.getMeta());
str += pDetail.getChartname() + "." + pDetail.getMeta() + ",";
}
}
result.add(lm);
for (String s : this.chartnames1) {
cnames += s +",";
}
str = str.substring(0,str.length()-1);
cnames = cnames.substring(0,cnames.length()-1);
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
String jdbcUrl = "jdbc:mysql://localhost:3306/"+this.dbname+"?useSSL=false&serverTimezone=UTC";
try {
Class.forName("com.mysql.cj.jdbc.Driver");//————————————————————————————————————————————————————————————————
//创建连接
conn = DriverManager.getConnection(jdbcUrl,"root","jiamohan@599");//——————————————————————————
String sql = "SELECT " + str + " FROM " + cnames + " WHERE " + condition;
System.out.println(sql);
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while (rs.next()){
List<Object> data = new ArrayList<>();
for (Object o : lm) {
data.add(rs.getString(o.toString()));
}
result.add(data);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
rs.close();
psmt.close();
conn.close();
} catch (SQLException e){
e.printStackTrace();
}
}
return result;
}