初学ibatis遇到的一些异常

java文件:

[code="java"]package com.ibatis.sample;

import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.sqlmap.engine.builder.xml.XMLSqlSource;

public class Test {

public void insertTest() {
    SqlMapClient smc = null;
    try {
        String str = "./com/ibatis/sample/sqlMapConfig.xml";
        Reader reader = Resources.getResourceAsReader(str);
        smc = SqlMapClientBuilder.buildSqlMapClient(reader);
        //smc.startTransaction();
        Dept d = new Dept();
        d.setDname("aaaa");
        d.setLoc("bbbb");
        smc.insert("insertDept");
        System.out.print(d.getDname());
        //smc.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try{
        //smc.endTransaction();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public static void main(String[] args){
    Test t=new Test();
    t.insertTest();
}

}
[/code]

数据库配置:
[code="java"]<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">

lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />

    <dataSource type="SIMPLE">
        <property name="JDBC.Driver"
            value="oracle.jdbc.driver.OracleDriver" />
        <property name="JDBC.ConnectionURL"
            value="jdbc:oracle:thin:@192.168.0.35:1521:orcl" />
        <property name="JDBC.Username"  value="scott" />
        <property name="JDBC.Password"  value="tiger" />
        <property name="Pool.MaxmumActiveConnections" value="10"/>
        <property name="Pool.MaxmumIdleConnections" value="5"/>
        <property name="Pool.MaxmumCheckoutTime" value="120000"/>
        <property name="Pool.TimeToWait" value="500" />
        <property name="Pool.PingQuery"" value="select 1 from ACCOUNT" />
        <property name="Pool.PingEnabled" value="false" />
        <property name="Pool.PingConnectionsOlderThan" value= "1" />
        <property name="Pool.PingConnectionsNotUsedFor" value= "1" />
    </dataSource>
</transactionManager>
<sqlMap resource="com/ibatis/sample/sqlMapConfig.xml" />

[/code]

sqlMap 文件:
[code="xml"]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">



<![CDATA[select dname, loc from Dept where deptno = #deptno# ]]>


<![CDATA[update Dept set dname=#dname#, loc=#loc# where deptno = #deptno# ]]>


insert into Dept ( dname, loc) values ( #dname#, #loc# )


delete from Dept where deptno = #deptno#


[/code]

最后老提示:
com.ibatis.sqlmap.client.SqlMapException: There is no statement named insertDept in this SqlMap.
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.getMappedStatement(SqlMapExecutorDelegate.java:231)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:367)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:86)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:62)
at com.ibatis.sample.Test.insertTest(Test.java:22)
at com.ibatis.sample.Test.main(Test.java:38)
[b]问题补充:[/b]
写上“d”,还是一样的异常

There is no statement named insertDept in this SqlMap.

3个回答

代码中:
[code="java"]
String str = "./com/ibatis/sample/sqlMapConfig.xml";

[/code]

数据库配置文件中:
[code="java"]


[/code]
这两个文件怎么是一样的, 你的代码中的应该是你的数据库配置文件的路径吧!

[code="java"]
//smc.startTransaction();

Dept d = new Dept();

d.setDname("aaaa");

d.setLoc("bbbb");

smc.insert("insertDept", d); //掉了参数

System.out.print(d.getDname());

//smc.commitTransaction();

[/code]

掉了参数d

请确认你的
[code="java"]
String str = "./com/ibatis/sample/sqlMapConfig.xml"; [/code]
路径是正确的!试着改为
[code="java"]
String str = "com/ibatis/sample/sqlMapConfig.xml";
[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐