From 96f1607dcde1bfa1950877a21cc6c1d5d62236c7 Mon Sep 17 00:00:00 2001 From: IT-xiaoXiong <774495953@qq.com> Date: Wed, 22 Dec 2021 19:26:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8F=E8=A7=88=E6=A1=86=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=8D=E5=88=B6-=E4=B8=80=E5=AF=B9?= =?UTF-8?q?=E5=A4=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + .../java/aiyh/utils/HotDeployToolUtil.java | 74 ------ src/main/java/aiyh/utils/Util.java | 37 +++ .../utils/annotation/recordset/Delete.java | 1 + .../utils/annotation/recordset/Insert.java | 3 +- .../utils/annotation/recordset/Select.java | 3 +- .../utils/annotation/recordset/Update.java | 1 + .../utils/recordset/BooleanTypeHandler.java | 6 +- .../aiyh/utils/recordset/DataTypeHandler.java | 43 +++- .../utils/recordset/IntegerTypeHandler.java | 6 +- .../aiyh/utils/recordset/RecordsetUtil.java | 1 + .../aiyh/utils/recordset/ResultMapper.java | 25 +- .../java/aiyh/utils/recordset/SqlHandler.java | 12 +- .../utils/recordset/StringTypeHandler.java | 6 +- .../aiyh/utils/recordset/TypeHandler.java | 6 +- .../builderSql/impl/BuilderSqlImpl.java | 30 +++ src/test/java/baseTest/BaseTest.java | 2 +- src/test/java/entity/ImageInfo.java | 9 +- .../java/mybatisTest/mapper/IMapperTest.java | 36 +++ src/test/java/pcn/copy/TestCopy.java | 40 +++ src/test/java/utilTest/ITestMapper.java | 10 +- src/test/java/utilTest/UtilTest.java | 243 ++++++++++++------ 22 files changed, 401 insertions(+), 195 deletions(-) delete mode 100644 src/main/java/aiyh/utils/HotDeployToolUtil.java create mode 100644 src/test/java/mybatisTest/mapper/IMapperTest.java create mode 100644 src/test/java/pcn/copy/TestCopy.java diff --git a/.gitignore b/.gitignore index 83bee01..0695e38 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,8 @@ log /target /lib/classbean .idea/ +src/main/java/.gitignore +src/main/java/README.md !/src/main/resources/WEB-INF/prop/prop2map/*.properties # Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties` # should NOT be excluded as they contain compiler settings and other important diff --git a/src/main/java/aiyh/utils/HotDeployToolUtil.java b/src/main/java/aiyh/utils/HotDeployToolUtil.java deleted file mode 100644 index 12e113f..0000000 --- a/src/main/java/aiyh/utils/HotDeployToolUtil.java +++ /dev/null @@ -1,74 +0,0 @@ -package aiyh.utils; - -import aiyh.utils.zwl.common.ToolUtil; - - -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; - -/** - * @author EBU7-dev1-ayh - * @date 2021/8/25 0025 10:53 - * 热部署 自定义类加载器 - */ - - -public class HotDeployToolUtil extends ClassLoader { - ToolUtil toolUtil = new ToolUtil(); - private final String classpath; - private final String className; - - public HotDeployToolUtil(String classpath, String className) { - this.classpath = classpath; - this.className = className; - } - - @Override - public Class loadClass(String name, boolean resolve) throws ClassNotFoundException { - Class loadedClass = findLoadedClass(name); - // 需要我自己去加载 - if (loadedClass == null) { - loadedClass = findClass(name); - if (loadedClass != null) { - return loadedClass; - } - } - return super.loadClass(name, resolve); - } - - - @Override - public Class findClass(String name) throws ClassNotFoundException { - if (name.startsWith("java.")) { - return getSystemClassLoader().loadClass(name); - } - byte[] data; - String classPath = name.replace(".", System.getProperties().getProperty("file.separator")) + ".class"; - try { - data = this.loadClassData(classPath); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - return this.defineClass(name, data, 0, data.length); - } - - public byte[] loadClassData(String name) throws Exception { - FileInputStream inputStream; - try { - toolUtil.writeDebuggerLog(classpath + name); - inputStream = new FileInputStream(classpath + name); - // 定义字节数组输出流 - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int b = 0; - while ((b = inputStream.read()) != -1) { - baos.write(b); - } - inputStream.close(); - return baos.toByteArray(); - } catch (Exception e) { - e.printStackTrace(); - throw new ClassNotFoundException(); - } - } -} diff --git a/src/main/java/aiyh/utils/Util.java b/src/main/java/aiyh/utils/Util.java index f94c200..80abe36 100644 --- a/src/main/java/aiyh/utils/Util.java +++ b/src/main/java/aiyh/utils/Util.java @@ -5,6 +5,7 @@ import aiyh.utils.entity.*; import aiyh.utils.fileUtil.ProperUtil; import aiyh.utils.mapUtil.UtilHashMap; import aiyh.utils.mapUtil.UtilLinkedHashMap; +import aiyh.utils.recordset.RecordsetUtil; import aiyh.utils.service.UtilService; import aiyh.utils.sqlUtil.builderSql.impl.BuilderSqlImpl; import aiyh.utils.sqlUtil.whereUtil.Where; @@ -55,6 +56,7 @@ public class Util extends weaver.general.Util { static ToolUtil toolUtil = new ToolUtil(); private static final UtilService utilService = new UtilService(); private static final RecordSet rs = new RecordSet(); + private static RecordsetUtil recordsetUtil = new RecordsetUtil();; /** @@ -144,6 +146,12 @@ public class Util extends weaver.general.Util { return str; } + /** + * 移除前后的指定字符 + * @param sqlBuilder 需要移除的字符串的StringBuilder 对象 + * @param removeSeparator 移除的字符串 + * @return 移除前后指定字符后的字符串 + */ public static String removeSeparator(StringBuilder sqlBuilder, String removeSeparator) { String str = sqlBuilder.toString().trim(); if (str.endsWith(removeSeparator)) { @@ -156,6 +164,25 @@ public class Util extends weaver.general.Util { return str; } + /** + * 移除前后的指定字符 + * @param string 需要移除的字符串的 + * @param removeSeparator 移除的字符串 + * @return 移除前后指定字符后的字符串 + */ + public static String removeSeparator(String string, String removeSeparator) { + StringBuilder sqlBuilder = new StringBuilder(string); + String str = sqlBuilder.toString().trim(); + if (str.endsWith(removeSeparator)) { +// 如果以分割号结尾,则去除分割号 + str = str.substring(0, str.length() - 1); + } + if (str.trim().startsWith(removeSeparator)) { + str = str.substring(1); + } + return str; + } + /** * 通过RecordSet对象,获取查询后的值,返回map * @@ -1777,4 +1804,14 @@ public class Util extends weaver.general.Util { }); return stringWriter.getBuffer().toString(); } + + /** + * 获取RecordSetUtil的SQL代理类对象 + * @param t + * @param + * @return + */ + public static T getMapper(Class t) { + return recordsetUtil.getMapper(t); + } } diff --git a/src/main/java/aiyh/utils/annotation/recordset/Delete.java b/src/main/java/aiyh/utils/annotation/recordset/Delete.java index 3101586..65d724f 100644 --- a/src/main/java/aiyh/utils/annotation/recordset/Delete.java +++ b/src/main/java/aiyh/utils/annotation/recordset/Delete.java @@ -12,5 +12,6 @@ import java.lang.annotation.*; @Documented public @interface Delete { String value() default ""; + // sql是否是在参数中 boolean custom() default false; } diff --git a/src/main/java/aiyh/utils/annotation/recordset/Insert.java b/src/main/java/aiyh/utils/annotation/recordset/Insert.java index b8fa244..59cf7ae 100644 --- a/src/main/java/aiyh/utils/annotation/recordset/Insert.java +++ b/src/main/java/aiyh/utils/annotation/recordset/Insert.java @@ -11,6 +11,7 @@ import java.lang.annotation.*; @Target(ElementType.METHOD) @Documented public @interface Insert { - String value() default "" ; + String value() default ""; + // sql是否是在参数中 boolean custom() default false; } diff --git a/src/main/java/aiyh/utils/annotation/recordset/Select.java b/src/main/java/aiyh/utils/annotation/recordset/Select.java index 25388c6..7caa308 100644 --- a/src/main/java/aiyh/utils/annotation/recordset/Select.java +++ b/src/main/java/aiyh/utils/annotation/recordset/Select.java @@ -11,8 +11,7 @@ import java.lang.annotation.*; @Target(ElementType.METHOD) @Documented public @interface Select { -// sql String value() default ""; -// sql是否是在参数中 + // sql是否是在参数中 boolean custom() default false; } diff --git a/src/main/java/aiyh/utils/annotation/recordset/Update.java b/src/main/java/aiyh/utils/annotation/recordset/Update.java index 8e44950..f9c76e1 100644 --- a/src/main/java/aiyh/utils/annotation/recordset/Update.java +++ b/src/main/java/aiyh/utils/annotation/recordset/Update.java @@ -12,5 +12,6 @@ import java.lang.annotation.*; @Documented public @interface Update { String value() default ""; + // sql是否是在参数中 boolean custom() default false; } diff --git a/src/main/java/aiyh/utils/recordset/BooleanTypeHandler.java b/src/main/java/aiyh/utils/recordset/BooleanTypeHandler.java index d8e0863..dfc688a 100644 --- a/src/main/java/aiyh/utils/recordset/BooleanTypeHandler.java +++ b/src/main/java/aiyh/utils/recordset/BooleanTypeHandler.java @@ -2,6 +2,8 @@ package aiyh.utils.recordset; import weaver.conn.RecordSet; +import java.lang.reflect.Field; + /** * @author EBU7-dev1-ayh * create 2021/12/21 0021 13:34 @@ -10,12 +12,12 @@ import weaver.conn.RecordSet; public class BooleanTypeHandler implements TypeHandler{ @Override - public Object getValue(RecordSet rs, String fieldName) { + public Object getValue(RecordSet rs, String fieldName, Field declaredField) { return rs.getBoolean(fieldName); } @Override - public Object getValue(RecordSet rs, int index) { + public Object getValue(RecordSet rs, int index,Field declaredField) { return rs.getBoolean(index); } } diff --git a/src/main/java/aiyh/utils/recordset/DataTypeHandler.java b/src/main/java/aiyh/utils/recordset/DataTypeHandler.java index ed9fc25..7b5c121 100644 --- a/src/main/java/aiyh/utils/recordset/DataTypeHandler.java +++ b/src/main/java/aiyh/utils/recordset/DataTypeHandler.java @@ -1,7 +1,12 @@ package aiyh.utils.recordset; +import aiyh.utils.annotation.DateFormatAn; +import aiyh.utils.excention.TypeNonsupportException; +import com.ibm.icu.text.SimpleDateFormat; import weaver.conn.RecordSet; +import java.lang.reflect.Field; +import java.text.ParseException; import java.util.Date; /** @@ -10,18 +15,42 @@ import java.util.Date; */ -public class DataTypeHandler implements TypeHandler{ +public class DataTypeHandler implements TypeHandler { @Override - public Object getValue(RecordSet rs, String fieldName) { - String dataString = rs.getString(fieldName); - Date date = new Date(dataString); + public Object getValue(RecordSet rs, String fieldName, Field declaredField) { + if (declaredField == null) { + throw new TypeNonsupportException("An error occurred while trying to convert the query result field to type Date!"); + } + DateFormatAn annotation = declaredField.getAnnotation(DateFormatAn.class); + Date date = null; + if (annotation != null) { + String value = annotation.value(); + try { + date = new SimpleDateFormat(value).parse(rs.getString(fieldName)); + } catch (ParseException e) { + e.printStackTrace(); + throw new TypeNonsupportException("Failed to convert [" + rs.getString(fieldName) + "] to a Date object as [" + value + "]!!"); + } + } return date; } @Override - public Object getValue(RecordSet rs, int index) { - String dataString = rs.getString(index); - Date date = new Date(dataString); + public Object getValue(RecordSet rs, int index, Field declaredField) { + if (declaredField == null) { + throw new TypeNonsupportException("An error occurred while trying to convert the query result field to type Date!"); + } + DateFormatAn annotation = declaredField.getAnnotation(DateFormatAn.class); + Date date = null; + if (annotation != null) { + String value = annotation.value(); + try { + date = new SimpleDateFormat(value).parse(rs.getString(index)); + } catch (ParseException e) { + e.printStackTrace(); + throw new TypeNonsupportException("Failed to convert [" + rs.getString(index) + "] to a Date object as [" + value + "]!!"); + } + } return date; } } diff --git a/src/main/java/aiyh/utils/recordset/IntegerTypeHandler.java b/src/main/java/aiyh/utils/recordset/IntegerTypeHandler.java index e2def61..81d0709 100644 --- a/src/main/java/aiyh/utils/recordset/IntegerTypeHandler.java +++ b/src/main/java/aiyh/utils/recordset/IntegerTypeHandler.java @@ -2,6 +2,8 @@ package aiyh.utils.recordset; import weaver.conn.RecordSet; +import java.lang.reflect.Field; + /** * @author EBU7-dev1-ayh * create 2021/12/21 0021 13:10 @@ -10,12 +12,12 @@ import weaver.conn.RecordSet; public class IntegerTypeHandler implements TypeHandler{ @Override - public Object getValue(RecordSet rs, String fieldName) { + public Object getValue(RecordSet rs, String fieldName, Field declaredField) { return rs.getInt(fieldName); } @Override - public Object getValue(RecordSet rs, int index) { + public Object getValue(RecordSet rs, int index,Field declaredField) { return rs.getInt(index); } } diff --git a/src/main/java/aiyh/utils/recordset/RecordsetUtil.java b/src/main/java/aiyh/utils/recordset/RecordsetUtil.java index 6f5a612..a608863 100644 --- a/src/main/java/aiyh/utils/recordset/RecordsetUtil.java +++ b/src/main/java/aiyh/utils/recordset/RecordsetUtil.java @@ -44,6 +44,7 @@ public class RecordsetUtil implements InvocationHandler { String sql = select.value(); boolean custom = select.custom(); PrepSqlResultImpl handler = sqlHandler.handler(sql, custom, method, args); + System.out.println(handler); if (handler.getArgs().isEmpty()) { rs.executeQuery(handler.getSqlStr()); } else { diff --git a/src/main/java/aiyh/utils/recordset/ResultMapper.java b/src/main/java/aiyh/utils/recordset/ResultMapper.java index efe7c36..944b35c 100644 --- a/src/main/java/aiyh/utils/recordset/ResultMapper.java +++ b/src/main/java/aiyh/utils/recordset/ResultMapper.java @@ -32,7 +32,7 @@ public class ResultMapper { typeHandler.put(Long.class, integerTypeHandler); typeHandler.put(Boolean.class, new BooleanTypeHandler()); typeHandler.put(boolean.class, new BooleanTypeHandler()); - typeHandler.put(Date.class, new StringTypeHandler()); + typeHandler.put(Date.class, new DataTypeHandler()); } public T mapperResult(RecordSet rs, Method method, Class tClass) { @@ -48,7 +48,7 @@ public class ResultMapper { } if (ResultMapper.typeHandler.containsKey(tClass)) { rs.next(); - return (T) ResultMapper.typeHandler.get(tClass).getValue(rs, 1); + return (T) ResultMapper.typeHandler.get(tClass).getValue(rs, 1,null); } T t = tClass.newInstance(); if (t instanceof Collection) { @@ -82,15 +82,18 @@ public class ResultMapper { if (o instanceof Map || o instanceof Collection) { throw new TypeNonsupportException("An unsupported return type!"); } - rs.next(); - return (T) getObject(rs, t,method); + if(rs.next()){ + return (T) getObject(rs, t,method); + } + return null; } - if (t.getClass().isArray()) { throw new TypeNonsupportException("An unsupported return type!"); } - return (T) getObject(rs, t,method); - + if(rs.next()){ + return (T) getObject(rs, t,method); + } + return null; } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } @@ -133,6 +136,7 @@ public class ResultMapper { } if(enable){ ((Map) o).put(Util.toCamelCase(columnName[i]), rs.getString(i + 1)); + continue; } ((Map) o).put(columnName[i], rs.getString(i + 1)); continue; @@ -163,15 +167,18 @@ public class ResultMapper { if (o instanceof Boolean) { return rs.getBoolean(i + 1); } + BeanInfo beanInfo = Introspector.getBeanInfo(o.getClass(), Object.class); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { Class propertyType = propertyDescriptor.getPropertyType(); Object value = null; + String fieldName = propertyDescriptor.getName(); + Field declaredField = o.getClass().getDeclaredField(fieldName); if(enable){ - value = ResultMapper.typeHandler.get(propertyType).getValue(rs, Util.toUnderlineCase(propertyDescriptor.getName())); + value = ResultMapper.typeHandler.get(propertyType) == null ? null : ResultMapper.typeHandler.get(propertyType).getValue(rs, Util.toUnderlineCase(fieldName),declaredField); }else { - value = ResultMapper.typeHandler.get(propertyType).getValue(rs, propertyDescriptor.getName()); + value = ResultMapper.typeHandler.get(propertyType) == null ? null : ResultMapper.typeHandler.get(propertyType).getValue(rs, fieldName,declaredField); } propertyDescriptor.getWriteMethod().invoke(o, value); } diff --git a/src/main/java/aiyh/utils/recordset/SqlHandler.java b/src/main/java/aiyh/utils/recordset/SqlHandler.java index f8fce8b..fd591b0 100644 --- a/src/main/java/aiyh/utils/recordset/SqlHandler.java +++ b/src/main/java/aiyh/utils/recordset/SqlHandler.java @@ -31,21 +31,13 @@ public class SqlHandler { // 处理基本类型以及包装类 String parse; if (methodArgNameMap.size() == 0) { - return new PrepSqlResultImpl(sql, sqlArgs); + return new PrepSqlResultImpl(findSql, sqlArgs); } if (methodArgNameMap.size() == 1) { - if (!custom) { Optional first = methodArgNameMap.values().stream().findFirst(); parse = parse(findSql, first.get()); - } else { - return new PrepSqlResultImpl(sql, sqlArgs); - } - } else if (methodArgNameMap.size() == 2 && custom) { - int index = findArg(method); - parse = parse(findSql, args[index]); } else { parse = parse(findSql, methodArgNameMap); - } return new PrepSqlResultImpl(parse, sqlArgs); } @@ -183,7 +175,7 @@ public class SqlHandler { } if (arg.getClass().isArray()) { // throw new TypeNonsupportException("A value is expected, but a set is received!"); - return StringUtils.join(Collections.singletonList(arg), ","); + return StringUtils.join((Object[]) arg, ","); } // 判断参数类型 if (arg instanceof Collection) { diff --git a/src/main/java/aiyh/utils/recordset/StringTypeHandler.java b/src/main/java/aiyh/utils/recordset/StringTypeHandler.java index 5a55e78..a5032e2 100644 --- a/src/main/java/aiyh/utils/recordset/StringTypeHandler.java +++ b/src/main/java/aiyh/utils/recordset/StringTypeHandler.java @@ -2,6 +2,8 @@ package aiyh.utils.recordset; import weaver.conn.RecordSet; +import java.lang.reflect.Field; + /** * @author EBU7-dev1-ayh * create 2021/12/21 0021 13:06 @@ -10,12 +12,12 @@ import weaver.conn.RecordSet; public class StringTypeHandler implements TypeHandler{ @Override - public Object getValue(RecordSet rs, String fieldName) { + public Object getValue(RecordSet rs, String fieldName, Field declaredField) { return rs.getString(fieldName); } @Override - public Object getValue(RecordSet rs, int index) { + public Object getValue(RecordSet rs, int index,Field declaredField) { return rs.getString(index); } } diff --git a/src/main/java/aiyh/utils/recordset/TypeHandler.java b/src/main/java/aiyh/utils/recordset/TypeHandler.java index 716b700..f0dd1bc 100644 --- a/src/main/java/aiyh/utils/recordset/TypeHandler.java +++ b/src/main/java/aiyh/utils/recordset/TypeHandler.java @@ -2,12 +2,14 @@ package aiyh.utils.recordset; import weaver.conn.RecordSet; +import java.lang.reflect.Field; + /** * @author @author EBU7-dev1-ay * create 2021/12/21 0021 13:05 */ public interface TypeHandler { - Object getValue(RecordSet rs,String fieldName); - Object getValue(RecordSet rs,int index); + Object getValue(RecordSet rs, String fieldName, Field declaredField); + Object getValue(RecordSet rs,int index,Field declaredField); } diff --git a/src/main/java/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java b/src/main/java/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java index fa9b0b8..8cf0589 100644 --- a/src/main/java/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java +++ b/src/main/java/aiyh/utils/sqlUtil/builderSql/impl/BuilderSqlImpl.java @@ -64,6 +64,13 @@ public class BuilderSqlImpl implements BuilderSql { return new PrepSqlResultImpl(sqlBuilder.toString(), args); } + /** + * 通过实体类构建插入SQL + * @param tableName 数据库表名 + * @param t 实体类对象 + * @param 实体类对象泛型 + * @return SQL结果对象 + */ public PrepSqlResultImpl insertSqlByEntity(String tableName, T t){ List args = new ArrayList<>(); StringBuilder sqlBuilder = new StringBuilder("insert into "); @@ -136,6 +143,13 @@ public class BuilderSqlImpl implements BuilderSql { return new BatchSqlResultImpl(sqlBuilder.toString(), args); } + /** + * 构建批量插入SQL + * @param tableName 数据库表名 + * @param list 实体类数组 + * @param 实体类泛型 + * @return SQL对象 + */ public BatchSqlResultImpl insertBatchSqlByEntity(String tableName, List list) { StringBuilder sqlBuilder = new StringBuilder("insert into "); sqlBuilder.append(tableName); @@ -209,6 +223,14 @@ public class BuilderSqlImpl implements BuilderSql { return new PrepSqlResultImpl(sqlBuilder.toString(), args); } + /** + * 构建更新SQL语句 + * @param tableName 数据库表名 + * @param t 实体类对象 + * @param where 更新条件对象 + * @param 实体类泛型 + * @return 构建后的SQL对象 + */ public PrepSqlResultImpl updateSqlByEntity(String tableName, T t, Where where){ this.verifyWhere(where); StringBuilder sqlBuilder = new StringBuilder("update "); @@ -284,6 +306,14 @@ public class BuilderSqlImpl implements BuilderSql { return new BatchSqlResultImpl(sqlBuilder.toString(), args); } + /** + * 通过实体类构建批量更新SQL + * @param tableName 数据库表名 + * @param list 实体类集合 + * @param whereList 更新条件集合,一一对应更新数据集合 + * @param 泛型 + * @return 构建后的批量SQL对象 + */ public BatchSqlResultImpl updateBatchSqlByEntity(String tableName, List list, List whereList) { this.verifyWhereList(whereList); if (list.size() != whereList.size()) { diff --git a/src/test/java/baseTest/BaseTest.java b/src/test/java/baseTest/BaseTest.java index cc232bf..cb30452 100644 --- a/src/test/java/baseTest/BaseTest.java +++ b/src/test/java/baseTest/BaseTest.java @@ -15,7 +15,7 @@ public class BaseTest { @Before public void before() { weaver.general.GCONST.setServerName("ecology"); - weaver.general.GCONST.setRootPath("H:\\ecology-9-dev\\src\\main\\resources"); + weaver.general.GCONST.setRootPath("H:\\ecology-9-dev\\src\\main\\resources\\"); } @Test diff --git a/src/test/java/entity/ImageInfo.java b/src/test/java/entity/ImageInfo.java index de3846f..d5fdd42 100644 --- a/src/test/java/entity/ImageInfo.java +++ b/src/test/java/entity/ImageInfo.java @@ -1,7 +1,10 @@ package entity; +import aiyh.utils.annotation.DateFormatAn; import lombok.Data; +import java.util.Date; + /** * @author EBU7-dev1-ayh * create 2021/12/21 0021 15:12 @@ -11,8 +14,10 @@ import lombok.Data; public class ImageInfo { private Integer id; private Integer docId; - private String operateDate; - private String operateTime; + @DateFormatAn("yyyy-MM-dd") + private Date operateDate; + @DateFormatAn("HH:mm:ss") + private Date operateTime; private Integer imageFileId; private String imageFileName; diff --git a/src/test/java/mybatisTest/mapper/IMapperTest.java b/src/test/java/mybatisTest/mapper/IMapperTest.java new file mode 100644 index 0000000..2393595 --- /dev/null +++ b/src/test/java/mybatisTest/mapper/IMapperTest.java @@ -0,0 +1,36 @@ +package mybatisTest.mapper; + +import aiyh.utils.annotation.recordset.ParamMapper; +import aiyh.utils.annotation.recordset.Select; +import aiyh.utils.annotation.recordset.SqlMapper; +import mybatisTest.entity.License; + +import java.util.Map; + +/** + * @author @author EBU7-dev1-ay + * create 2021/12/21 0021 21:52 + */ + + +//添加注解,标明需要对该类进行sql执行代理 +@SqlMapper +public interface IMapperTest { + +// 添加注解,表示改方法属于查询方法,并且返回一个map对象 + @Select("select * from table") + public Map selectOne(); + + @Select("select * from table where id = #{id}") + public Map selectOneById(int id); + + +// 使用多个参数需要使用@ParamMapper指定别名 + @Select("select * from table where id = #{id} and name = #{name}") + public Map selectOneByIdAndName(@ParamMapper("id") int id, + @ParamMapper("name") String name); + + // 使用多个参数需要使用@ParamMapper指定别名 + @Select("select * from table where license = #{license} and companyName = #{companyName}") + public Map selectOneByLicens(License license); +} diff --git a/src/test/java/pcn/copy/TestCopy.java b/src/test/java/pcn/copy/TestCopy.java new file mode 100644 index 0000000..968a19f --- /dev/null +++ b/src/test/java/pcn/copy/TestCopy.java @@ -0,0 +1,40 @@ +package pcn.copy; + +import com.alibaba.fastjson.JSON; +import com.api.aiyh_pcn.multipleCopy.pojo.CopyMultipleMain; +import com.api.aiyh_pcn.multipleCopy.services.MultipleCopyService; +import com.api.aiyh_pcn.multipleCopy.vo.MultipleCopyConfigVO; +import org.apache.commons.lang.StringUtils; +import org.junit.Before; +import org.junit.Test; + +/** + * @author EBU7-dev1-ayh + * create 2021/12/22 0022 17:27 + */ + + +public class TestCopy { + + @Before + public void before() { + weaver.general.GCONST.setServerName("ecology"); + weaver.general.GCONST.setRootPath("H:\\ecology-9-dev\\src\\main\\resources\\"); + } + + @Test + public void test(){ + MultipleCopyService multipleCopyService = new MultipleCopyService(); + MultipleCopyConfigVO configInfo = multipleCopyService.getConfigInfo("48"); + System.out.println(JSON.toJSONString(configInfo)); + } + + @Test + public void testUtil(){ + String str = "1,2"; + String join = StringUtils.join(str.split(","), ","); + System.out.println(join); + } + + +} diff --git a/src/test/java/utilTest/ITestMapper.java b/src/test/java/utilTest/ITestMapper.java index 0c25b91..9becf46 100644 --- a/src/test/java/utilTest/ITestMapper.java +++ b/src/test/java/utilTest/ITestMapper.java @@ -19,7 +19,7 @@ public interface ITestMapper { @ParamMapper("map") Map map); @Select(custom = true) - void selectOne(@SqlString String sql,String name); + License selectOne(@SqlString String sql); @Select("select * from lj where id = #{id} and name like #{nameLike} and age > #{age}") Object selectOne(@ParamMapper("id") String id, @@ -28,18 +28,20 @@ public interface ITestMapper { @Select("select * from table where id = #{id} and name like #{nameLike} and age > ${age}" + " and account = #{user.account} or userid = {user.uID}") + @CaseConversion(false) Object selectOne(Map map); @Select("select companyname company_name,license,expiredate expire_date,CVERSION c_version from license;") License selectOne(); - @Select("select imagefileid image_file_id,imagefilename image_file_name," + + @Select(value = "select imagefileid image_file_id,imagefilename image_file_name," + "docid doc_id,id,operatedate operate_date,operatetime operate_time" + - " from docimagefile where docid in (${list})") - List> selectList(List list); + " from docimagefile where docid in (${list})",custom = true) + List selectList(@SqlString String sql, List list); @Update("update hrmresource set email = #{email} where id > #{min} and id < #{max}") + void updateEmail(@ParamMapper("email") String email, @ParamMapper("min") int min, @ParamMapper("max") int max); diff --git a/src/test/java/utilTest/UtilTest.java b/src/test/java/utilTest/UtilTest.java index 63afd39..d3fdd68 100644 --- a/src/test/java/utilTest/UtilTest.java +++ b/src/test/java/utilTest/UtilTest.java @@ -1,13 +1,19 @@ package utilTest; -import aiyh.utils.recordset.RecordsetUtil; +import aiyh.utils.Util; +import aiyh.utils.httpUtil.ResponeVo; +import aiyh.utils.httpUtil.util.HttpUtils; +import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl; +import aiyh.utils.sqlUtil.whereUtil.Where; +import com.alibaba.fastjson.JSON; import entity.ImageInfo; -import mybatisTest.entity.License; +import mybatisTest.mapper.IMapperTest; import org.apache.commons.lang3.StringUtils; import org.junit.Before; import org.junit.Test; -import weaver.hrm.User; +import weaver.conn.RecordSet; +import java.io.IOException; import java.lang.reflect.*; import java.util.*; import java.util.regex.Matcher; @@ -21,79 +27,6 @@ import java.util.regex.Pattern; public class UtilTest { - @Before - public void before() { - weaver.general.GCONST.setServerName("ecology"); - weaver.general.GCONST.setRootPath("H:\\ecology-9-dev\\src\\main\\resources\\"); - } - - @Test - public void test() { - int i = 0; - testInstanceOfType(i); - Integer j = 10; - testInstanceOfType(j); - } - - public void testInstanceOfType(Object obj) { - System.out.println(obj.getClass().isPrimitive()); - System.out.println(obj instanceof Number); - } - - - @Test - public void testRecordsetUtil() { - RecordsetUtil recordsetUtil = new RecordsetUtil(); - ITestMapper mapper = recordsetUtil.getMapper(ITestMapper.class); -// User user = new User(); -// user.setUid(1002); -// user.setAccount("aiyh"); -// Map map = new HashMap() { -// { -// put("id", 10); -// put("nameLike", "%不知道%"); -// put("age", 20); -// put("user", user); -// } -// }; -// Object o = mapper.selectOne(map); -// o = mapper.selectOne(); -// o = mapper.selectOne("234", "%好的吧%", 30); -// System.out.println(o); -// License stringObjectMap = mapper.selectOne(); -// System.out.println(stringObjectMap); -// List> imageInfos = mapper.selectList(new ArrayList(){{ -// add(1); -// add(2); -// add(3); -// add(4); -// }}); -// System.out.println(imageInfos); - mapper.updateEmail("1234567@qq.com",20,40); - } - - - @Test - public void testTypeMethod() throws NoSuchMethodException, InstantiationException, IllegalAccessException { - Method getList = this.getClass().getMethod("getList"); - Class returnType = getList.getReturnType(); - - System.out.println(returnType.equals(void.class)); - //获取返回值的类型 - Type genericReturnType = getList.getGenericReturnType(); - //获取返回值的泛型参数 - Type actualTypeArgument = ((ParameterizedType) genericReturnType).getActualTypeArguments()[0]; - Class rawType = getRawType(actualTypeArgument); - if(rawType.equals(Map.class)){ - rawType = HashMap.class; - } - Object o = rawType.newInstance(); - System.out.println(rawType); - System.out.println( o); - // Class type = (Class) actualTypeArgument.getClass().newInstance(); -// System.out.println(type); - } - public static Class getRawType(Type type) { if (type instanceof Class) { return (Class) type; @@ -114,6 +47,83 @@ public class UtilTest { } } + @Before + public void before() { + weaver.general.GCONST.setServerName("ecology"); + weaver.general.GCONST.setRootPath("H:\\ecology-9-dev\\src\\main\\resources\\"); + } + + @Test + public void test() { + int i = 0; + testInstanceOfType(i); + Integer j = 10; + testInstanceOfType(j); + } + + public void testInstanceOfType(Object obj) { + System.out.println(obj.getClass().isPrimitive()); + System.out.println(obj instanceof Number); + } + + @Test + public void testRecordsetUtil() { +// RecordsetUtil recordsetUtil = new RecordsetUtil(); +// ITestMapper mapper = recordsetUtil.getMapper(ITestMapper.class); +// User user = new User(); +// user.setUid(1002); +// user.setAccount("aiyh"); +// Map map = new HashMap() { +// { +// put("id", 10); +// put("nameLike", "%不知道%"); +// put("age", 20); +// put("user", user); +// } +// }; +// Object o = mapper.selectOne(map); +// o = mapper.selectOne(); +// o = mapper.selectOne("234", "%好的吧%", 30); +// System.out.println(o); +// License stringObjectMap = mapper.selectOne(); +// System.out.println(stringObjectMap); + ITestMapper mapper = Util.getMapper(ITestMapper.class); +// License license = mapper.selectOne("select companyname company_name,license,expiredate expire_date,CVERSION c_version from license"); +// System.out.println(license); + List imageInfos = mapper.selectList("select imagefileid image_file_id,imagefilename image_file_name," + + "docid doc_id,id,operatedate operate_date,operatetime operate_time" + + " from docimagefile where docid in (${list})", new ArrayList() { + { + add(1); + add(2); + add(3); + add(4); + } + }); + System.out.println(imageInfos); +// mapper.updateEmail("1234567@qq.com",20,40); + } + + @Test + public void testTypeMethod() throws NoSuchMethodException, InstantiationException, IllegalAccessException { + Method getList = this.getClass().getMethod("getList"); + Class returnType = getList.getReturnType(); + + System.out.println(returnType.equals(void.class)); + //获取返回值的类型 + Type genericReturnType = getList.getGenericReturnType(); + //获取返回值的泛型参数 + Type actualTypeArgument = ((ParameterizedType) genericReturnType).getActualTypeArguments()[0]; + Class rawType = getRawType(actualTypeArgument); + if (rawType.equals(Map.class)) { + rawType = HashMap.class; + } + Object o = rawType.newInstance(); + System.out.println(rawType); + System.out.println(o); + // Class type = (Class) actualTypeArgument.getClass().newInstance(); +// System.out.println(type); + } public void getList() { return; @@ -155,7 +165,7 @@ public class UtilTest { add(3); add(4); }}; - System.out.println(StringUtils.join(integers,",")); + System.out.println(StringUtils.join(integers, ",")); String parse = "id.public.test.omg"; String pattern = "(?\\w+)\\.*(?(\\S+)*)"; Pattern compile = Pattern.compile(pattern); @@ -167,5 +177,84 @@ public class UtilTest { } + @Test + public void builderSelect(){ +// 将以下数据插入到数据库中,比如 inset into name ,email values('update_name','update_email') + Map map = new HashMap<>(); + map.put("name","update_name"); + map.put("email","update_email"); + map.put("phone","update_phone"); + +// 构建SQL + PrepSqlResultImpl sqlResult = Util.createSqlBuilder().insertSql( + "tableName", map); +// 获取完整的SQL对象 + System.out.println(sqlResult); + RecordSet rs = new RecordSet(); +// 执行SQL + rs.executeUpdate(sqlResult.getSqlStr(),sqlResult.getArgs()); + } + + @Test + public void getRsValue(){ + String sql = "select * from table"; + RecordSet rs = new RecordSet(); + rs.executeQuery(sql); +// 将rs结果转化为Map,并不进行下划线转驼峰 +// List> result = Util.recordSet2MapList(rs,false); +// 将rs结果转化为Map,将下划线转为驼峰命名 + List> result = Util.recordSet2MapList(rs); + System.out.println(result); + +// 将执行结果转为javaBean对象 + sql = "select * from fileImage"; + rs.executeQuery(sql); +// 不进行驼峰转换 +// List imageInfos = Util.recordeSet2Array(rs, ImageInfo.class,false); + List imageInfos = Util.recordeSet2Array(rs, ImageInfo.class); + System.out.println(imageInfos); + } + + + + @Test + public void testSqlUtil(){ +// 获取代理对象 + IMapperTest mapper = Util.getMapper(IMapperTest.class); +// 调用方法并获取返回值 + Map result = mapper.selectOne(); + System.out.println(result); + } + + + @Test + public void testRequest(){ +// 构建工具实体 + HttpUtils httpUtils = new HttpUtils(); +// 获取该对象内的全局 请求头,可以对put来添加请求头信息 + Map header = httpUtils.getGlobalCache().header; +// 全局请求参数 + Map paramMap = httpUtils.getGlobalCache().paramMap; +// 调用方法 + try { + HashMap requestParams = new HashMap<>(); + HashMap requestHeader = new HashMap<>(); + ResponeVo responeVo = httpUtils.apiPost("url", requestParams, requestHeader); +// 获取相应结果String类型的 + String entityString = responeVo.getEntityString(); +// 获取响应结果并转为Map对象 + Map entityMap = responeVo.getEntityMap(); +// 获取相应状态码 + int code = responeVo.getCode(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testReadProp(){ + Map patentWall = Util.readProperties2Map("PatentWall", "aiyh.patentWall.search"); + System.out.println(JSON.toJSONString(patentWall)); + } }