JDBC异常抽象
Spring会将数据操作的异常转换为DataAccessException
解析错误码
- SQLErrorCodeSQLExceptionTranslator
- ErrorCode定义
- org/springframework/jdbc/support/sql-error-codes.xml
- classpath下的sql-error-codes.xml(定制)
org/springframework/jdbc/support/sql-error-codes.xml
Default SQL error codes for well-known databases. Can be overridden by definitions in a “sql-error-codes.xml” file in the root of the class path.
1 | <bean id="H2" class="org.springframework.jdbc.support.SQLErrorCodes"> |
org.springframework.jdbc.support.SQLErrorCodes
1 | public class SQLErrorCodes { |
定制错误码
项目路径
1 | └── src |
sql-error-codes.xml
src/main/resources/sql-error-codes.xml
1 | <beans> |
CustomDuplicateKeyException
1 | public class CustomDuplicateKeyException extends DuplicateKeyException { |
单元测试
1 | (SpringRunner.class) |