diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Intercept.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Intercept.java index 3051b2d7..676aa20f 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Intercept.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Intercept.java @@ -21,7 +21,10 @@ */ package fun.asgc.neutrino.core.aop; +import fun.asgc.neutrino.core.aop.interceptor.ExceptionHandler; +import fun.asgc.neutrino.core.aop.interceptor.Filter; import fun.asgc.neutrino.core.aop.interceptor.Interceptor; +import fun.asgc.neutrino.core.aop.interceptor.ResultAdvice; import java.lang.annotation.*; @@ -35,7 +38,39 @@ import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface Intercept { + /** + * 指定拦截器 + * @return + */ Class[] value() default {}; + + /** + * 排除拦截器 + * @return + */ Class[] exclude() default {}; + + /** + * 忽略一切全局拦截器 + * @return + */ boolean ignoreGlobal() default false; + + /** + * 指定过滤器 + * @return + */ + Class[] filter() default {}; + + /** + * 指定异常处理器 + * @return + */ + Class[] exceptionHandler() default {}; + + /** + * 指定结果处理器 + * @return + */ + Class[] resultAdvice() default {}; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Invocation.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Invocation.java index aa92284e..e081cb73 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Invocation.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Invocation.java @@ -55,9 +55,10 @@ public class Invocation { this.callback = callback; this.args = args; this.interceptors = InterceptorFactory.getListByTargetMethod(this.targetMethod); + this.returnValue = TypeUtil.getDefaultValue(this.targetMethod.getReturnType()); } - public void invoke() { + public void invoke() throws Exception { if (CollectionUtil.notEmpty(this.interceptors) && index < this.interceptors.size()) { this.interceptors.get(index++).intercept(this); } else { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InnerGlobalInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InnerGlobalInterceptor.java index e9212a38..9a4cf798 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InnerGlobalInterceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InnerGlobalInterceptor.java @@ -22,6 +22,7 @@ package fun.asgc.neutrino.core.aop.interceptor; import fun.asgc.neutrino.core.aop.Invocation; +import fun.asgc.neutrino.core.util.Assert; import fun.asgc.neutrino.core.util.CollectionUtil; import fun.asgc.neutrino.core.util.TypeUtil; import lombok.extern.slf4j.Slf4j; @@ -36,59 +37,57 @@ import java.util.List; */ @Slf4j public class InnerGlobalInterceptor implements Interceptor { - private static final List filterList = new ArrayList<>(); - private static final List resultAdviceList = new ArrayList<>(); - private static final List exceptionHandlerList = new ArrayList<>(); + /** + * 拦截器包装器 + */ + private static final InterceptorWrapper interceptorWrapper = new InterceptorWrapper(InnerGlobalInterceptor.class.getSimpleName()); @Override public void intercept(Invocation inv) { - try { - log.debug("内置顶层拦截器 class:{} method:{} args:{} before", inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs()); - if (CollectionUtil.notEmpty(filterList)) { - for (Filter filter : filterList) { - if (filter.filtration(inv.getTargetClass(), inv.getTargetMethod(), inv.getArgs())) { - return; - } - } - } - inv.invoke(); - Object result = inv.getReturnValue(); - log.debug("内置顶层拦截器 class:{} method:{} args:{} result:{} after", inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs(), result); - - if (CollectionUtil.notEmpty(resultAdviceList)) { - for (ResultAdvice advice : resultAdviceList) { - result = advice.advice(inv.getTargetClass(), inv.getTargetMethod(), result); - } - } - if (null == result) { - result = TypeUtil.getDefaultValue(inv.getReturnType()); - } - inv.setReturnValue(result); - - log.debug("内置顶层拦截器 class:{} method:{} args:{} result:{} finished.", inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs(), result); - } catch (Exception e) { - log.debug("内置顶层拦截器 class:{} method:{} args:{} exception.", inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs()); - if (CollectionUtil.notEmpty(exceptionHandlerList)) { - for (ExceptionHandler handler : exceptionHandlerList) { - if (handler.support(e)) { - Object result = handler.handle(e); - inv.setReturnValue(result); - return; - } - } - } - } + interceptorWrapper.intercept(inv); } + /** + * 注册过滤器 + * @param filter + */ public static synchronized void registerFilter(Filter filter) { - filterList.add(filter); + interceptorWrapper.registerFilter(filter); } + /** + * 注册过滤器 + * @param filterList + */ + public static synchronized void registerFilter(List filterList) { + interceptorWrapper.registerFilter(filterList); + } + + /** + * 注册结果处理器 + * @param resultAdvice + */ public static synchronized void registerResultAdvice(ResultAdvice resultAdvice) { - resultAdviceList.add(resultAdvice); + interceptorWrapper.registerResultAdvice(resultAdvice); } + /** + * 注册结果处理器 + * @param resultAdviceList + */ + public static synchronized void registerResultAdvice(List resultAdviceList) { + interceptorWrapper.registerResultAdvice(resultAdviceList); + } + + /** + * 注册异常处理器 + * @param exceptionHandler + */ public static synchronized void registerExceptionHandler(ExceptionHandler exceptionHandler) { - exceptionHandlerList.add(exceptionHandler); + interceptorWrapper.registerExceptionHandler(exceptionHandler); + } + + public static synchronized void registerExceptionHandler(List exceptionHandlerList) { + interceptorWrapper.registerExceptionHandler(exceptionHandlerList); } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/Interceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/Interceptor.java index 8cec35cc..d799d054 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/Interceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/Interceptor.java @@ -32,5 +32,5 @@ public interface Interceptor { * 拦截方法 * @param inv */ - void intercept(Invocation inv); + void intercept(Invocation inv) throws Exception; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java index 614c5bfc..39165d8f 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java @@ -28,6 +28,8 @@ import fun.asgc.neutrino.core.util.*; import java.lang.reflect.Method; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * 拦截器工厂 @@ -38,29 +40,123 @@ public class InterceptorFactory { private static final Cache, Interceptor> interceptorCache = new MemoryCache<>(); private static final List globalInterceptorList = Collections.synchronizedList(new ArrayList<>()); private static final Map> methodInterceptorListMap = new HashMap<>(); + private static final Cache, Filter> filterCache = new MemoryCache<>(); + private static final Cache, ResultAdvice> resultAdviceCache = new MemoryCache<>(); + private static final Cache, ExceptionHandler> exceptionHandlerCache = new MemoryCache<>(); static { registerGlobalInterceptor(InnerGlobalInterceptor.class); } - public static T get(Class clazz) { - return (T)LockUtil.doubleCheckProcess(() -> !interceptorCache.containsKey(clazz), + /** + * 获取或新建缓存bean实例 + * @param clazz + * @param cache + * @param + * @return + */ + private static T getOrNewCacheBean(Class clazz, Cache cache) { + return (T)LockUtil.doubleCheckProcess(() -> !cache.containsKey(clazz), clazz, () -> { try { if (BeanManager.getBean(clazz) != null) { - interceptorCache.set(clazz, BeanManager.getBean(clazz)); + cache.set(clazz, BeanManager.getBean(clazz)); } else { - interceptorCache.set(clazz, clazz.newInstance()); + cache.set(clazz, clazz.newInstance()); } } catch (InstantiationException|IllegalAccessException e) { throw new RuntimeException(e); } }, - () -> interceptorCache.get(clazz) + () -> cache.get(clazz) ); } + /** + * 获取拦截器实例 + * @param clazz + * @param + * @return + */ + public static T get(Class clazz) { + return (T)getOrNewCacheBean(clazz, interceptorCache); + } + + /** + * 获取过滤器实例 + * @param clazz + * @param + * @return + */ + private static T getFilter(Class clazz) { + return (T)getOrNewCacheBean(clazz, filterCache); + } + + /** + * 获取过滤器列表 + * @param classes + * @param + * @return + */ + private static List getFilterList(Class[] classes) { + if (ArrayUtil.isEmpty(classes)) { + return null; + } + return Stream.of(classes).map(c -> (T)getFilter(c)).filter(Objects::nonNull).collect(Collectors.toList()); + } + + /** + * 获取结果处理器实例 + * @param clazz + * @param + * @return + */ + private static T getResultAdvice(Class clazz) { + return (T)getOrNewCacheBean(clazz, resultAdviceCache); + } + + /** + * 获取结果处理器列表 + * @param classes + * @param + * @return + */ + private static List getResultAdviceList(Class[] classes) { + if (ArrayUtil.isEmpty(classes)) { + return null; + } + return Stream.of(classes).map(c -> (T)getResultAdvice(c)).filter(Objects::nonNull).collect(Collectors.toList()); + } + + /** + * 获取异常处理器实例 + * @param clazz + * @param + * @return + */ + private static T getExceptionHandler(Class clazz) { + return (T)getOrNewCacheBean(clazz, exceptionHandlerCache); + } + + /** + * 获取异常处理器列表 + * @param classes + * @param + * @return + */ + private static List getExceptionHandlerList(Class[] classes) { + if (ArrayUtil.isEmpty(classes)) { + return null; + } + return Stream.of(classes).map(c -> (T)getExceptionHandler(c)).filter(Objects::nonNull).collect(Collectors.toList()); + } + + /** + * 获取目标方法的拦截器实例集合 + * @param targetMethod + * @return + */ public static List getListByTargetMethod(Method targetMethod) { Assert.notNull(targetMethod, "目标方法不能为空!"); @@ -69,6 +165,8 @@ public class InterceptorFactory { () -> { List interceptors = new ArrayList<>(); interceptors.addAll(globalInterceptorList); + addInterceptorByAnnotation(interceptors, targetMethod.getDeclaringClass().getAnnotation(Intercept.class)); + addInterceptorByAnnotation(interceptors, targetMethod.getAnnotation(Intercept.class)); // 如果被代理方法所属类是一个接口,那么该接口所有继承接口链路上的注解都对该方法生效 if (ClassUtil.isInterface(targetMethod.getDeclaringClass())) { List> interfaceList = ReflectUtil.getInterfaceAll(targetMethod.getDeclaringClass()); @@ -78,13 +176,16 @@ public class InterceptorFactory { } } } - addInterceptorByAnnotation(interceptors, targetMethod.getDeclaringClass().getAnnotation(Intercept.class)); - addInterceptorByAnnotation(interceptors, targetMethod.getAnnotation(Intercept.class)); methodInterceptorListMap.put(targetMethod, interceptors); }, () -> methodInterceptorListMap.get(targetMethod)); } + /** + * 根据拦截器注解,将拦截器添加到拦截器集合中 + * @param interceptors + * @param intercept + */ private static void addInterceptorByAnnotation(List interceptors, Intercept intercept) { if (null == interceptors || null == intercept) { return; @@ -105,6 +206,22 @@ public class InterceptorFactory { } } } + if (ArrayUtil.notEmpty(intercept.filter()) || ArrayUtil.notEmpty(intercept.resultAdvice()) || ArrayUtil.notEmpty(intercept.exceptionHandler())) { + InterceptorWrapper wrapper = new InterceptorWrapper(); + List filterList = getFilterList(intercept.filter()); + List resultAdviceList = getResultAdviceList(intercept.resultAdvice()); + List exceptionHandlerList = getExceptionHandlerList(intercept.exceptionHandler()); + if (CollectionUtil.notEmpty(filterList)) { + wrapper.registerFilter(filterList); + } + if (CollectionUtil.notEmpty(resultAdviceList)) { + wrapper.registerResultAdvice(resultAdviceList); + } + if (CollectionUtil.notEmpty(exceptionHandlerList)) { + wrapper.registerExceptionHandler(exceptionHandlerList); + } + interceptors.add(wrapper); + } if (intercept.ignoreGlobal()) { interceptors.removeAll(globalInterceptorList); } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorWrapper.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorWrapper.java new file mode 100644 index 00000000..6533c96e --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorWrapper.java @@ -0,0 +1,144 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.aop.interceptor; + +import fun.asgc.neutrino.core.aop.Invocation; +import fun.asgc.neutrino.core.util.Assert; +import fun.asgc.neutrino.core.util.CollectionUtil; +import fun.asgc.neutrino.core.util.TypeUtil; +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.List; + +/** + * 拦截器的包装器 + * @author: aoshiguchen + * @date: 2022/6/30 + */ +@Slf4j +public class InterceptorWrapper implements Interceptor { + private final List filterList = new ArrayList<>(); + private final List resultAdviceList = new ArrayList<>(); + private final List exceptionHandlerList = new ArrayList<>(); + private String name; + + public InterceptorWrapper() { + this.name = this.getClass().getSimpleName(); + } + + public InterceptorWrapper(String name) { + this.name = name; + } + + @Override + public void intercept(Invocation inv) { + try { + log.debug("拦截器:{} class:{} method:{} args:{} before", this.name, inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs()); + if (CollectionUtil.notEmpty(filterList)) { + for (Filter filter : filterList) { + if (filter.filtration(inv.getTargetClass(), inv.getTargetMethod(), inv.getArgs())) { + return; + } + } + } + inv.invoke(); + Object result = inv.getReturnValue(); + log.debug("拦截器:{} class:{} method:{} args:{} result:{} after", this.name, inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs(), result); + + if (CollectionUtil.notEmpty(resultAdviceList)) { + for (ResultAdvice advice : resultAdviceList) { + result = advice.advice(inv.getTargetClass(), inv.getTargetMethod(), result); + } + } + if (null == result) { + result = TypeUtil.getDefaultValue(inv.getReturnType()); + } + inv.setReturnValue(result); + + log.debug("拦截器:{} class:{} method:{} args:{} result:{} finished.", this.name, inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs(), result); + } catch (Exception e) { + log.debug("拦截器:{} class:{} method:{} args:{} exception.", this.name, inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs()); + if (CollectionUtil.notEmpty(exceptionHandlerList)) { + for (ExceptionHandler handler : exceptionHandlerList) { + if (handler.support(e)) { + Object result = handler.handle(e); + if (null != result) { + inv.setReturnValue(result); + } + return; + } + } + } + } + } + + /** + * 注册过滤器 + * @param filter + */ + public synchronized void registerFilter(Filter filter) { + Assert.notNull(filter, "过滤器不能为空!"); + this.filterList.add(filter); + } + + /** + * 注册过滤器 + * @param filterList + */ + public synchronized void registerFilter(List filterList) { + Assert.notEmpty(filterList, "过滤器不能为空!"); + this.filterList.addAll(filterList); + } + + /** + * 注册结果处理器 + * @param resultAdvice + */ + public synchronized void registerResultAdvice(ResultAdvice resultAdvice) { + Assert.notNull(resultAdvice, "结果处理器不能为空!"); + this.resultAdviceList.add(resultAdvice); + } + + /** + * 注册结果处理器 + * @param resultAdviceList + */ + public synchronized void registerResultAdvice(List resultAdviceList) { + Assert.notEmpty(resultAdviceList, "结果处理器不能为空!"); + this.resultAdviceList.addAll(resultAdviceList); + } + + /** + * 注册异常处理器 + * @param exceptionHandler + */ + public synchronized void registerExceptionHandler(ExceptionHandler exceptionHandler) { + Assert.notNull(exceptionHandler, "异常处理器不能为空!"); + this.exceptionHandlerList.add(exceptionHandler); + } + + public synchronized void registerExceptionHandler(List exceptionHandlerList) { + Assert.notEmpty(exceptionHandlerList, "异常处理器不能为空!"); + this.exceptionHandlerList.addAll(exceptionHandlerList); + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/dao/Dao.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/dao/Dao.java index fa404708..f48e55d7 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/dao/Dao.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/dao/Dao.java @@ -22,6 +22,7 @@ package fun.asgc.neutrino.core.db.dao; import java.io.Serializable; +import java.sql.SQLException; import java.util.List; /** @@ -37,7 +38,7 @@ public interface Dao { * @param po * @return */ - T add(T po); + T add(T po) throws SQLException; //==============================================修改===================== @@ -70,13 +71,13 @@ public interface Dao { int delete(); //==============================================查询===================== - Long count(); - Long count(T po, String ...field); + Long count() throws SQLException; + Long count(T po, String ...field) throws SQLException; - T findOneById(Serializable id); - T findOne(T po, String ...field); - List find(); - List find(T po, String ...field); + T findOneById(Serializable id) throws SQLException; + T findOne(T po, String ...field) throws SQLException; + List find() throws SQLException; + List find(T po, String ...field) throws SQLException; List findPage(int beginNo, int pageSize); List findPage(T po,int beginNo, int pageSize, String ...field); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/dao/DefaultDaoImpl.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/dao/DefaultDaoImpl.java index af365820..d77bc405 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/dao/DefaultDaoImpl.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/dao/DefaultDaoImpl.java @@ -35,6 +35,7 @@ import fun.asgc.neutrino.core.util.ReflectUtil; import javax.sql.DataSource; import java.io.Serializable; import java.lang.reflect.Field; +import java.sql.SQLException; import java.util.*; /** @@ -88,7 +89,7 @@ public class DefaultDaoImpl implements Dao { } @Override - public T add(T po) { + public T add(T po) throws SQLException { SqlAndParams sqlAndParams = this.sqlDialect.add(po); jdbcTemplate.update(sqlAndParams.getSql()); Serializable idValue = getIdValue(po); @@ -109,13 +110,13 @@ public class DefaultDaoImpl implements Dao { } @Override - public Long count() { + public Long count() throws SQLException { SqlAndParams sqlAndParams = this.sqlDialect.count(entryClass, null); return jdbcTemplate.queryForLong(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } @Override - public Long count(T po, String... field) { + public Long count(T po, String... field) throws SQLException { Set filter = Sets.newHashSet(field); Cache fieldCache = DbCache.getFieldCache(entryClass); SqlAndParams sqlAndParams = this.sqlDialect.count(entryClass, new LinkedHashMap(){ @@ -132,7 +133,7 @@ public class DefaultDaoImpl implements Dao { } @Override - public T findOneById(Serializable id) { + public T findOneById(Serializable id) throws SQLException { SqlAndParams sqlAndParams = this.sqlDialect.find(entryClass, new LinkedHashMap(){ { this.put(idColumnName, id); @@ -142,7 +143,7 @@ public class DefaultDaoImpl implements Dao { } @Override - public T findOne(T po, String... field) { + public T findOne(T po, String... field) throws SQLException { Set filter = Sets.newHashSet(field); Cache fieldCache = DbCache.getFieldCache(entryClass); SqlAndParams sqlAndParams = this.sqlDialect.find(entryClass, new LinkedHashMap(){ @@ -159,13 +160,13 @@ public class DefaultDaoImpl implements Dao { } @Override - public List find() { + public List find() throws SQLException { SqlAndParams sqlAndParams = this.sqlDialect.find(entryClass, null); return jdbcTemplate.queryForList(entryClass, sqlAndParams); } @Override - public List find(T po, String... field) { + public List find(T po, String... field) throws SQLException { Set filter = Sets.newHashSet(field); Cache fieldCache = DbCache.getFieldCache(entryClass); SqlAndParams sqlAndParams = this.sqlDialect.find(entryClass, new LinkedHashMap(){ diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java index f1253624..c78171af 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java @@ -34,6 +34,7 @@ import lombok.Data; import lombok.experimental.Accessors; import java.lang.reflect.Method; +import java.sql.SQLException; import java.util.Collection; import java.util.Map; @@ -50,12 +51,13 @@ public class SqlMapperInterceptor implements Interceptor { private static final Cache paramsCache = new MemoryCache<>(); @Override - public void intercept(Invocation inv) { + public void intercept(Invocation inv) throws SQLException { Assert.notNull(jdbcTemplate, "JdbcTemplate未注入,调用失败!"); Params params = getParams(inv.getTargetMethod()); if (null == params) { return; } + String sql = params.getSql(); Class resultType = params.getResultType(); Object res = null; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcCallback.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcCallback.java index 1cf60465..341687eb 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcCallback.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcCallback.java @@ -21,6 +21,8 @@ */ package fun.asgc.neutrino.core.db.template; +import java.sql.SQLException; + /** * @author: aoshiguchen * @date: 2022/6/27 @@ -31,6 +33,6 @@ public interface JdbcCallback { * 执行 * @return */ - T execute(); + T execute() throws SQLException; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcOperations.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcOperations.java index f960d431..a796127a 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcOperations.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcOperations.java @@ -53,15 +53,8 @@ public class JdbcOperations { * @param * @return */ - public T execute(JdbcCallback callback) { - T res = null; - try { - res = callback.execute(); - } catch (Exception e) { - throw new RuntimeException(e); - } - - return res; + public T execute(JdbcCallback callback) throws SQLException { + return callback.execute(); } /** @@ -71,17 +64,11 @@ public class JdbcOperations { * @param params * @return */ - public int executeUpdate(final Connection conn , final String sql, final Object[] params) { + public int executeUpdate(final Connection conn , final String sql, final Object[] params) throws SQLException { return this.execute(new PreparedStatementJdbcCallback(){ @Override - public Integer execute(PreparedStatement ps) { - int res = -1; - try{ - res = ps.executeUpdate(); - }catch(SQLException e){ - throw new RuntimeException(e); - } - return res; + public Integer execute(PreparedStatement ps) throws SQLException { + return ps.executeUpdate(); } @Override public Object[] getParams() { @@ -107,7 +94,7 @@ public class JdbcOperations { * @param * @return */ - public T executeQuery(final Connection conn,final Class clazz,final String sql,final Object[] params) { + public T executeQuery(final Connection conn,final Class clazz,final String sql,final Object[] params) throws SQLException { return this.execute(new PreparedStatementJdbcCallback() { @Override @@ -172,7 +159,7 @@ public class JdbcOperations { * @param * @return */ - public List executeQueryForList(final Connection conn, final Class clazz, final String sql, final Object[] params) { + public List executeQueryForList(final Connection conn, final Class clazz, final String sql, final Object[] params) throws SQLException { return this.execute(new PreparedStatementJdbcCallback>() { @Override public List execute(PreparedStatement ps) { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcTemplate.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcTemplate.java index f5c018bf..29257d3d 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcTemplate.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/JdbcTemplate.java @@ -52,15 +52,13 @@ public class JdbcTemplate { this.jdbcOperations = JdbcOperations.getInstance(); } - public int update(String sql, Object ...params){ + public int update(String sql, Object ...params) throws SQLException { int res = -1; Connection conn = null; try { conn = dataSourceHolder.getConnection(); res = jdbcOperations.executeUpdate(conn,sql, params); - } catch (SQLException e) { - throw new RuntimeException(e); } finally { try { dataSourceHolder.tryClose(conn); @@ -72,27 +70,25 @@ public class JdbcTemplate { return res; } - public int update(SqlAndParams sqlAndParams) { + public int update(SqlAndParams sqlAndParams) throws SQLException { return update(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public int updateByMap(String sql, Map params){ + public int updateByMap(String sql, Map params) throws SQLException { return update(new SqlAndParams(sql, params)); } - public int updateByModel(String sql, Object model){ + public int updateByModel(String sql, Object model) throws SQLException { return update(new SqlAndParams(sql, model)); } - public T query(Class clazz, String sql, Object ...params){ + public T query(Class clazz, String sql, Object ...params) throws SQLException { T res = null; Connection conn = null; try { conn = dataSourceHolder.getConnection(); res = jdbcOperations.executeQuery(conn, clazz,sql, params); - } catch (SQLException e) { - new RuntimeException(e); } finally { try { dataSourceHolder.tryClose(conn); @@ -104,167 +100,165 @@ public class JdbcTemplate { return res; } - public T query(Class clazz, SqlAndParams sqlAndParams) { + public T query(Class clazz, SqlAndParams sqlAndParams) throws SQLException { return query(clazz, sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public T queryByMap(Class clazz, String sql, Map params){ + public T queryByMap(Class clazz, String sql, Map params) throws SQLException { return query(clazz, new SqlAndParams(sql, params)); } - public T queryByModel(Class clazz, String sql, Object model){ + public T queryByModel(Class clazz, String sql, Object model) throws SQLException { return query(clazz, new SqlAndParams(sql, model)); } - public byte queryForByteByMap(String sql, Map params){ + public byte queryForByteByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForByte(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public byte queryForByteByModel(String sql, Object model){ + public byte queryForByteByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForByte(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public byte queryForByte(String sql, Object ...params){ + public byte queryForByte(String sql, Object ...params) throws SQLException { return query(byte.class, sql, params); } - public short queryForShortByMap(String sql, Map params){ + public short queryForShortByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForShort(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public short queryForShortByModel(String sql, Object model){ + public short queryForShortByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForShort(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public short queryForShort(String sql, Object ...params){ + public short queryForShort(String sql, Object ...params) throws SQLException { return query(short.class, sql, params); } - public int queryForIntByMap(String sql, Map params){ + public int queryForIntByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForInt(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public int queryForIntByModel(String sql, Object model){ + public int queryForIntByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForInt(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public int queryForInt(String sql, Object ...params){ + public int queryForInt(String sql, Object ...params) throws SQLException { return query(int.class, sql, params); } - public Long queryForLongByMap(String sql, Map params){ + public Long queryForLongByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForLong(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public Long queryForLongByModel(String sql, Object model){ + public Long queryForLongByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForLong(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public Long queryForLong(String sql,Object ...params){ + public Long queryForLong(String sql,Object ...params) throws SQLException { return query(long.class, sql, params); } - public float queryForFloatByMap(String sql, Map params){ + public float queryForFloatByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForFloat(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public float queryForFloatByModel(String sql, Object model){ + public float queryForFloatByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForFloat(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public float queryForFloat(String sql,Object ...params){ + public float queryForFloat(String sql,Object ...params) throws SQLException { return query(float.class, sql, params); } - public double queryForDoubleByMap(String sql, Map params){ + public double queryForDoubleByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForDouble(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public double queryForDoubleByModel(String sql, Object model){ + public double queryForDoubleByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForDouble(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public double queryForDouble(String sql, Object ...params){ + public double queryForDouble(String sql, Object ...params) throws SQLException { return query(double.class, sql, params); } - public char queryForCharByMap(String sql, Map params){ + public char queryForCharByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForChar(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public char queryForCharByModel(String sql, Object model){ + public char queryForCharByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForChar(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public char queryForChar(String sql, Object ...params){ + public char queryForChar(String sql, Object ...params) throws SQLException { return query(char.class, sql, params); } - public boolean queryForBooleanByMap(String sql, Map params){ + public boolean queryForBooleanByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForBoolean(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public boolean queryForBooleanByModel(String sql, Object model){ + public boolean queryForBooleanByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForBoolean(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public boolean queryForBoolean(String sql, Object ...params){ + public boolean queryForBoolean(String sql, Object ...params) throws SQLException { return query(boolean.class, sql, params); } - public String queryForStringByMap(String sql, Map params){ + public String queryForStringByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForString(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public String queryForStringByModel(String sql, Object model){ + public String queryForStringByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForString(sql, sqlAndParams.getParamArray()); } - public String queryForString(String sql, Object ...params){ + public String queryForString(String sql, Object ...params) throws SQLException { return query(String.class, sql, params); } - public Map queryForMapByMap(String sql, Map params){ + public Map queryForMapByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForMap(sql, sqlAndParams.getParamArray()); } - public Map queryForMapByModel(String sql, Object model){ + public Map queryForMapByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForMap(sql, sqlAndParams.getParamArray()); } - public Map queryForMap(String sql, Object ...params){ + public Map queryForMap(String sql, Object ...params) throws SQLException { return (Map)query(HashMap.class, sql, params); } - public List queryForList(Class clazz, String sql, Object ...params){ + public List queryForList(Class clazz, String sql, Object ...params) throws SQLException { List res = null; Connection conn = null; try { conn = dataSourceHolder.getConnection(); res = jdbcOperations.executeQueryForList(conn, clazz, sql, params); - } catch (SQLException e) { - throw new RuntimeException(e); } finally { try { dataSourceHolder.tryClose(conn); @@ -276,156 +270,156 @@ public class JdbcTemplate { return res; } - public List queryForList(Class clazz, SqlAndParams sqlAndParams) { + public List queryForList(Class clazz, SqlAndParams sqlAndParams) throws SQLException { return queryForList(clazz, sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListByMap(Class clazz, String sql, Map params){ + public List queryForListByMap(Class clazz, String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForList(clazz, sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListByModel(Class clazz, String sql, Object model){ + public List queryForListByModel(Class clazz, String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForList(clazz, sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListByteByMap(String sql, Map params){ + public List queryForListByteByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListByte(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListByteByModel(String sql, Object model){ + public List queryForListByteByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListByte(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListByte(String sql,Object ...params){ + public List queryForListByte(String sql,Object ...params) throws SQLException { return queryForList(byte.class, sql,params); } - public List queryForListShortByMap(String sql, Map params){ + public List queryForListShortByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListShort(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListShortByModel(String sql, Object model){ + public List queryForListShortByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListShort(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListShort(String sql, Object ...params){ + public List queryForListShort(String sql, Object ...params) throws SQLException { return queryForList(short.class, sql, params); } - public List queryForListIntByMap(String sql, Map params){ + public List queryForListIntByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListInt(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListIntByModel(String sql, Object model){ + public List queryForListIntByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListInt(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListInt(String sql, Object ...params){ + public List queryForListInt(String sql, Object ...params) throws SQLException { return queryForList(int.class, sql, params); } - public List queryForListLongByMap(String sql, Map params){ + public List queryForListLongByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListLong(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListLongByModel(String sql, Object model){ + public List queryForListLongByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListLong(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListLong(String sql,Object ...params){ + public List queryForListLong(String sql,Object ...params) throws SQLException { return queryForList(long.class, sql, params); } - public List queryForListFloatByMap(String sql, Map params){ + public List queryForListFloatByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListFloat(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListFloatByModel(String sql, Object model){ + public List queryForListFloatByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListFloat(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListFloat(String sql, Object ...params){ + public List queryForListFloat(String sql, Object ...params) throws SQLException { return queryForList(float.class, sql, params); } - public List queryForListDoubleByMap(String sql ,Map params){ + public List queryForListDoubleByMap(String sql ,Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListDouble(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListDoubleByModel(String sql, Object model){ + public List queryForListDoubleByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListDouble(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListDouble(String sql, Object ...params){ + public List queryForListDouble(String sql, Object ...params) throws SQLException { return queryForList(double.class, sql, params); } - public List queryForListCharByMap(String sql, Map params){ + public List queryForListCharByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListChar(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListCharByModel(String sql, Object model){ + public List queryForListCharByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListChar(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListChar(String sql, Object ...params){ + public List queryForListChar(String sql, Object ...params) throws SQLException { return queryForList(char.class, sql, params); } - public List queryForListBooleanByMap(String sql, Map params){ + public List queryForListBooleanByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListBoolean(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListBooleanByModel(String sql, Object model){ + public List queryForListBooleanByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListBoolean(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListBoolean(String sql, Object ...params){ + public List queryForListBoolean(String sql, Object ...params) throws SQLException { return queryForList(boolean.class, sql, params); } - public List queryForListStringByMap(String sql, Map params){ + public List queryForListStringByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListString(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListStringByModel(String sql, Object model){ + public List queryForListStringByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListString(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListString(String sql, Object ...params){ + public List queryForListString(String sql, Object ...params) throws SQLException { return queryForList(String.class, sql, params); } - public List queryForListMap(String sql, Object ...params){ + public List queryForListMap(String sql, Object ...params) throws SQLException { return queryForList(Map.class, sql, params); } - public List queryForListMapByMap(String sql, Map params){ + public List queryForListMapByMap(String sql, Map params) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, params); return queryForListMap(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } - public List queryForListMapByModel(String sql, Object model){ + public List queryForListMapByModel(String sql, Object model) throws SQLException { SqlAndParams sqlAndParams = new SqlAndParams(sql, model); return queryForListMap(sqlAndParams.getSql(), sqlAndParams.getParamArray()); } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/PreparedStatementJdbcCallback.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/PreparedStatementJdbcCallback.java index 7b2ff02d..d50c802c 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/PreparedStatementJdbcCallback.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/PreparedStatementJdbcCallback.java @@ -26,6 +26,7 @@ import lombok.extern.slf4j.Slf4j; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.SQLException; /** * @@ -36,35 +37,32 @@ import java.sql.PreparedStatement; public abstract class PreparedStatementJdbcCallback implements JdbcCallback { @Override - public T execute() { + public T execute() throws SQLException { PreparedStatement pstm = null; Object[] params = this.getParams(); Connection conn = getConnection(); T res = null; - try { - log.debug("sql:" + this.getSql()); - StringBuffer sb = new StringBuffer(); - if (ArrayUtil.notEmpty(params)) { - for(Object o : params){ - sb.append(o.toString()).append(","); - } - if(sb.length() > 0 && sb.charAt(sb.length() - 1) == ','){ - sb.deleteCharAt(sb.length() - 1); - } + log.debug("sql:" + this.getSql()); + StringBuffer sb = new StringBuffer(); + if (ArrayUtil.notEmpty(params)) { + for(Object o : params){ + sb.append(o.toString()).append(","); } - log.debug("params:" + sb.toString()); - pstm = conn.prepareStatement(this.getSql()); - if (ArrayUtil.notEmpty(params)) { - for(int i = 0;i < params.length;i++){ - pstm.setObject(i + 1, params[i]); - } + + if(sb.length() > 0 && sb.charAt(sb.length() - 1) == ','){ + sb.deleteCharAt(sb.length() - 1); } - res = this.execute(pstm); - } catch (Exception e) { - throw new RuntimeException(e); } + log.debug("params:" + sb.toString()); + pstm = conn.prepareStatement(this.getSql()); + if (ArrayUtil.notEmpty(params)) { + for(int i = 0;i < params.length;i++){ + pstm.setObject(i + 1, params[i]); + } + } + res = this.execute(pstm); return res; } @@ -86,7 +84,7 @@ public abstract class PreparedStatementJdbcCallback implements JdbcCallback userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); User user = new User(); user.setId(4L); @@ -63,14 +64,14 @@ public class DaoTest { } @Test - public void findOneById() { + public void findOneById() throws SQLException { Dao userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); User user = userDao.findOneById(3); System.out.println(user); } @Test - public void findOne1() { + public void findOne1() throws SQLException { Dao userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); User user = userDao.findOne(new User() .setId(1L), "id"); @@ -78,7 +79,7 @@ public class DaoTest { } @Test - public void findOne2() { + public void findOne2() throws SQLException { Dao userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); User user = userDao.findOne(new User() .setAge(23), "age"); @@ -86,7 +87,7 @@ public class DaoTest { } @Test - public void findOne3() { + public void findOne3() throws SQLException { Dao userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); User user = userDao.findOne(new User() .setAge(23) @@ -95,27 +96,27 @@ public class DaoTest { } @Test - public void find1() { + public void find1() throws SQLException { Dao userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); List userList = userDao.find(); System.out.println(userList); } @Test - public void find2() { + public void find2() throws SQLException { Dao userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); List userList = userDao.find(new User().setAge(21), "age"); System.out.println(userList); } @Test - public void count1() { + public void count1() throws SQLException { Dao userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); System.out.println(userDao.count()); } @Test - public void count2() { + public void count2() throws SQLException { Dao userDao = new DefaultDaoImpl<>(dataSource, dbType, User.class); System.out.println(userDao.count(new User().setAge(21), "age")); } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestExceptionHandler.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestExceptionHandler.java new file mode 100644 index 00000000..6fcddb23 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestExceptionHandler.java @@ -0,0 +1,38 @@ +/** + * Copyright (C) 2018-2022 Zeyi information technology (Shanghai) Co., Ltd. + *

+ * All right reserved. + *

+ * This software is the confidential and proprietary + * information of Zeyi Company of China. + * ("Confidential Information"). You shall not disclose + * such Confidential Information and shall use it only + * in accordance with the terms of the contract agreement + * you entered into with Zeyi inc. + */ +package fun.asgc.neutrino.core.db.mapper; + +import fun.asgc.neutrino.core.aop.interceptor.ExceptionHandler; +import lombok.extern.slf4j.Slf4j; + +import java.sql.SQLException; + +/** + * + * @author: aoshiguchen + * @date: 2022/6/30 + */ +@Slf4j +public class TestExceptionHandler implements ExceptionHandler { + + @Override + public boolean support(Exception e) { + return e instanceof SQLException; + } + + @Override + public Object handle(Exception e) { + log.error("SQL执行异常", e); + return null; + } +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/UserMapper.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/UserMapper.java index b1d59c54..f8f816c6 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/UserMapper.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/UserMapper.java @@ -22,6 +22,7 @@ package fun.asgc.neutrino.core.db.mapper; import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.aop.Intercept; import fun.asgc.neutrino.core.db.annotation.Insert; import fun.asgc.neutrino.core.db.annotation.ResultType; import fun.asgc.neutrino.core.db.annotation.Select; @@ -33,6 +34,7 @@ import java.util.List; * @author: aoshiguchen * @date: 2022/6/28 */ +@Intercept(exceptionHandler = TestExceptionHandler.class) @Component public interface UserMapper extends SqlMapper { diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/template/JdbcTemplateTest.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/template/JdbcTemplateTest.java index 3a0d9814..314a93ed 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/template/JdbcTemplateTest.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/template/JdbcTemplateTest.java @@ -27,6 +27,7 @@ import lombok.Data; import lombok.experimental.Accessors; import org.junit.Test; +import java.sql.SQLException; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -49,12 +50,12 @@ public class JdbcTemplateTest { } @Test - public void 新增数据1() { + public void 新增数据1() throws SQLException { jdbcTemplate.update("insert into user(`id`,`name`,`age`,`email`,`sex`,`create_time`) values(?,?,?,?,?,?)", 1, "张三", 21, "zhangsan@qq.com", "男", new Date()); } @Test - public void 数据新增2() { + public void 数据新增2() throws SQLException { Map params = new HashMap<>(); params.put("id", 2); params.put("name", "李四"); @@ -66,7 +67,7 @@ public class JdbcTemplateTest { } @Test - public void 数据新增3() { + public void 数据新增3() throws SQLException { User user = new User(); user.setId(3L); user.setName("王五"); @@ -78,12 +79,12 @@ public class JdbcTemplateTest { } @Test - public void 更新数据1() { + public void 更新数据1() throws SQLException { jdbcTemplate.update("update user set age = ? ,update_time = ? where id = ?", 31, new Date(), 1); } @Test - public void 更新数据2() { + public void 更新数据2() throws SQLException { Map params = new HashMap<>(); params.put("id", 2); params.put("updateTime", new Date()); @@ -92,7 +93,7 @@ public class JdbcTemplateTest { } @Test - public void 更新数据3() { + public void 更新数据3() throws SQLException { User user = new User(); user.setId(3L); user.setAge(33); @@ -101,68 +102,68 @@ public class JdbcTemplateTest { } @Test - public void 删除数据1() { + public void 删除数据1() throws SQLException { jdbcTemplate.update("delete from user where id = ?", 1L); } @Test - public void 删除数据2() { + public void 删除数据2() throws SQLException { Map params = new HashMap<>(); params.put("id", 2); jdbcTemplate.updateByMap("delete from user where id = :id", params); } @Test - public void 删除数据3() { + public void 删除数据3() throws SQLException { User user = new User(); user.setId(3L); jdbcTemplate.updateByModel("delete from user where id = :id", user); } @Test - public void 查询单个行记录1() { + public void 查询单个行记录1() throws SQLException { Map map = jdbcTemplate.queryForMap("select * from user where id = ?", 1); System.out.println(map); } @Test - public void 查询单个行记录2() { + public void 查询单个行记录2() throws SQLException { User user = jdbcTemplate.query(User.class, "select * from user where id = 1"); System.out.println(user); } @Test - public void 查询单个字段记录1() { + public void 查询单个字段记录1() throws SQLException { String name = jdbcTemplate.queryForString("select name from user where id = ?", 1); System.out.println(name); } @Test - public void 查询单个字段记录2() { + public void 查询单个字段记录2() throws SQLException { int age = jdbcTemplate.queryForInt("select age from user where id = 1"); System.out.println(age); } @Test - public void 查询单个字段记录3() { + public void 查询单个字段记录3() throws SQLException { Long count = jdbcTemplate.queryForLong("select count(1) from user"); System.out.println(count); } @Test - public void 查询多个行记录1() { + public void 查询多个行记录1() throws SQLException { List list = jdbcTemplate.queryForListMap("select * from user"); System.out.println(list); } @Test - public void 查询多个行记录2() { + public void 查询多个行记录2() throws SQLException { List list = jdbcTemplate.queryForList(User.class, "select * from user"); System.out.println(list); } @Test - public void 查询多个字段记录1() { + public void 查询多个字段记录1() throws SQLException { List list = jdbcTemplate.queryForListInt("select age from user"); System.out.println(list); }