01 Mybatis报错日志
BindingException
Type interface com.zhnx.demo1.mapper.UserMapper is not known to the MapperRegistry.
org.apache.ibatis.binding.BindingException: Type interface com.zhnx.demo1.mapper.UserMapper is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:845)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:288)
at com.zhnx.demo1.mapper.test.textInsert(test.java:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
异常原因可能是我的UserMapper没有被识别到。
解决办法:查看核心配置文件和映射文件是否出错
修改以后再次运行
<!--引入映射文件-->
<mappers>
<mapper resource="mappers/UserMapper.xml"/>
</mappers>
补充:关于中的和
- * 这个标签可以直接跟映射文件,不需要Mapper接口和映射文件的目录一样 * 但是每个映射文件都需要用
- * 这个标签可以一次性引入整个包的映射文件 * 但是mapper接口的目录和映射文件的目录必须相同
上一个问题解决,但是又报错了
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource.
Cause: java.lang.ClassNotFoundException: Cannot find class: ${jdbc.driver}
### The error may exist in mappers/UserMapper.xml
### The error may involve com.zhnx.demo1.mapper.UserMapper.insertUser
### The error occurred while executing an update
### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: ${jdbc.driver}
一眼看到找不到${jdbc.driver}
核心配置文件中的引用idbc.properties文件没有出错,配置环境也没有出错
<properties resource="jdbc.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 设置数据库连接的驱动 ${jdbc.driver}-->
<property name="driver" value="${jdbc.driver}"/>
<!-- 设置数据库的连接地址 ${jdbc.url}-->
<property name="url" value="${jdbc.url}"/>
<!-- 设置连接数据库的用户名 ${jdbc.username}-->
<property name="username" value="${jdbc.username}"/>
<!-- 设置连接数据库的密码 ${jdbc.password}-->
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
我的jdbc.properties是这样的
一开始我也觉得没有出错,校对了好多遍突然想起来是不是要在driver前面加上一个"jdbc."
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.username=****** //给自己的用户名和密码打码一下
jdbc.password=******
修改以后运行成功