废话
刚开始学习时,自我感觉还是得先去学习Mybatis官方文档。
今天在学习mybatis时遇到的问题:
问题一
org.apache.ibatis.binding.BindingException: Type interface com.dao.UserDao is not known to the MapperRegistry.
问题主要是:Type interface com.dao.UserDao is not known to the MapperRegistry.(类型接口 com.dao.userdao 不为 mapperregistry 所知。)
问题二
该问题的出现是我把xml文件放到java目录下。。。。。。。(小声逼逼道:其实可以放在resource目录下,然后在核心配置文件的mappers那里使用短名称作为路径)
java.lang.ExceptionInInitializerError
问题简述:就是初始化失败,找不到文件
问题三:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 5,080 milliseconds ago. The last packet sent successfully to the server was 5,069 milliseconds ago.
### The error may exist in com/dao/UserMapper.xml
### The error may involve com.dao.UserDao.getUserList
### The error occurred while executing a query
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 5,080 milliseconds ago. The last packet sent successfully to the server was 5,069 milliseconds ago.
问题四
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='name', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
### The error may exist in com/dao/UserMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: /*select * from mybatis.user like ? ,第一种方式不推荐使用,会SQL注入*/ /* 推荐使用,防止SQL注入*/ select * from mybatis.user where `name` like "%"?"%"
### Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='name', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
原因是: 里面不能含有注释的SQL语句
解决方案:
问题一
在resources目录下mybatis-config.xml的核心配置文件里面添加映射
问题二
在pom.xml配置文件中加上,
<!--防止资源导出失败-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
问题三
问题四
把含有注释的SQL语句删掉或者放到select标签外面