---
title: "ibatis初学者遇到的问题及解决办法"
author: "yangkai"
date: "2009-12-28T14:31:52Z"
tags: []
categories: ["企业架构"]
slug: "ibatis"
draft: false
lastmod: "2009-12-28T14:34:14Z"
id: 558305
url: "https://yangkai.iteye.com/blog/558305"
---

昨日遇到两个问题

1)com.ibatis.sqlmap.client.SqlMapException: There is no statement named getPerson in this SqlMap.

2)at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.endTransaction

第一个问题 useStatementNamespaces="true" 是在调用sqlmap时添加命名前缀(namespace which is defined in sqlMap xml),false为直接引用sqlmap(id)

第二个问题 是没有数据源造成

示例

Person.java

```java

import java.util.Date;
public class Person {
    private int id;
    private String firstName;
    private String lastName;
    private Date birthDate;
    private double weightInKilograms;
    private double heightInMeters;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public double getWeightInKilograms() {
        return weightInKilograms;
    }

    public void setWeightInKilograms(double weightInKilograms) {
        this.weightInKilograms = weightInKilograms;
    }

    public double getHeightInMeters() {
        return heightInMeters;
    }

    public void setHeightInMeters(double heightInMeters) {
        this.heightInMeters = heightInMeters;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

}

```

SqlMapConfigExample.xml

```xml

<?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">
  <!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>


  <properties resource="examples/domain/SqlMapConfigExample.properties" />
  <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="true"
    maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />

  <!--
    <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${driver}" />
    <property name="JDBC.ConnectionURL" value="${url}" /> <property name="JDBC.Username" value="${username}" />
    <property name="JDBC.Password" value="${password}" /> </dataSource> </transactionManager>
  -->

  <transactionManager type="JDBC">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="http://www.blog.com.cn/oracle.jdbc.driver.OracleDriver" />
      <property name="JDBC.ConnectionURL" value="http://www.blog.com.cn/jdbc:oracle:thin:@120.154.21.97:1521:orcl" />
      <property name="JDBC.Username" value="steel" />
      <property name="JDBC.Password" value="steel" />
      <property name="Pool.MaximumActiveConnections" value="10" />
      <property name="Pool.MaximumIdleConnections" value="5" />
      <property name="Pool.MaximumCheckoutTime" value="120000" />
      <property name="Pool.TimeToWait" value="500" />
      <property name="Pool.PingQuery" value="select 1 from dual" />
      <property name="Pool.PingEnabled" value="false" />
      <property name="Pool.PingConnectionsOlderThan" value="1" />
      <property name="Pool.PingConnectionsNotUsedFor" value="1" />
    </dataSource>
  </transactionManager>


  <sqlMap resource="examples/domain/Person.xml" />
</sqlMapConfig>

```

Person.xml

```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">
<sqlMap namespace="Person">
  <select id="getPerson" resultClass="examples.domain.Person">
   <![cdata[ select per_id as id,
    per_first_name as firstname,
    per_last_name as lastname,
    per_birth_date as birthdate,
    per_weight_kg as weightinkilograms,
    per_height_m as heightinmeters
    from person
    where per_id
    = #value#]]>
  </select>
  <insert id="insertPerson" parameterClass="examples.domain.Person">
    <![cdata[ insert into
    person (per_id, per_first_name, per_last_name,
    per_birth_date, per_weight_kg, per_height_m)
    values (#id#, #firstname#, #lastname#,
    #birthdate#, #weightinkilograms#,
    #heightinmeters#)]]>
  </insert>
  <update id="updatePerson" parameterClass="examples.domain.Person">
   <![cdata[update person set per_first_name = #firstName# 
   ,PER_BIRTH_DATE=#birthDate#,PER_WEIGHT_KG=#weightInKilograms#
   WHERE PER_ID = #id#]]>
  </update>
  <delete id="deletePerson" parameterClass="examples.domain.Person">
    DELETE PERSON
    WHERE PER_ID = #id#
</delete>
</sqlMap>

```

Test.java


```java

/* 
 *   Date       Developer   Description
 *   
 *
 *   Classname:  examples.domain.Test
 * 
 *   Date:   2009-5-12
 * 
 *   The source code contained in this listing is proprietary to
 *   HLJ POST YITONG INFO-NET CO.,LTD.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2009 HLJ POST YITONG INFO-NET CO.,LTD.
 */
package examples.domain;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

/**
 * @author Administrator
 */
public class Test  {

    public static void main(String[] args) {
        String resource = "examples/domain/SqlMapConfigExample.xml";
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }

        SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        try {
             sqlMap.startTransaction();            
             Person user = new Person(); 
             user.setId(5);
             user.setFirstName("刘秀");
             user.setBirthDate(new Date());
             user.setWeightInKilograms(66.5);
             
             sqlMap.update("updatePerson", user);
             System.out.println("good!---------");
             Integer personPk = new Integer(5);
             List list = sqlMap.queryForList ("getPerson", personPk);
             System.out.println(list.size());
             for (int i=0;i<list.size();i++){
                 Person author=(Person)list.get(i);
                System.out.println(author.getFirstName()+"\t"+author.getId());
             }
            sqlMap.commitTransaction();
        } catch (SQLException ef) {
            ef.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException ef) {

            }
        }
    }

   
}

```