今天再写项目时,遇到了下面的错误:
Caused by: org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'moudle' at row 1
### The error may involve com.imooc.sm.dao.LogDao.insert-Inline
### The error occurred while setting parameters
### SQL: insert into log values (?,?,?,?,?,?)
### Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'moudle' at row 1
; SQL []; Data truncation: Data too long for column 'moudle' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'moudle' at row 1
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:100)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy17.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy22.insert(Unknown Source)
at com.imooc.sm.service.impl.LogServiceImpl.addLoginLog(LogServiceImpl.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy23.addLoginLog(Unknown Source)
at com.imooc.sm.global.LogAdvice.log(LogAdvice.java:103)
at com.imooc.sm.global.LogAdvice.loginLog(LogAdvice.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:603)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:46)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.imooc.sm.controller.SelfController$$EnhancerBySpringCGLIB$$8a4cb8fe.login()
... 32 more
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'moudle' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3971)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 67 more
提取主要错误信息(如下):
1、Caused by: org.springframework.dao.DataIntegrityViolationException:
2、Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column ‘moudle’ at row 1
通过在网上查阅资料发现,DataIntegrityViolationException是指:insert或update语句违反了整体性,而我遇到的错误是.MysqlDataTruncation: Data truncation: Data too long for column ‘moudle’ at row 1。这个意思是是我在插入的“moudle”的这个字段的信息太长了。
然后我就 发现了代码中的错误:
log.setMoudle(joinPoint.getTarget().getClass()**.getName()**);
System.out.println(joinPoint.getTarget().getClass().getName());
上述代码中getName()用的不正确,应该用getSimpleName();
如果用getName(),则它的输出结果是:
com.imooc.sm.controller.SelfController
然后将它改为getSimpleName的话,
log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
System.out.println(joinPoint.getTarget().getClass().getSimpleName());
输出结果为:
SelfController
由此可见他们两个的区别:
根据API中的定义:
Class.getName():以String的形式,返回Class对象的“实体”名称;
Class.getSimpleName():获取源代码中给出的“底层类”简称。
再一个,我列出常见的springDAO异常:
异常 | 何时抛出 |
---|---|
CleanupFailureDataAccessException | 一项操作成功地执行,但在释放数据库资源时发生异常(例如,关闭一个Connection) |
DataAccessResourceFailureException | 数据访问资源彻底失败,例如不能连接数据库 |
DataIntegrityViolationException | Insert或Update数据时违反了完整性,例如违反了惟一性限制 |
DataRetrievalFailureException | 某些数据不能被检测到,例如不能通过关键字找到一条记录 |
DeadlockLoserDataAccessException | 当前的操作因为死锁而失败 |
IncorrectUpdateSemanticsDataAccessException | Update时发生某些没有预料到的情况,例如更改超过预期的记录数。当这个异常被抛出时,执行着的事务不会被回滚 |
InvalidDataAccessApiusageException | 一个数据访问的JAVA API没有正确使用,例如必须在执行前编译好的查询编译失败了 |
invalidDataAccessResourceUsageException | 错误使用数据访问资源,例如用错误的SQL语法访问关系型数据库 |
OptimisticLockingFailureException | 乐观锁的失败。这将由ORM工具或用户的DAO实现抛出 |
TypemismatchDataAccessException | Java类型和数据类型不匹配,例如试图把String类型插入到数据库的数值型字段中 |
UncategorizedDataAccessException | 有错误发生,但无法归类到某一更为具体的异常中 |