Commit bf7c1eeb authored by dqjdda's avatar dqjdda
Browse files

代码优化完成,去除大量idea警告,代码生成器优化等

parent e3c3ebb1
......@@ -13,9 +13,9 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface Query {
/** Dong ZhaoYang 2017/8/7 基本对象的属性名 */
// Dong ZhaoYang 2017/8/7 基本对象的属性名
String propName() default "";
/** Dong ZhaoYang 2017/8/7 查询方式 */
// Dong ZhaoYang 2017/8/7 查询方式
Type type() default Type.EQUAL;
/**
......
......@@ -35,7 +35,7 @@ public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
return null;
}
String str = new String(bytes, StandardCharsets.UTF_8);
return (T) JSON.parseObject(str, clazz);
return JSON.parseObject(str, clazz);
}
}
......@@ -35,7 +35,7 @@ public class SwaggerConfig {
@Bean
public Docket createRestApi() {
ParameterBuilder ticketPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
List<Parameter> pars = new ArrayList<>();
ticketPar.name(tokenHeader).description("token")
.modelRef(new ModelRef("string"))
.parameterType("header")
......
......@@ -19,9 +19,6 @@ public class EncryptUtils {
/**
* 对称加密
* @param source
* @return
* @throws Exception
*/
public static String desEncrypt(String source) throws Exception {
if (source == null || source.length() == 0){
......
......@@ -11,6 +11,7 @@ import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
......@@ -92,7 +93,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
* 文件大小转换
*/
public static String getSize(long size){
String resultSize = "";
String resultSize;
if (size / GB >= 1) {
//如果当前Byte的值大于等于1GB
resultSize = DF.format(size / (float) GB) + "GB ";
......@@ -117,7 +118,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
......@@ -144,7 +145,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
String d = dest.getPath();
file.transferTo(dest);// 文件写入
return dest;
} catch (Exception e) {
......@@ -155,7 +155,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
public static String fileToBase64(File file) throws Exception {
FileInputStream inputFile = new FileInputStream(file);
String base64 =null;
String base64;
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
......@@ -210,4 +210,64 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
throw new BadRequestException("文件超出规定大小");
}
}
/**
* 判断两个文件是否相同
*/
public static boolean check(File file1, File file2) {
String img1Md5 = getMD5(file1);
String img2Md5 = getMD5(file2);
return img1Md5.equals(img2Md5);
}
/**
* 判断两个文件是否相同
*/
public static boolean check(String file1Md5, String file2Md5) {
return file1Md5.equals(file2Md5);
}
private static byte[] getByte(File file) {
// 得到文件长度
byte[] b = new byte[(int) file.length()];
try {
InputStream in = new FileInputStream(file);
try {
in.read(b);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return b;
}
private static String getMD5(byte[] bytes) {
// 16进制字符
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(bytes);
byte[] md = mdTemp.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
// 移位 输出字符串
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getMD5(File file) {
return getMD5(getByte(file));
}
}
......@@ -112,7 +112,8 @@ public class QueryHelp {
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return cb.and(list.toArray(new Predicate[list.size()]));
int size = list.size();
return cb.and(list.toArray(new Predicate[size]));
}
@SuppressWarnings("unchecked")
......@@ -124,8 +125,7 @@ public class QueryHelp {
}
}
@SuppressWarnings("unchecked")
public static boolean isBlank(final CharSequence cs) {
private static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
......@@ -138,7 +138,6 @@ public class QueryHelp {
return true;
}
@SuppressWarnings("unchecked")
private static List<Field> getAllFields(Class clazz, List<Field> fields) {
if (clazz != null) {
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
......
......@@ -13,7 +13,7 @@ import org.springframework.security.core.userdetails.UserDetails;
public class SecurityUtils {
public static UserDetails getUserDetails() {
UserDetails userDetails = null;
UserDetails userDetails;
try {
userDetails = (UserDetails) org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication().getPrincipal();
} catch (Exception e) {
......
......@@ -7,7 +7,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author
* @author Jie
* @date 2019-01-07
*/
@Slf4j
......@@ -15,14 +15,6 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
private static ApplicationContext applicationContext = null;
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
}
/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
......@@ -53,14 +45,14 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
/**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clearHolder() {
private static void clearHolder() {
log.debug("清除SpringContextHolder中的ApplicationContext:"
+ applicationContext);
applicationContext = null;
}
@Override
public void destroy() throws Exception {
public void destroy(){
SpringContextHolder.clearHolder();
}
......
......@@ -18,25 +18,6 @@ import java.util.Date;
public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final char SEPARATOR = '_';
private static final String CHARSET_NAME = "UTF-8";
/**
* 是否包含字符串
*
* @param str 验证字符串
* @param strs 字符串组
* @return 包含返回true
*/
static boolean inString(String str, String... strs) {
if (str != null) {
for (String s : strs) {
if (str.equals(trim(s))) {
return true;
}
}
}
return false;
}
/**
* 驼峰命名法工具
......@@ -151,9 +132,9 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
DbConfig config = new DbConfig();
File file = FileUtil.inputStreamToFile(new ClassPathResource(path).getStream(), name);
DbSearcher searcher = new DbSearcher(config, file.getPath());
Method method = null;
Method method;
method = searcher.getClass().getMethod("btreeSearch", String.class);
DataBlock dataBlock = null;
DataBlock dataBlock;
dataBlock = (DataBlock) method.invoke(searcher, ip);
String address = dataBlock.getRegion().replace("0|","");
if(address.charAt(address.length()-1) == '|'){
......
......@@ -36,7 +36,7 @@ public class TranslatorUtil {
}
}
private static String parseResult(String inputJson) throws Exception {
private static String parseResult(String inputJson){
JSONArray jsonArray2 = (JSONArray) new JSONArray(inputJson).get(0);
StringBuilder result = new StringBuilder();
for (Object o : jsonArray2) {
......
package me.zhengjie.utils;
import cn.hutool.core.util.ObjectUtil;
import me.zhengjie.exception.BadRequestException;
import java.util.Optional;
/**
* 验证工具
......@@ -13,11 +13,9 @@ public class ValidationUtil{
/**
* 验证空
*/
public static void isNull(Optional optional, String entity,String parameter , Object value){
if(!optional.isPresent()){
String msg = entity
+ " 不存在 "
+"{ "+ parameter +":"+ value.toString() +" }";
public static void isNull(Object obj, String entity, String parameter , Object value){
if(ObjectUtil.isNull(obj)){
String msg = entity + " 不存在: "+ parameter +" is "+ value;
throw new BadRequestException(msg);
}
}
......
......@@ -11,12 +11,6 @@ import static org.junit.Assert.*;
public class StringUtilsTest {
@Test
public void testInString() {
assertTrue(inString("?", "?"));
assertFalse(inString("?", new String[]{}));
}
@Test
public void testToCamelCase() {
assertNull(toCamelCase(null));
......
......@@ -16,26 +16,26 @@ public class GenConfig {
@Id
private Long id;
/** 包路径 **/
// 包路径
private String pack;
/** 模块名 **/
// 模块名
@Column(name = "module_name")
private String moduleName;
/** 前端文件路径 **/
// 前端文件路径
private String path;
/** 前端文件路径 **/
// 前端文件路径
@Column(name = "api_path")
private String apiPath;
/** 作者 **/
// 作者
private String author;
/** 表前缀 **/
// 表前缀
private String prefix;
/** 是否覆盖 **/
// 是否覆盖
private Boolean cover;
}
......@@ -14,27 +14,27 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public class ColumnInfo {
/** 数据库字段名称 **/
// 数据库字段名称
private Object columnName;
/** 允许空值 **/
// 允许空值
private Object isNullable;
/** 数据库字段类型 **/
// 数据库字段类型
private Object columnType;
/** 数据库字段注释 **/
// 数据库字段注释
private Object columnComment;
/** 数据库字段键类型 **/
// 数据库字段键类型
private Object columnKey;
/** 额外的参数 **/
// 额外的参数
private Object extra;
/** 查询 1:模糊 2:精确 **/
// 查询 1:模糊 2:精确
private String columnQuery;
/** 是否在列表显示 **/
// 是否在列表显示
private String columnShow;
}
......@@ -14,10 +14,10 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public class TableInfo {
/** 表名称 **/
// 表名称
private Object tableName;
/** 创建日期 **/
// 创建日期
private Object createTime;
// 数据库引擎
......
package me.zhengjie.rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.domain.GenConfig;
import me.zhengjie.service.GenConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
......@@ -13,23 +14,25 @@ import org.springframework.web.bind.annotation.*;
* @date 2019-01-14
*/
@RestController
@RequestMapping("api")
@RequestMapping("/api/genConfig")
@Api(tags = "系统:代码生成器配置管理")
public class GenConfigController {
@Autowired
private GenConfigService genConfigService;
private final GenConfigService genConfigService;
/**
* 查询生成器配置
* @return
*/
@GetMapping(value = "/genConfig")
public GenConfigController(GenConfigService genConfigService) {
this.genConfigService = genConfigService;
}
@ApiOperation("查询")
@GetMapping
public ResponseEntity get(){
return new ResponseEntity(genConfigService.find(), HttpStatus.OK);
return new ResponseEntity<>(genConfigService.find(), HttpStatus.OK);
}
@PutMapping(value = "/genConfig")
@ApiOperation("修改")
@PutMapping
public ResponseEntity emailConfig(@Validated @RequestBody GenConfig genConfig){
return new ResponseEntity(genConfigService.update(genConfig),HttpStatus.OK);
return new ResponseEntity<>(genConfigService.update(genConfig),HttpStatus.OK);
}
}
package me.zhengjie.rest;
import cn.hutool.core.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.domain.vo.ColumnInfo;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.service.GenConfigService;
import me.zhengjie.service.GeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -17,49 +18,39 @@ import java.util.List;
* @date 2019-01-02
*/
@RestController
@RequestMapping("api")
@RequestMapping("/api/generator")
@Api(tags = "系统:代码生成管理")
public class GeneratorController {
@Autowired
private GeneratorService generatorService;
private final GeneratorService generatorService;
@Autowired
private GenConfigService genConfigService;
private final GenConfigService genConfigService;
@Value("${generator.enabled}")
private Boolean generatorEnabled;
/**
* 查询数据库元数据
* @param name
* @param page
* @param size
* @return
*/
@GetMapping(value = "/generator/tables")
public GeneratorController(GeneratorService generatorService, GenConfigService genConfigService) {
this.generatorService = generatorService;
this.genConfigService = genConfigService;
}
@ApiOperation("查询数据库元数据")
@GetMapping(value = "/tables")
public ResponseEntity getTables(@RequestParam(defaultValue = "") String name,
@RequestParam(defaultValue = "0")Integer page,
@RequestParam(defaultValue = "10")Integer size){
int[] startEnd = PageUtil.transToStartEnd(page+1, size);
return new ResponseEntity(generatorService.getTables(name,startEnd), HttpStatus.OK);
return new ResponseEntity<>(generatorService.getTables(name,startEnd), HttpStatus.OK);
}
/**
* 查询表内元数据
* @param tableName
* @return
*/
@GetMapping(value = "/generator/columns")
@ApiOperation("查询表内元数据")
@GetMapping(value = "/columns")
public ResponseEntity getTables(@RequestParam String tableName){
return new ResponseEntity(generatorService.getColumns(tableName), HttpStatus.OK);
return new ResponseEntity<>(generatorService.getColumns(tableName), HttpStatus.OK);
}
/**
* 生成代码
* @param columnInfos
* @return
*/
@PostMapping(value = "/generator")
@ApiOperation("生成代码")
@PostMapping
public ResponseEntity generator(@RequestBody List<ColumnInfo> columnInfos, @RequestParam String tableName){
if(!generatorEnabled){
throw new BadRequestException("此环境不允许生成代码!");
......
......@@ -10,21 +10,9 @@ import org.springframework.cache.annotation.Cacheable;
* @author Zheng Jie
* @date 2019-01-14
*/
@CacheConfig(cacheNames = "genConfig")
public interface GenConfigService {
/**
* find
* @return
*/
@Cacheable(key = "'1'")
GenConfig find();
/**
* update
* @param genConfig
* @return
*/
@CacheEvict(allEntries = true)
GenConfig update(GenConfig genConfig);
}
......@@ -12,24 +12,24 @@ public interface GeneratorService {
/**
* 查询数据库元数据
* @param name
* @param startEnd
* @return
* @param name 表名
* @param startEnd 分页参数
* @return /
*/
Object getTables(String name, int[] startEnd);
/**
* 得到数据表的元数据
* @param name
* @return
* @param name 表名
* @return /
*/
Object getColumns(String name);
/**
* 生成代码
* @param columnInfos
* @param genConfig
* @param tableName
* @param columnInfos 表字段数据
* @param genConfig 代码生成配置
* @param tableName 表名
*/
void generator(List<ColumnInfo> columnInfos, GenConfig genConfig, String tableName);
}
......@@ -3,9 +3,10 @@ package me.zhengjie.service.impl;
import me.zhengjie.domain.GenConfig;
import me.zhengjie.repository.GenConfigRepository;
import me.zhengjie.service.GenConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.Optional;
......@@ -14,35 +15,37 @@ import java.util.Optional;
* @date 2019-01-14
*/
@Service
@CacheConfig(cacheNames = "genConfig")
public class GenConfigServiceImpl implements GenConfigService {
@Autowired
private GenConfigRepository genConfigRepository;
private final GenConfigRepository genConfigRepository;
public GenConfigServiceImpl(GenConfigRepository genConfigRepository) {
this.genConfigRepository = genConfigRepository;
}
@Override
@Cacheable(key = "'1'")
public GenConfig find() {
Optional<GenConfig> genConfig = genConfigRepository.findById(1L);
if(genConfig.isPresent()){
return genConfig.get();
} else {
return new GenConfig();
}
return genConfig.orElseGet(GenConfig::new);
}
@Override
@CacheEvict(allEntries = true)
public GenConfig update(GenConfig genConfig) {
genConfig.setId(1L);
// 自动设置Api路径,注释掉前需要同步取消前端的注释
String separator = File.separator;
String[] paths = null;
String[] paths;
if (separator.equals("\\")) {
paths = genConfig.getPath().split("\\\\");
} else paths = genConfig.getPath().split(File.separator);
StringBuffer api = new StringBuffer();
for (int i = 0; i < paths.length; i++) {
api.append(paths[i]);
StringBuilder api = new StringBuilder();
for (String path : paths) {
api.append(path);
api.append(separator);
if(paths[i].equals("src")){
if (path.equals("src")) {
api.append("api");
break;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment