您的位置:首页 > 编程语言 > Java开发

spring的jdbcTemplate查询不到数据

2010-02-05 00:11 495 查看
搞了一个晚上,未果。

测试代码如下:

主程序代码

package org.xiziyin.shop.dal.dao.jdbcimpl;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.xiziyin.shop.dal.dao.GoodsDAO;
import org.xiziyin.shop.dal.dao.exception.DAOException;
import org.xiziyin.shop.dal.dataobject.Goods;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* Class JdbcTemplateGoodsDAO ...
*
* @author <a href="mailto:czy88840616@163.com">czy</a>
*         Created on 2010-2-4 22:14:41
*/
public class JdbcTemplateGoodsDAO implements GoodsDAO {

private JdbcTemplate jdbcTemplate;

/**
* Method getJdbcTemplate returns the jdbcTemplate of this JdbcTemplateGoodsDAO object.
*
* @return the jdbcTemplate (type JdbcTemplate) of this JdbcTemplateGoodsDAO object.
*/
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}

/**
* Method setJdbcTemplate sets the jdbcTemplate of this JdbcTemplateGoodsDAO object.
*
* @param jdbcTemplate the jdbcTemplate of this JdbcTemplateGoodsDAO object.
*/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

/**
* Method getGoodsById ...
*
* @param id of type Long
*
* @return Goods
*
* @throws DAOException when
*/
@Override
public Goods getGoodsById(Long id) throws DAOException {
final Goods goods = new Goods();
this.jdbcTemplate.query("select * from goods where id = ?", new Object[]{id}, new RowCallbackHandler(){
/** @see org.springframework.jdbc.core.RowCallbackHandler#processRow(ResultSet) */
@Override
public void processRow(ResultSet rs) throws SQLException {
if(rs.next()) {
goods.setId(rs.getLong(1));
goods.setName(rs.getString(2));
goods.setQuantity(rs.getInt(3));
goods.setPrice(rs.getDouble(4));
goods.setDescribe(rs.getString(5));
}
}
});
//        Goods goods = new Goods();
//        goods = (Goods) this.jdbcTemplate.queryForObject("select * from goods where id=?", new Object[]{id}, new RowMapper() {
//
//            @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
//                Goods g = new Goods();
//                if (rs.next()) {
//                    g.setId(rs.getLong(1));
//                    g.setName(rs.getString(2));
//                    g.setQuantity(rs.getInt(3));
//                    g.setPrice(rs.getDouble(4));
//                    g.setDescribe(rs.getString(5));
//                }
//                return g;
//            }
//        });
return goods;
}

/**
* Method getGoodsList returns the goodsList of this JdbcTemplateGoodsDAO object.
*
* @return the goodsList (type List<Goods>) of this JdbcTemplateGoodsDAO object.
*
* @throws DAOException when
*/
@Override
public List<Goods> getGoodsList() throws DAOException {
final List<Goods> list = new ArrayList<Goods>();
this.jdbcTemplate.query("select * from goods", new Object[]{}, new RowCallbackHandler() {

/** @see org.springframework.jdbc.core.RowCallbackHandler#processRow(ResultSet) */
@Override
public void processRow(ResultSet rs) throws SQLException {
while (rs.next()) {
Goods goods = new Goods();
goods.setId(rs.getLong(1));
goods.setName(rs.getString(2));
goods.setQuantity(rs.getInt(3));
goods.setPrice(rs.getDouble(4));
goods.setDescribe(rs.getString(5));
list.add(goods);
}
}
});
return list;
}

}


很奇怪的是使用getAllGoods的时候能够将结果正确的返回,而是用id来查询的时候却根本查不到值。难道是bug?使用sql在mysql里面查询是正常的,bean配置都正确。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐