修复代码生成器移除JPA后异常
This commit is contained in:
@ -52,8 +52,7 @@ public class GeneratorController {
|
|||||||
public ResponseEntity<Object> getTables(@RequestParam(defaultValue = "") String name,
|
public ResponseEntity<Object> getTables(@RequestParam(defaultValue = "") String name,
|
||||||
@RequestParam(defaultValue = "0")Integer page,
|
@RequestParam(defaultValue = "0")Integer page,
|
||||||
@RequestParam(defaultValue = "10")Integer size){
|
@RequestParam(defaultValue = "10")Integer size){
|
||||||
int[] startEnd = PageUtil.transToStartEnd(page, size);
|
return new ResponseEntity<>(generatorService.getTables(name, page, size), HttpStatus.OK);
|
||||||
return new ResponseEntity<>(generatorService.getTables(name,startEnd), HttpStatus.OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("查询字段数据")
|
@ApiOperation("查询字段数据")
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package co.yixiang.gen.service;
|
package co.yixiang.gen.service;
|
||||||
|
|
||||||
|
import co.yixiang.common.web.vo.Paging;
|
||||||
import co.yixiang.gen.domain.ColumnConfig;
|
import co.yixiang.gen.domain.ColumnConfig;
|
||||||
import co.yixiang.gen.domain.GenConfig;
|
import co.yixiang.gen.domain.GenConfig;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@ -22,10 +23,11 @@ public interface GeneratorService {
|
|||||||
/**
|
/**
|
||||||
* 查询数据库元数据
|
* 查询数据库元数据
|
||||||
* @param name 表名
|
* @param name 表名
|
||||||
* @param startEnd 分页参数
|
* @param page 分页页码
|
||||||
|
* @param size 分页大小
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
Object getTables(String name, int[] startEnd);
|
Object getTables(String name, Integer page, Integer size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 得到数据表的元数据
|
* 得到数据表的元数据
|
||||||
@ -84,4 +86,6 @@ public interface GeneratorService {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
List<ColumnConfig> query(String table);
|
List<ColumnConfig> query(String table);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ import co.yixiang.utils.FileUtil;
|
|||||||
import co.yixiang.utils.PageUtil;
|
import co.yixiang.utils.PageUtil;
|
||||||
import co.yixiang.utils.StringUtils;
|
import co.yixiang.utils.StringUtils;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -30,6 +32,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.sql.Connection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -58,26 +61,12 @@ public class GeneratorServiceImpl extends BaseServiceImpl<ColumnInfoMapper, Colu
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getTables(String name, int[] startEnd) {
|
public Object getTables(String name, Integer page, Integer size) {
|
||||||
// 使用预编译防止sql注入
|
IPage<TableInfo> pages = null;
|
||||||
String sql = "select table_name ,create_time , engine, table_collation, table_comment from information_schema.tables " +
|
Page<TableInfo> pageModel = new Page<>(page, size);
|
||||||
"where table_schema = (select database()) " +
|
pages = baseMapper.selectTables(pageModel,name);
|
||||||
"and table_name like ? order by create_time desc";
|
Integer totalElements = 0;
|
||||||
Query query = em.createNativeQuery(sql);
|
return PageUtil.toPage(pages.getRecords(),pages.getTotal());
|
||||||
query.setFirstResult(startEnd[0]);
|
|
||||||
query.setMaxResults(startEnd[1]-startEnd[0]);
|
|
||||||
query.setParameter(1, StringUtils.isNotBlank(name) ? ("%" + name + "%") : "%%");
|
|
||||||
List result = query.getResultList();
|
|
||||||
List<TableInfo> tableInfos = new ArrayList<>();
|
|
||||||
for (Object obj : result) {
|
|
||||||
Object[] arr = (Object[]) obj;
|
|
||||||
tableInfos.add(new TableInfo(arr[0],arr[1],arr[2],arr[3], ObjectUtil.isNotEmpty(arr[4])? arr[4] : "-"));
|
|
||||||
}
|
|
||||||
Query query1 = em.createNativeQuery("SELECT COUNT(*) from information_schema.tables where table_schema = (select database()) " +
|
|
||||||
"and table_name like ? order by create_time desc");
|
|
||||||
query1.setParameter(1, StringUtils.isNotBlank(name) ? ("%" + name + "%") : "%%");
|
|
||||||
Object totalElements = query1.getSingleResult();
|
|
||||||
return PageUtil.toPage(tableInfos,totalElements);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -96,12 +85,27 @@ public class GeneratorServiceImpl extends BaseServiceImpl<ColumnInfoMapper, Colu
|
|||||||
@Override
|
@Override
|
||||||
public List<ColumnConfig> query(String tableName){
|
public List<ColumnConfig> query(String tableName){
|
||||||
// 使用预编译防止sql注入
|
// 使用预编译防止sql注入
|
||||||
String sql = "select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " +
|
/* String sql = "select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " +
|
||||||
"where table_name = ? and table_schema = (select database()) order by ordinal_position";
|
"where table_name = ? and table_schema = (select database()) order by ordinal_position";
|
||||||
Query query = em.createNativeQuery(sql);
|
Query query = em.createNativeQuery(sql);
|
||||||
query.setParameter(1,tableName);
|
query.setParameter(1,tableName);
|
||||||
List result = query.getResultList();
|
List result = query.getResultList();
|
||||||
List<ColumnConfig> columnInfos = new ArrayList<>();
|
List<ColumnConfig> columnInfos = new ArrayList<>();
|
||||||
|
for (Object obj : result) {
|
||||||
|
Object[] arr = (Object[]) obj;
|
||||||
|
columnInfos.add(
|
||||||
|
new ColumnConfig(
|
||||||
|
tableName,
|
||||||
|
arr[0].toString(),
|
||||||
|
"NO".equals(arr[1]),
|
||||||
|
arr[2].toString(),
|
||||||
|
ObjectUtil.isNotNull(arr[3]) ? arr[3].toString() : null,
|
||||||
|
ObjectUtil.isNotNull(arr[4]) ? arr[4].toString() : null,
|
||||||
|
ObjectUtil.isNotNull(arr[5]) ? arr[5].toString() : null)
|
||||||
|
);
|
||||||
|
}*/
|
||||||
|
List<ColumnConfig> columnInfos = new ArrayList<>();
|
||||||
|
List result = baseMapper.queryByTableName(tableName);
|
||||||
for (Object obj : result) {
|
for (Object obj : result) {
|
||||||
Object[] arr = (Object[]) obj;
|
Object[] arr = (Object[]) obj;
|
||||||
columnInfos.add(
|
columnInfos.add(
|
||||||
@ -133,7 +137,7 @@ public class GeneratorServiceImpl extends BaseServiceImpl<ColumnInfoMapper, Colu
|
|||||||
if(StringUtils.isBlank(column.getRemark())){
|
if(StringUtils.isBlank(column.getRemark())){
|
||||||
column.setRemark(columnInfo.getRemark());
|
column.setRemark(columnInfo.getRemark());
|
||||||
}
|
}
|
||||||
this.save(column);
|
this.saveOrUpdate(column);
|
||||||
} else {
|
} else {
|
||||||
// 如果找不到,则保存新字段信息
|
// 如果找不到,则保存新字段信息
|
||||||
this.save(columnInfo);
|
this.save(columnInfo);
|
||||||
|
@ -7,10 +7,31 @@ package co.yixiang.gen.service.mapper;
|
|||||||
|
|
||||||
import co.yixiang.gen.domain.ColumnConfig;
|
import co.yixiang.gen.domain.ColumnConfig;
|
||||||
import co.yixiang.common.mapper.CoreMapper;
|
import co.yixiang.common.mapper.CoreMapper;
|
||||||
|
import co.yixiang.gen.domain.vo.TableInfo;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface ColumnInfoMapper extends CoreMapper<ColumnConfig> {
|
public interface ColumnInfoMapper extends CoreMapper<ColumnConfig> {
|
||||||
|
|
||||||
|
@Select("select table_name ,create_time , engine, table_collation, table_comment from information_schema.tables " +
|
||||||
|
"where table_schema = (select database()) and table_name like CONCAT('%',#{name},'%') order by create_time desc")
|
||||||
|
IPage<TableInfo> selectTables(@Param("page") Page page, @Param("name") String name);
|
||||||
|
|
||||||
|
@Select("SELECT COUNT(*) from information_schema.tables where table_schema = (select database()) and table_name like CONCAT('%',#{name},'%') order by create_time desc")
|
||||||
|
Integer selectTablesCount(@Param("name") String name);
|
||||||
|
|
||||||
|
|
||||||
|
@Select("select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " +
|
||||||
|
"where table_name = #{name} and table_schema = (select database()) order by ordinal_position")
|
||||||
|
List queryByTableName(@Param("name") String name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user