ibatis查询结果为null

theoffspring 2005-05-10 04:44:24
我照着ibatis的导读的例子做的,但采用struts+ibatis架构,表
person:
per_id:编号
per_first_name:第一个名
per_last_name:最后一个名
per_birth_date:出生日期,datetime
per_weight_kg:体重
per_height_m:身高

sqlmap配置文件sql_map_config.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">
<sqlMapConfig>
<properties resource="myconfig/database.properties" />
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="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>
<sqlMap resource="myconfig/person.xml" />
</sqlMapConfig>

sqlmap映射文件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">
<resultMap class="example.domain.Person" id="map_person">
<result property="id" column="per_id"></result>
<result property="firstName" column="per_first_name"></result>
<result property="lastName" column="per_last_name"></result>
<result property="birthDate" column="per_birth_date"></result>
<result property="weight" column="per_column_kg"></result>
<result property="height" column="per_height_m"></result>

</resultMap>


<select id="getPerson" resultMap="map_person"
parameterClass="int">
select per_id as id,per_first_name as firstName,
per_last_naem as lastName,per_birth_date as birthDate,
per_weight_kg as weight,per_height_m as height
from person where per_id=#value#
</select>
<select id="getAllPerson" resultMap="map_person">
<![CDATA[
select * from person
]]>
</select>

<insert id="insertPerson" parameterClass="example.domain.Person">
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#,#weight#,
#height#)
</insert>

<update id="updatePerson" parameterClass="example.domain.Person">
update person set per_first_name=#firstName#,
per_last_name=#lastName#,per_birth_date=#birthDate#,
per_weight_kg=#weight#,per_height_m=#height#
where per_id=#id#
</update>

<delete id="deletePerson" parameterClass="example.domain.Person">
delete from person where per_id=#id#
</delete>
</sqlMap>

我在一个action里调用queryForList,然后赋值给一个List时,竟然报错。
dao的源代码:
package daos;
import java.sql.SQLException;
import example.domain.*;
import com.ibatis.sqlmap.client.*;
import java.util.*;


/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class PersonDAO {
private SqlMapClient client;
public PersonDAO(SqlMapClient client)
{
this.client=client;
}
public void insert(Person person) {
try {
client.startTransaction();
client.insert("Person.insertPerson",person);
client.commitTransaction();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

public void update(Person person) {
try {
client.startTransaction();
client.update("Person.updatePerson",person);
client.commitTransaction();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}
public void delete(int id)
{
//client.delete("Person.delete",person);
}
public Person findById(int id)
{
Person rst=null;
try {
rst=(Person)(client.queryForObject("Person.getPerson",new Integer(id)));
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return rst;
}
public List findAll()
{
List list=null;
try {
list=client.queryForList("Person.getAllPerson",null);
int i;
i=0;
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
finally
{
return list;
}
}
}
...全文
959 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
MARS.nEIL 2005-05-17
  • 打赏
  • 举报
回复
帮顶..学习.
theoffspring 2005-05-17
  • 打赏
  • 举报
回复
to 楼上:
  命名空间我去掉也不好用
zhuzy 2005-05-17
  • 打赏
  • 举报
回复
Chenshy and i read your code and find
that there are at least 2 bugs in your
code:

(1)You use namespace in your code but you
make definition to use no namespace in your
"sqlMapConfig".
xml:
useStatementNamespaces="false" <-----this is false
source code:
client.delete("Person.delete",person); <--using namespace of "Person"

(2) the direct reason is that you use the instance varibal
named "client" (line 4 of the following code) to transfer
the reference of SqlMapClient to the other objects as input
(see the line 14 in the following code).

BUT, JUST SEE LINE 9. You build the SqlMapClient object there but
you defined it as a local varible using the same name as "client".
That means, although you build the SqlMapClient successfully
and the "client" varible of line 9 is not null, it is only
a LOCAL varible so it is OUT OF RANGE after line 12.
so just correct it as the following:
before: SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(reader);
after: client = SqlMapClientBuilder.buildSqlMapClient(reader);
then the varible of "client" is the instance varibal (property)
of PersonBO as is defined at line 4. So it will go well.

Your code:
----------------------------------------------------
1
2
3 public class PersonBO {
4 private SqlMapClient client; <--**** instance varibal definition
5
6 public PersonBO() {
7 try {
8 Reader reader = .....
9 SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(reader); <--**local varibal definition
10 int i;
11 i=1;
12 }
...

13 public void update(Person person) {
14 PersonDAO personDAO = new PersonDAO(client); <----*** use "client" to transfer SqlMapClient reference
15 personDAO.update(person);
16 }

----------------------------------------------------


Regards.
ipqitfk 2005-05-16
  • 打赏
  • 举报
回复
没有执行,就不是错了吗?既然挂出帖子来,就不要怕大家批评。
言归正传,
你贴出的
<select id="getAllPerson" resultMap="map_person">
<![CDATA[
select * from person
]]>
</select>

<!是代码里就这样,还是??
theoffspring 2005-05-16
  • 打赏
  • 举报
回复
up
chenshy 2005-05-16
  • 打赏
  • 举报
回复
Maybe the probrom is in the Java class.

public class PersonBO {
private SqlMapClient client;

public PersonBO() {
try {
Reader reader = Resources.getResourceAsReader("myconfig/sql_map_config.xml");
//SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(reader);
//local varity
client = SqlMapClientBuilder.buildSqlMapClient(reader);
int i;
i=1;
}
...


public class PersonDAO {
...
public List findAll()
{
List list=null;
try {
//list=client.queryForList("Person.getAllPerson",null);
//no namespace
list=client.queryForList("getAllPerson",null);
int i;
i=0;
}
....


}

.....

theoffspring 2005-05-16
  • 打赏
  • 举报
回复
<select id="getAllPerson" resultMap="map_person">
<![CDATA[
select * from person
]]>
</select>
theoffspring 2005-05-16
  • 打赏
  • 举报
回复
那是论坛的问题

我欢迎有道理的指正,不欢迎自以为是的指责
theoffspring 2005-05-13
  • 打赏
  • 举报
回复
但错误还是那么报的
theoffspring 2005-05-13
  • 打赏
  • 举报
回复
to zhuzy:
1.表里的数据没有为null的
 2. <result property="weight" column="per_column_kg"></result> <--------!!!!!!!!!这个地方是笔误,已经改了
 3.又发现一处错误,没有建立相应的用户,在sqlserver中
zhuzy 2005-05-13
  • 打赏
  • 举报
回复
Just again.

a suggestion again:

the original SqlMap definition:
----------------------------------------------------------
<resultMap class="example.domain.Person" id="map_person">
<result property="id" column="per_id"></result>
<result property="firstName" column="per_first_name"></result>
<result property="lastName" column="per_last_name"></result>
<result property="birthDate" column="per_birth_date"></result>
<result property="weight" column="per_column_kg"></result> <--------!!!!!!!!!
<result property="height" column="per_height_m"></result>
</resultMap>
----------------------------------------------------------

Just notice
<result property="weight" column="per_column_kg"></result> <--------!!!!!!!!!

the correct column name is "per_weight_kg" and NOT ="per_column_kg".
maybe that the REAL reason. so just correct it and try.

zhuzy 2005-05-13
  • 打赏
  • 举报
回复

Sorry to reply in english because i type PINGYIN so late :-(

Just a suggestion:

i think the reason is that JDBC doesn't know the JDBC type
when the column is NULL so it cann't convert it into the
proper java type.

for the column that may be NULL, you can define a parameterMap
in which you can define its Java type & JDBC type , and even
you can define the default value when it is really NULL.
(it is awsome that so many definition should be written )

i've not used iBATIS before but we are going to use it for
our current project, as use the following architecture:
Struts + SpringFramework + iBATIS
and that seems feeling well. (REALLY????)
maybe you can mail me to talk about some of that.

To XiaoXu:
i'm sorry that i have no time to test what i said above, so
would you please do me a favour to write some code to test
it? and please tell us the result so we need to test it ourselves.
Thanks.
theoffspring 2005-05-12
  • 打赏
  • 举报
回复
ipqitfk(灵魂出走)说的那个地方我根本没有调用,我调用的是getAllPerson,和这个没关系
theoffspring 2005-05-12
  • 打赏
  • 举报
回复
怎么没有理解,不就是属性和列的映射吗?你不能直接说怎么错的吗
MARS.nEIL 2005-05-12
  • 打赏
  • 举报
回复
不熟.帮顶..
ipqitfk 2005-05-12
  • 打赏
  • 举报
回复
一眼就看到错误的地方了
select per_id as id,per_first_name as firstName

第四个id在你前面的
<resultMap class="example.domain.Person" id="map_person">
<result property="id" column="per_id"></result>
<result property="firstName" column="per_first_name"></result>
<result property="lastName" column="per_last_name"></result>
<result property="birthDate" column="per_birth_date"></result>
<result property="weight" column="per_column_kg"></result>
<result property="height" column="per_height_m"></result>

</resultMap>
根本没有定义,你还没有理解<result property="id" column="per_id"></result>这句话的含义,建议再看看文档
theoffspring 2005-05-12
  • 打赏
  • 举报
回复
client我跟踪了,并不是null。所以我才奇怪啊
sgdb 2005-05-11
  • 打赏
  • 举报
回复
判断一下client 吧,估计那个是null
theoffspring 2005-05-10
  • 打赏
  • 举报
回复
bo的代码:
/*
* 创建日期 2005-5-9
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package bos;

import daos.*;
import example.domain.*;
import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.*;
import java.io.*;
import java.util.List;

/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class PersonBO {
private SqlMapClient client;

public PersonBO() {
try {
Reader reader = Resources.getResourceAsReader("myconfig/sql_map_config.xml");
SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(reader);
int i;
i=1;
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

public void create(Person person) {
PersonDAO personDAO = new PersonDAO(client);
personDAO.insert(person);
}

public void update(Person person) {
PersonDAO personDAO = new PersonDAO(client);
personDAO.update(person);
}

public void delete(int id) {
PersonDAO personDAO = new PersonDAO(client);
personDAO.delete(id);
}

public List findAll() {
PersonDAO personDAO = new PersonDAO(client);
return personDAO.findAll();
}

public Person findOne(int id)
{
PersonDAO personDAO=new PersonDAO(client);
return personDAO.findById(id);
}
}

action:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.4/xslt/JavaClass.xsl

package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import bos.*;
import java.util.*;

/**
* MyEclipse Struts
* Creation date: 05-09-2005
*
* XDoclet definition:
* @struts:action validate="true"
*/
public class Index extends Action {

// --------------------------------------------------------- Instance Variables

// --------------------------------------------------------- Methods

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
PersonBO personBO=new PersonBO();
List list=null;
list=personBO.findAll();//运行到这行就报错,查询所有注册的用户
request.setAttribute("person",list);
return mapping.findForward("success");
}
}

报错信息:
java.lang.NullPointerException

at daos.PersonDAO.findAll(PersonDAO.java:76)

at bos.PersonBO.findAll(PersonBO.java:53)

at actions.Index.execute(Index.java:44)

at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)

at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)

at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)

at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)

at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)

at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)

at java.lang.Thread.run(Thread.java:534)

用开源的框架调试真是麻烦,大家估计一下吧,什么原因?

67,549

社区成员

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

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