mybatis 遇到个问题,怎么搞也搞不定

错误代码是:org.apache.ibatis.exceptions.PersistenceException:

Error building SqlSession.

The error may exist in SQL Mapper Configuration

Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource db.properties

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66)
at cn.itcast.mybatis.first.MybatisFirst.findUserByIdTest(MybatisFirst.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource db.properties
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:109)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:92)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
... 25 more
Caused by: java.io.IOException: Could not find resource db.properties
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:110)
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:97)
at org.apache.ibatis.io.Resources.getResourceAsProperties(Resources.java:123)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.propertiesElement(XMLConfigBuilder.java:176)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:98)
... 27 more

Mapper里面配置问题,把代码贴一下呢

图片说明

sqlmapconfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<!-- 加载属性文件 -->
<properties resource="db.properties">
    <!--properties中还可以配置一些属性名和属性值  -->
    <!-- <property name="jdbc.driver" value=""/> -->
</properties>
<!-- 全局配置参数,需要时再设置 -->
<!-- <settings>

</settings> -->

<!-- 别名定义 -->
<typeAliases>

    <!-- 针对单个别名定义
    type:类型的路径
    alias:别名
     -->
    <!-- <typeAlias type="cn.itcast.mybatis.po.User" alias="user"/> -->
    <!-- 批量别名定义 
    指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名(首字母大写或小写都可以)
    -->
    <package name="cn.itcast.mybatis.po"/>

</typeAliases>

<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
    <environment id="development">
    <!-- 使用jdbc事务管理,事务控制由mybatis-->
        <transactionManager type="JDBC" />
    <!-- 数据库连接池,由mybatis管理-->
        <dataSource type="POOLED">
            <property name="driver" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </dataSource>
    </environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml"></mapper>
</mappers>
</configuration>

User.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


select * from User where id=#{id}

找不到这个文件db.properties ,如果项目里面存在这个文件还有这种错误,那就可能是你发布后的项目不完整

    jdbc数据库配置文件中参数配置,以下这几项参数看看配置对了没
    jdbc.driverClassName=oracle.jdbc.OracleDriver
    jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl  #注意@后面数据库服务器Ip:端口:orcl
    jdbc.username=root
    jdbc.password=12345
下面这个,jdbc数据库配置文件中参数配置,以下这几项参数看看配置对了没
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl  #注意@后面数据库服务器Ip:端口:orcl
jdbc.username=root
jdbc.password=12345

明显的呀,你resource里面配置了db.properties 但是你项目里面并没有呀,他当然读取不到,会报错了。。新建一个叫db.properties的文件,把楼上的代码占进去,变量名字不要变,值改成你自己数据库的就行了。

没有db.properties

您好:问题应该出在这里

<!--properties中还可以配置一些属性名和属性值 -->
<!-- -->

您想引入配置文件。但是引入失败。尝试解决:
1、查看当前db.properties是否存在
2、如果存在,查看db.properties存放发位置
3、确保db.properties内的配置是正确的