提交 944ce68f authored 作者: ww1xhqc's avatar ww1xhqc

[数据模型] 新增自定义SQL语句查询接口

上级 f3b18506
...@@ -55,7 +55,7 @@ public class WebMvcConfig { ...@@ -55,7 +55,7 @@ public class WebMvcConfig {
private ApiInfo apiInfo() { private ApiInfo apiInfo() {
return new ApiInfoBuilder() return new ApiInfoBuilder()
.title("excel->操作表") .title("excel->表")
.description("excel 转化的实体类") .description("excel 转化的实体类")
.version("1.0") .version("1.0")
.build(); .build();
......
...@@ -178,4 +178,16 @@ public class ModelController { ...@@ -178,4 +178,16 @@ public class ModelController {
"获得example成功!"); "获得example成功!");
} }
@ApiOperation("自定义SQL查询")
@PostMapping("/sql")
public ResponseEntity executeSql(String sql){
try {
return ResultUtil.success(modelService.executeQuery(sql),"查询成功!");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return ResultUtil.failed("SQL错误,查询失败!");
}
} }
...@@ -104,5 +104,5 @@ public interface ModelService { ...@@ -104,5 +104,5 @@ public interface ModelService {
*/ */
TableAndColumnInfoVO getTableInfoAndColumnInfoByBatch(Integer[] ids); TableAndColumnInfoVO getTableInfoAndColumnInfoByBatch(Integer[] ids);
List<Map<String, Object>> executeQuery(String sql) throws SQLException;
} }
...@@ -17,6 +17,10 @@ public interface QuoteService { ...@@ -17,6 +17,10 @@ public interface QuoteService {
*/ */
void updateQuote(Integer id); void updateQuote(Integer id);
List<Quote> getAll(); List<Quote> getAllQuote();
Quote saveQuote(Quote quote);
void delQuote(Integer id);
} }
...@@ -36,7 +36,6 @@ import org.springframework.jdbc.core.JdbcTemplate; ...@@ -36,7 +36,6 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.persistence.criteria.Path; import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import java.lang.reflect.Field; import java.lang.reflect.Field;
...@@ -119,7 +118,7 @@ public class ModelImpl implements ModelService { ...@@ -119,7 +118,7 @@ public class ModelImpl implements ModelService {
* @Date 16:15 2021/3/5 * @Date 16:15 2021/3/5
**/ **/
@Override @Override
public List<ColumnInfo> showModelFields(SearchColumnInfoVo searchColumnInfoVo) { public List<ColumnInfo> showModelFields(SearchColumnInfoVo searchColumnInfoVo) {
PredicateBuilder<ColumnInfo> and = Specifications.and(); PredicateBuilder<ColumnInfo> and = Specifications.and();
and.eq(searchColumnInfoVo.getDbId() != null, "dbId", searchColumnInfoVo.getDbId()); and.eq(searchColumnInfoVo.getDbId() != null, "dbId", searchColumnInfoVo.getDbId());
and.eq(searchColumnInfoVo.getDbName() != null && StringUtils.isNotEmpty(searchColumnInfoVo.getDbName()), "dbName", searchColumnInfoVo.getDbName()); and.eq(searchColumnInfoVo.getDbName() != null && StringUtils.isNotEmpty(searchColumnInfoVo.getDbName()), "dbName", searchColumnInfoVo.getDbName());
...@@ -154,7 +153,7 @@ public class ModelImpl implements ModelService { ...@@ -154,7 +153,7 @@ public class ModelImpl implements ModelService {
.stream() .stream()
.map(TableInfo::getModelName) .map(TableInfo::getModelName)
.collect(Collectors.toList()); .collect(Collectors.toList());
if (collect.contains(tableVO.getModelName())){ if (collect.contains(tableVO.getModelName())) {
throw new ApiException("表名已经存在!"); throw new ApiException("表名已经存在!");
} }
TableInfo tableInfo = new TableInfo(); TableInfo tableInfo = new TableInfo();
...@@ -166,7 +165,7 @@ public class ModelImpl implements ModelService { ...@@ -166,7 +165,7 @@ public class ModelImpl implements ModelService {
tableInfo.setParentTable(parentTable); tableInfo.setParentTable(parentTable);
tableInfoDao.save(tableInfo); tableInfoDao.save(tableInfo);
//默认存储ID字段 //默认存储ID字段
if (tableInfo.getModelType().equals(ModelType.VIRTUAL)){ if (tableInfo.getModelType().equals(ModelType.VIRTUAL)) {
ColumnInfo cId = new ColumnInfo(0, "id", "主键", "java.lang.Integer", 11, tableInfo.getModelName(), tableInfo.getId(), "主键"); ColumnInfo cId = new ColumnInfo(0, "id", "主键", "java.lang.Integer", 11, tableInfo.getModelName(), tableInfo.getId(), "主键");
columnInfoDao.save(cId); columnInfoDao.save(cId);
} }
...@@ -281,7 +280,6 @@ public class ModelImpl implements ModelService { ...@@ -281,7 +280,6 @@ public class ModelImpl implements ModelService {
} }
/** /**
* @param tableName * @param tableName
* @return boolean * @return boolean
...@@ -297,7 +295,7 @@ public class ModelImpl implements ModelService { ...@@ -297,7 +295,7 @@ public class ModelImpl implements ModelService {
}; };
List<TableInfo> all = tableInfoDao.findAll(spec); List<TableInfo> all = tableInfoDao.findAll(spec);
if (all!=null&&all.size()>0){ if (all != null && all.size() > 0) {
return all.get(0); return all.get(0);
} }
return null; return null;
...@@ -544,4 +542,14 @@ public class ModelImpl implements ModelService { ...@@ -544,4 +542,14 @@ public class ModelImpl implements ModelService {
} }
} }
@Override
public List<Map<String, Object>> executeQuery(String sql) {
try {
return jdbcTemplate.queryForList(sql);
} catch (ApiException e) {
throw new ApiException("sql语法错误!");
}
}
} }
package com.tykj.model_layer.service.impl; package com.tykj.model_layer.service.impl;
import com.tykj.base.result.ApiException;
import com.tykj.model_layer.dao.QuoteDao; import com.tykj.model_layer.dao.QuoteDao;
import com.tykj.model_layer.entity.Quote; import com.tykj.model_layer.entity.Quote;
import com.tykj.model_layer.service.QuoteService; import com.tykj.model_layer.service.QuoteService;
...@@ -25,11 +26,23 @@ public class QuoteServiceImpl implements QuoteService { ...@@ -25,11 +26,23 @@ public class QuoteServiceImpl implements QuoteService {
} }
@Override @Override
public List<Quote> getAll() { public List<Quote> getAllQuote() {
return quoteDao.findAll(); return quoteDao.findAll();
} }
public boolean existValue(Integer cId,String value){
@Override
public Quote saveQuote(Quote quote) {
return quoteDao.save(quote);
}
@Override
public void delQuote(Integer id) {
quoteDao.deleteById(id);
}
public boolean existValue(Integer cId, String value){
List<String> collect = quoteDao.findAllByColumnId(cId) List<String> collect = quoteDao.findAllByColumnId(cId)
.stream() .stream()
.map(Quote::getValue) .map(Quote::getValue)
......
...@@ -9,11 +9,14 @@ import org.hibernate.SessionFactory; ...@@ -9,11 +9,14 @@ import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata; import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.schema.TargetType;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.EnumSet;
import java.util.List; import java.util.List;
/** /**
...@@ -59,8 +62,8 @@ public class SessionUtil { ...@@ -59,8 +62,8 @@ public class SessionUtil {
metadataSources.addInputStream(new ByteArrayInputStream(xmlStr.getBytes())); metadataSources.addInputStream(new ByteArrayInputStream(xmlStr.getBytes()));
Metadata metadata = metadataSources.buildMetadata(); Metadata metadata = metadataSources.buildMetadata();
//更新数据库Schema,如果不存在就创建表,存在就更新字段,不会影响已有数据 //更新数据库Schema,如果不存在就创建表,存在就更新字段,不会影响已有数据
// SchemaUpdate schemaUpdate = new SchemaUpdate(); SchemaUpdate schemaUpdate = new SchemaUpdate();
// schemaUpdate.execute(EnumSet.of(TargetType.DATABASE), metadata, serviceRegistry); schemaUpdate.execute(EnumSet.of(TargetType.DATABASE), metadata, serviceRegistry);
} }
public Session getSession(){ public Session getSession(){
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论