学习mybatis时遇到部分错误及解决

废话

刚开始学习时,自我感觉还是得先去学习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标签外面
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值