SQL异常总结

1.resultTyperesultMap写错时,启动时就会报错

 原因:

2.The error occurred while handling results

### SQL: select USER_ID from user_dept   where COMP_ID=?

### Cause: java.lang.UnsupportedOperationException

原因:

查询出来的是个List集合时,list元素是对象时resultType为对象(或者用resultMap),list元素为USER_ID这种时resultType用String,而不应该使用list

3.org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException

: Parameter 'userIds' not found. Available parameters are [list]

 入参为List<String>类型,parameterType用String,需要的结果为List<String> ,resultType用String

 4.bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;

原因:

in() 时,输入的数组或集合必须不为空,否则报错,应该在业务层进行控制,为空时不能执行这条SQL 

 5.

6.java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.ss.tmall.mapper.PropertyMapper.selectByPrimaryKey

原因:Mapping  xml里面没有找到对应的namespace 

7.Unknown column 'XX' in 'field list'错误

在mysql中insert into语句中出现Unknown column 'XX' in 'field list'错误,一般有两种情况:

1、顾名思义,表里面没有这个列。就是所要插入的字段中所包含的XX,表格中没有这一列;

这里需要注意的是,有时候程序员粗心,可能在XX字段前面多加了个空格,空格在检查的时候不容易发现,却会使这个错误一直出现

2、要插入的数据不合符存在字段的要求,如字段类型是varchar,但是插入数据没有加''也会出现这样的错误

8.The used SELECT statements have a different number of columns

这是因为使用union的两个SQL语句产生的记录的表结构不一致. 必须是结构完全一致的记录集合才可以使用UNION. 

9.Caused by: java.sql.SQLException: Incorrect string value: 'xF0x9Fx90xB3aa...' for column 'Content' at row 1

原因;存储表情数据失败。

解决方案:

https://blog.csdn.net/dmw412724/article/details/81119325?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

https://blog.csdn.net/qq_31122833/article/details/83992085

10.Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

在进行修改数据库的时候出现了这个异常 
很明显这个是只读引起的

是事务问题了 
原因:你配置了只读事务 
解决办法:看下你的service层是否配置@Transactional(readOnly=true)

在spring的配置文件中:

 <aop:config
  <aop:advisor pointcut-ref="servicePointcut" advice-ref="txAdvice"/>
    </aop:config>

    <!-- 事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="query*" propagation="REQUIRED" read-only="true"/>
    </tx:attributes>

你是不是有类似于这样的,如果你的service方法名称是findpassword的话那么就会被拦截到了,然后就read-only了
所以改一下你的方法名称吧

或者在方法上添加注释
@Transactional(readOnly = false)

11.java从数据库查询出来的时间日期后面多了一个.0

这个.0其实代表的是纳秒。当我们数据库时间类型字段设置为datetime类型是,并且返回值用string类型接收的时候,把时间打印出来,会出现纳秒。

解决: 利用sql自带的函数在sql层转换为正常年月日时分秒。如:DATE_FORMAT(applyTime,'%Y-%m-%d %H:%i:%s')

https://blog.csdn.net/Z__Sheng/article/details/104723410

原文地址:https://www.cnblogs.com/lvhouhou/p/12067947.html