ibatis 配置问题?

gongyifeng_cs 2007-07-02 01:59:02
初学ibatis
写了个程序:
1.Person.java
package com.gyf.model;

import java.util.Date;

/**
* @author dell
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class Person {

private int id;
private String firstName;
private String lastName;
private Date birthDate;
private double weightInKilograms;
private double heightInMeters;

/**
* @return
*/
public Date getBirthDate() {
return birthDate;
}

/**
* @return
*/
public String getFirstName() {
return firstName;
}

/**
* @return
*/
public double getHeightInMeters() {
return heightInMeters;
}

/**
* @return
*/
public int getId() {
return id;
}

/**
* @return
*/
public String getLastName() {
return lastName;
}

/**
* @return
*/
public double getWeightInKilograms() {
return weightInKilograms;
}

/**
* @param date
*/
public void setBirthDate(Date date) {
birthDate = date;
}

/**
* @param string
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param d
*/
public void setHeightInMeters(double d) {
heightInMeters = d;
}

/**
* @param i
*/
public void setId(int i) {
id = i;
}

/**
* @param string
*/
public void setLastName(String string) {
lastName = string;
}

/**
* @param d
*/
public void setWeightInKilograms(double d) {
weightInKilograms = d;
}

}
2.Person.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="com.gyf.model.Person">
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>
</sqlMap>

3.MyAppSqlConfig.java
package com.gyf.ibatis;

import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

/**
* @author gyf
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class MyAppSqlConfig {
private static final SqlMapClient sqlMap;
static {
try {
String resource = "com/gyf/sqlmap/maps/SqlMapConfigExample.xml";
//String resource = "D:\\work\\IBM workspace\\struts_ibatis_spring\\JavaSource\\com\\gyf\\sqlmap\\maps\\SqlMapConfigExample.xml";
Reader reader = Resources.getResourceAsReader (resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
// If you get an error at this point, it matters little what it was. It is going to be
// unrecoverable and we will want the app to blow up good so we are aware of the
// problem. You should always log such errors and re-throw them in such a way that
// you can be made immediately aware of the problem.
e.printStackTrace();
throw new RuntimeException ("Error initializing MyAppSqlConfig class. Cause:"+e);
}
}
public static SqlMapClient getSqlMapInstance () {
return sqlMap;
}
}

4.SqlMapConfigExample.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>
<!-- The properties (name=value) in the file specified here can be used placeholders in this
config file (e.g. “${driver}”. The file is relative to the classpath and is completely optional. -->
<properties resource="com/gyf/sqlmap/maps/SqlMapConfigExample.properties" />

<!-- These settings control SqlMap configuration details, primarily to do with transaction
management. They are all optional (see the Developer Guide for more). -->
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<!-- Type aliases allow you to use a shorter name for long fully qualified class names. -->
<typeAlias alias="order" type="testdomain.Order"/>

<!-- Configure a datasource to use with this SQL Map using SimpleDataSource.
Notice the use of the properties from the above resource -->
<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>
<!-- Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths
are relative to the classpath. For now, we only have one… -->
<sqlMap resource="com/gyf/sqlmap/maps/Person.xml" />
</sqlMapConfig>

5.SqlMapConfigExample.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:KAS_154.1.1.164
username=system
password=manager

测试程序:
test.java
import java.sql.SQLException;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.gyf.ibatis.*;
import com.gyf.model.Person;

/*
* 创建日期 2007-7-2
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/

/**
* @author dell
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class test {

public static void main(String[] args) {
SqlMapClient sqlMap = MyAppSqlConfig.getSqlMapInstance(); // as coded above
Integer personPk = new Integer(11);
try {
Person person = (Person) sqlMap.queryForObject ("getPerson", personPk);
System.out.println(person.getFirstName());
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}

报错:
java.lang.NoSuchMethodError: java.lang.Exception: method <init>(Ljava/lang/String;Ljava/lang/Throwable;)V not found
at com.ibatis.common.xml.NodeletException.<init>(NodeletException.java:20)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:53)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:86)
at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:63)
at com.gyf.ibatis.MyAppSqlConfig.<clinit>(MyAppSqlConfig.java:28)
at test.main(test.java:23)
Exception in thread "main"

谢谢
...全文
227 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧