小段大帅
2017-04-26 03:55
采纳率: 17.4%
浏览 3.5k
已采纳

mybatis配置问题(应该是路径问题)

工程目录结构如图图片说明

mapper映射如下

 <?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">
<mapper namespace="UsersMapper">
    <select id="selectUser" resultType="bean.Users">
        select * from users where userID = #{id}
    </select>
</mapper>

配置文件如下

 <?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">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="user" value="root"></property>
                <property name="password" value="1995"></property>
                <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
                <property name="jdbcUrl"
                    value="jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8&amp;serverTimezone=UTC"></property>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="UsersMapper.xml" />
    </mappers>
</configuration>

junit测试类

 @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream) ;

        SqlSession session = sqlSessionFactory.openSession();
        try {
          Users user = session.selectOne("UsersMapper.selectUser", 1);
          System.out.println(user);
        } finally {
          session.close();
        }
    }

错误信息如下

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: org.apache.ibatis.datasource.DataSourceException: Unknown DataSource property: driverClass

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at main.MybatisTest.test(MybatisTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.datasource.DataSourceException: Unknown DataSource property: driverClass
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 25 more
Caused by: org.apache.ibatis.datasource.DataSourceException: Unknown DataSource property: driverClass
at org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory.setProperties(UnpooledDataSourceFactory.java:55)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.dataSourceElement(XMLConfigBuilder.java:321)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.environmentsElement(XMLConfigBuilder.java:275)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:115)
... 27 more

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 夕阳雨晴 2017-04-26 04:01
    已采纳

    这个应该是driverClassName

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • engchina 2017-04-26 05:39

    用下面这几个属性设置试下。




    评论
    解决 无用
    打赏 举报
  • engchina 2017-04-26 05:45

    csdn编辑器把写的内容全变成空白了。重发。

    <property name="url"  value="略"></property>
                    <property name="driver" value="com.mysql.jdbc.Driver"></property>
                    <property name="username" value="略"></property>
                    <property name="password" value="略"></property>
    
    评论
    解决 无用
    打赏 举报
  • engchina 2017-04-26 05:46

    你的程序还有其他错误。

    mapper可能需要修改。

    下面代码仅供参考。(字段名字等需要修改为你的数据库和程序信息)

    <mapper namespace="UsersMapper">
        <resultMap id="userResultMap" type="bean.Users">
            <id column="USER_ID" property="userId" jdbcType="VARCHAR" />
            <result column="USER_NAME" property="userName" jdbcType="VARCHAR" />
        </resultMap>
        <!-- <select id="selectUser" resultType="bean.Users"> -->
        <select id="selectUser" resultMap="userResultMap">
            select * from users where user_id = #{id}
        </select>
    </mapper>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题