# 连接数据库信息出错、
报错情况
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: No suitable driver found for mysql://127.0.0.1:3306/mybatis
### The error may exist in cn/vipblogs/dao/StudentDao.xml
### The error may involve cn.vipblogs.dao.StudentDao.selectAll
### The error occurred while executing a query
### Cause: java.sql.SQLException: No suitable driver found for mysql://127.0.0.1:3306/mybatis
2
3
4
5
6
对于这个问题,是因为我在配置连接数据库的时候,出错了
<dataSource type="POOLED">
<!--type:表示数据源的类型, POOLED表示使用连接池-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="mysql://127.0.0.1:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
2
3
4
5
6
7
<property name="url" value="mysql://127.0.0.1:3306/mybatis"/>
应该为
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis"/>
# 处理返回结果集报错
如果定义查询返回的返回值这样定义
<select id="selectAll" resultType="java.util.List">
select id,name,email,age from student
</select>
2
3
那么在执行程序,处理结果List<Student> list = sqlSession.selectList(sqlId);
就会出现异常,因为resultType="java.util.List"
定义的返回是一个List,但是接收使用List<Student>
就会出现异常
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.UnsupportedOperationException
### The error may exist in cn/vipblogs/dao/StudentDao.xml
### The error may involve cn.vipblogs.dao.StudentDao.selectAll
### The error occurred while handling results
### SQL: select id,name,email,age from student
### Cause: java.lang.UnsupportedOperationException
2
3
4
5
6
7
# 执行select语句时,出现不支持的类型
如果dao中的方法定义为
List<Map<String,Object>> selectAllMap();
xml中,使用下面的方式进行
<select id="selectAllMap" resultType="java.util.List">
就会报下面这个错,不支持的类型
Error querying database. Cause: java.lang.UnsupportedOperationException
在解决这个错误之前,应该知道一个概念,resultType=
的值,是每一条查询到的数据的返回类型,我们方法中定义的是List<Map<String,Object>>
,但是这个是所有的记录的总和是list,但是每一条数据是一个map集合,所以就出现了问题,这里改成map就可以了
<select id="selectAllMap" resultType="java.util.Map">
# 必须要有返回值类型
<select id="selectById" resultType="cn.vipblogs.domain.Student">
select * from student where id = #{id}
</select>
2
3
resultType="cn.vipblogs.domain.Student"
一定要写上返回值类型,否则报错,报错信息
### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'cn.vipblogs.dao.StudentDao.selectById'. It's likely that neither a Result Type nor a Result Map was specified.
# id标签名重复问题
如果标签中id的名称重复,那么会报以下错误
### The error may exist in cn/vipblogs/dao/StudentDao.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/vipblogs/dao/StudentDao.xml'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for cn.vipblogs.dao.StudentDao.selectById. please check cn/vipblogs/dao/StudentDao.xml and cn/vipblogs/dao/StudentDao.xml
2
mapper文件信息
<select id="selectById" parameterType="java.lang.Integer" resultType="cn.vipblogs.domain.Student">
select * from student where id = #{idd}
</select>
<select id="selectById" parameterType="java.lang.Integer" resultType="cn.vipblogs.domain.Student">
select * from student where id = #{id} or name=#{name}
</select>
2
3
4
5
6
7
id重复
# 出现异常,不匹配
如果出现下面的异常
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): vin.cco.dao.GoodsDao.selectById
那么就是无法找到dao接口文件的映射文件,这是一个很容易忽视的点,就是必须要保证接口名和mybatis的映射文件的名字要是一样的,否则就会报这个错误





