浏览 4649 次
锁定老帖子 主题:ibatis问题,初始化出错
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-07-02
写了个程序: 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" 目录结构: 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-07-04
<?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> ------------------------------ 这个地方的value错了吧 |
|
返回顶楼 | |
发表时间:2007-07-06
<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> 有没有写 <resultMap>啊? |
|
返回顶楼 | |