diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/JdbcSqlExecutorConfiguration.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/JdbcSqlExecutorConfiguration.java index f7ebe67bd..44c1c4082 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/JdbcSqlExecutorConfiguration.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/JdbcSqlExecutorConfiguration.java @@ -20,14 +20,14 @@ import javax.sql.DataSource; public class JdbcSqlExecutorConfiguration { @Bean @ConditionalOnMissingBean - public SyncSqlExecutor syncSqlExecutor() { - return new DefaultJdbcExecutor(); + public SyncSqlExecutor syncSqlExecutor(DataSource dataSource) { + return new DefaultJdbcExecutor(dataSource); } @Bean @ConditionalOnMissingBean - public ReactiveSqlExecutor reactiveSqlExecutor() { - return new DefaultJdbcReactiveExecutor(); + public ReactiveSqlExecutor reactiveSqlExecutor(DataSource dataSource) { + return new DefaultJdbcReactiveExecutor(dataSource); } } \ No newline at end of file diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/CrudService.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/CrudService.java index c3963b854..fea2ed262 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/CrudService.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/CrudService.java @@ -1,5 +1,6 @@ package org.hswebframework.web.crud.service; +import lombok.SneakyThrows; import org.apache.commons.collections4.CollectionUtils; import org.hswebframework.ezorm.core.param.QueryParam; import org.hswebframework.ezorm.rdb.mapping.SyncDelete; @@ -12,6 +13,7 @@ import org.hswebframework.web.api.crud.entity.QueryParamEntity; import org.hswebframework.web.api.crud.entity.TransactionManagers; import org.springframework.transaction.annotation.Transactional; +import java.sql.SQLException; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -33,12 +35,14 @@ public interface CrudService { } @Transactional( readOnly = true, transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default Optional findById(K id) { return getRepository() .findById(id); } @Transactional(readOnly = true, transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default List findById(Collection id) { if (CollectionUtils.isEmpty(id)) { return Collections.emptyList(); @@ -49,62 +53,69 @@ public interface CrudService { } @Transactional(rollbackFor = Throwable.class,transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default SaveResult save(Collection entityArr) { return getRepository() .save(entityArr); } @Transactional(rollbackFor = Throwable.class,transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default int insert(Collection entityArr) { return getRepository() .insertBatch(entityArr); } @Transactional(rollbackFor = Throwable.class, transactionManager = TransactionManagers.jdbcTransactionManager) - default void insert(E entityArr) { - getRepository() - .insert(entityArr); + default void insert(E entityArr){ + getRepository().insert(entityArr); } @Transactional(rollbackFor = Throwable.class,transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default int updateById(K id, E entityArr) { - return getRepository() - .updateById(id, entityArr); + return getRepository().updateById(id, entityArr); } @Transactional(rollbackFor = Throwable.class,transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default SaveResult save(E entity) { return getRepository() .save(Collections.singletonList(entity)); } @Transactional(rollbackFor = Throwable.class,transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default SaveResult save(List entities) { return getRepository() .save(entities); } @Transactional(rollbackFor = Throwable.class,transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default int deleteById(Collection idArr) { return getRepository().deleteById(idArr); } @Transactional(rollbackFor = Throwable.class,transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default int deleteById(K idArr) { return deleteById(Collections.singletonList(idArr)); } @Transactional(readOnly = true, transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default List query(QueryParamEntity queryParam) { return createQuery().setParam(queryParam).fetch(); } @Transactional(readOnly = true, transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default PagerResult queryPager(QueryParamEntity param) { int count = param.getTotal() == null ? count(param) : param.getTotal(); if (count == 0) { - return PagerResult.empty(); + return PagerResult.of(0,Collections.emptyList(),param); } param.rePaging(count); @@ -112,6 +123,7 @@ public interface CrudService { } @Transactional(readOnly = true, transactionManager = TransactionManagers.jdbcTransactionManager) + @SneakyThrows default int count(QueryParam param) { return getRepository() .createQuery() diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/sql/DefaultJdbcExecutor.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/sql/DefaultJdbcExecutor.java index abfb9eb58..bd6608c18 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/sql/DefaultJdbcExecutor.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/sql/DefaultJdbcExecutor.java @@ -1,5 +1,6 @@ package org.hswebframework.web.crud.sql; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.hswebframework.ezorm.rdb.executor.SqlRequest; import org.hswebframework.ezorm.rdb.executor.jdbc.JdbcSyncSqlExecutor; @@ -7,7 +8,10 @@ import org.hswebframework.ezorm.rdb.executor.wrapper.ResultWrapper; import org.hswebframework.web.api.crud.entity.TransactionManagers; import org.hswebframework.web.datasource.DataSourceHolder; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.jdbc.datasource.DataSourceUtils; +import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; +import org.springframework.jdbc.support.SQLExceptionTranslator; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @@ -25,6 +29,13 @@ public class DefaultJdbcExecutor extends JdbcSyncSqlExecutor { @Autowired private DataSource dataSource; + public DefaultJdbcExecutor() { + } + + public DefaultJdbcExecutor(DataSource dataSource) { + this.dataSource = dataSource; + } + protected String getDatasourceId() { return DataSourceHolder.switcher().datasource().current().orElse("default"); } @@ -33,8 +44,8 @@ public class DefaultJdbcExecutor extends JdbcSyncSqlExecutor { public Connection getConnection(SqlRequest sqlRequest) { DataSource dataSource = DataSourceHolder.isDynamicDataSourceReady() ? - DataSourceHolder.currentDataSource().getNative() : - this.dataSource; + DataSourceHolder.currentDataSource().getNative() : + this.dataSource; Connection connection = DataSourceUtils.getConnection(dataSource); boolean isConnectionTransactional = DataSourceUtils.isConnectionTransactional(connection, dataSource); if (log.isDebugEnabled()) { @@ -50,8 +61,8 @@ public class DefaultJdbcExecutor extends JdbcSyncSqlExecutor { } try { DataSource dataSource = DataSourceHolder.isDynamicDataSourceReady() ? - DataSourceHolder.currentDataSource().getNative() : - this.dataSource; + DataSourceHolder.currentDataSource().getNative() : + this.dataSource; DataSourceUtils.doReleaseConnection(connection, dataSource); } catch (SQLException e) { log.error(e.getMessage(), e); diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/sql/DefaultJdbcReactiveExecutor.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/sql/DefaultJdbcReactiveExecutor.java index 9c07e7602..2e7a17ad9 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/sql/DefaultJdbcReactiveExecutor.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/sql/DefaultJdbcReactiveExecutor.java @@ -24,14 +24,23 @@ public class DefaultJdbcReactiveExecutor extends JdbcReactiveSqlExecutor { @Autowired private DataSource dataSource; + @Deprecated + public DefaultJdbcReactiveExecutor() { + + } + + public DefaultJdbcReactiveExecutor(DataSource dataSource) { + this.dataSource = dataSource; + } + protected String getDatasourceId() { return DataSourceHolder.switcher().datasource().current().orElse("default"); } private Tuple2 getDataSourceAndConnection() { DataSource dataSource = DataSourceHolder.isDynamicDataSourceReady() ? - DataSourceHolder.currentDataSource().getNative() : - this.dataSource; + DataSourceHolder.currentDataSource().getNative() : + this.dataSource; Connection connection = DataSourceUtils.getConnection(dataSource); boolean isConnectionTransactional = DataSourceUtils.isConnectionTransactional(connection, dataSource); if (log.isDebugEnabled()) { @@ -44,56 +53,56 @@ public class DefaultJdbcReactiveExecutor extends JdbcReactiveSqlExecutor { @Override public Mono getConnection() { return Mono - .using( - this::getDataSourceAndConnection - , - tp2 -> Mono.just(tp2.getT2()), - tp2 -> DataSourceUtils.releaseConnection(tp2.getT2(), tp2.getT1()), - false - ); + .using( + this::getDataSourceAndConnection + , + tp2 -> Mono.just(tp2.getT2()), + tp2 -> DataSourceUtils.releaseConnection(tp2.getT2(), tp2.getT1()), + false + ); } @Override protected Flux doInConnection(Function> handler) { return Flux .using(this::getDataSourceAndConnection, - tp2 -> handler.apply(tp2.getT2()), - tp2 -> DataSourceUtils.releaseConnection(tp2.getT2(), tp2.getT1()) + tp2 -> handler.apply(tp2.getT2()), + tp2 -> DataSourceUtils.releaseConnection(tp2.getT2(), tp2.getT1()) ); } @Override - @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager,readOnly = true) + @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager, readOnly = true) public Flux select(String sql, ResultWrapper wrapper) { - return super.select(sql,wrapper); + return super.select(sql, wrapper); } @Override - @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager,rollbackFor = Throwable.class) + @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager, rollbackFor = Throwable.class) public Mono update(Publisher request) { return super.update(request); } @Override - @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager,rollbackFor = Throwable.class) + @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager, rollbackFor = Throwable.class) public Mono update(String sql, Object... args) { - return super.update(sql,args); + return super.update(sql, args); } @Override - @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager,rollbackFor = Throwable.class) + @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager, rollbackFor = Throwable.class) public Mono update(SqlRequest request) { return super.update(request); } @Override - @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager,rollbackFor = Throwable.class) + @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager, rollbackFor = Throwable.class) public Mono execute(Publisher request) { return super.execute(request); } @Override - @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager,rollbackFor = Throwable.class) + @Transactional(transactionManager = TransactionManagers.jdbcTransactionManager, rollbackFor = Throwable.class) public Mono execute(SqlRequest request) { return super.execute(request); } diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java index 6c21880e8..bd97d0ef7 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java @@ -13,6 +13,8 @@ import org.hswebframework.web.exception.ValidationException; import org.hswebframework.web.i18n.LocaleUtils; import org.hswebframework.web.logger.ReactiveLogger; import org.springframework.core.annotation.Order; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.DuplicateKeyException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.transaction.TransactionException; @@ -292,4 +294,21 @@ public class CommonErrorControllerAdvice { .map(msg -> ResponseMessage.error(400, e.getI18nCode(), msg)); } + @ExceptionHandler + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Mono> handleException(DataAccessException e) { + return LocaleUtils + .resolveMessageReactive("error.data_access_failed") + .map(msg -> ResponseMessage.error(400, "data_access_failed", msg)); + } + + @ExceptionHandler + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Mono> handleException(DuplicateKeyException e) { + return LocaleUtils + .resolveMessageReactive("error.duplicate_key") + .map(msg -> ResponseMessage.error(400, "duplicate_key", msg)); + } + + } diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebMvcErrorControllerAdvice.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebMvcErrorControllerAdvice.java index f5c2a6abe..2f6de6895 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebMvcErrorControllerAdvice.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebMvcErrorControllerAdvice.java @@ -13,6 +13,8 @@ import org.hswebframework.web.exception.ValidationException; import org.hswebframework.web.i18n.LocaleUtils; import org.hswebframework.web.logger.ReactiveLogger; import org.springframework.core.annotation.Order; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.DuplicateKeyException; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.BindException; @@ -243,4 +245,20 @@ public class CommonWebMvcErrorControllerAdvice { return ResponseMessage.error(400, e.getI18nCode(), resolveMessage(e)); } + @ExceptionHandler + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ResponseMessage handleException(DataAccessException e){ + return ResponseMessage.error(400, + "data_access_failed", + LocaleUtils.resolveMessage("error.data_access_failed")); + } + + @ExceptionHandler + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ResponseMessage handleException(DuplicateKeyException e){ + return ResponseMessage.error(400, + "duplicate_key", + LocaleUtils.resolveMessage("error.duplicate_key")); + } + } diff --git a/hsweb-commons/hsweb-commons-crud/src/main/resources/i18n/commons/messages_en.properties b/hsweb-commons/hsweb-commons-crud/src/main/resources/i18n/commons/messages_en.properties index 47df2c6d2..f5293f979 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/resources/i18n/commons/messages_en.properties +++ b/hsweb-commons/hsweb-commons-crud/src/main/resources/i18n/commons/messages_en.properties @@ -9,4 +9,6 @@ error.tree_entity_parent_id_not_exist=Parent node does not exist or has been del error.resource_not_found=Resource not found error.data.find.not_found=Data not found error.sql.prepare.failed.IndexOutOfBoundsException=Execute SQL failed, try check config: `easyorm.dialect`. -error.missing_request_body=Required request body is missing \ No newline at end of file +error.missing_request_body=Required request body is missing +error.duplicate_key=Duplicate Data +error.data_access_failed=Data Access Failed \ No newline at end of file diff --git a/hsweb-commons/hsweb-commons-crud/src/main/resources/i18n/commons/messages_zh.properties b/hsweb-commons/hsweb-commons-crud/src/main/resources/i18n/commons/messages_zh.properties index ae29dd68f..c0ee2ebd4 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/resources/i18n/commons/messages_zh.properties +++ b/hsweb-commons/hsweb-commons-crud/src/main/resources/i18n/commons/messages_zh.properties @@ -8,4 +8,6 @@ error.tree_entity_cyclic_dependency=\u4E0D\u80FD\u4FEE\u6539\u7236\u8282\u70B9\u error.tree_entity_parent_id_not_exist=\u7236\u8282\u70B9\u4E0D\u5B58\u5728\u6216\u5DF2\u88AB\u5220\u9664 error.data.find.not_found=\u6570\u636E\u4E0D\u5B58\u5728 error.sql.prepare.failed.IndexOutOfBoundsException=SQL\u6267\u884C\u5931\u8D25,\u8BF7\u5C1D\u8BD5\u68C0\u67E5`easyorm.dialect`\u914D\u7F6E. -error.missing_request_body=\u8BF7\u6C42\u4F53\u7F3A\u5931 \ No newline at end of file +error.missing_request_body=\u8BF7\u6C42\u4F53\u7F3A\u5931 +error.duplicate_key=\u5DF2\u5B58\u5728\u91CD\u590D\u7684\u6570\u636E +error.data_access_failed=\u8BBF\u95EE\u6570\u636E\u5931\u8D25 \ No newline at end of file diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/authentication-default/messages_en.properties b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/authentication-default/messages_en.properties index b2f495f82..65ff7fc0b 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/authentication-default/messages_en.properties +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/authentication-default/messages_en.properties @@ -1,3 +1,2 @@ -error.duplicate_key=Duplicate Data error.user_already_exists=User already exists error.user_not_found=The user does not exist or the id does not meet the rule \ No newline at end of file diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/authentication-default/messages_zh.properties b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/authentication-default/messages_zh.properties index 43724899f..84e18030c 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/authentication-default/messages_zh.properties +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/authentication-default/messages_zh.properties @@ -1,3 +1,2 @@ -error.duplicate_key=已存在重复的数据 -error.user_already_exists=用户已存在 -error.user_not_found=用户不存在或ID不符合规则:[{0}] \ No newline at end of file +error.user_already_exists=\u7528\u6237\u5DF2\u5B58\u5728 +error.user_not_found=\u7528\u6237\u4E0D\u5B58\u5728\u6216ID\u4E0D\u7B26\u5408\u89C4\u5219:[{0}] \ No newline at end of file