• spring的jdbcTemplate查询不到数据


    搞了一个晚上,未果。

    测试代码如下:

    测试代码
    package org.xiziyin.shop.dal.dao.jdbcimpl;

    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.testng.Assert;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    import org.xiziyin.shop.BaseTest;
    import org.xiziyin.shop.dal.dao.GoodsDAO;
    import org.xiziyin.shop.dal.dao.exception.DAOException;
    import org.xiziyin.shop.dal.dataobject.Goods;

    import java.util.List;


    /**
    * Class JdbcTemplateGoodsDAOTest ...
    *
    *
    @author <a href="mailto:czy88840616@163.com">czy</a>
    * Created on 2010-2-4 22:20:36
    */
    public class JdbcTemplateGoodsDAOTest extends BaseTest implements InitializingBean{
    private GoodsDAO goodsDAO;

    /**
    * Method getGoodsDAO returns the goodsDAO of this JdbcTemplateGoodsDAOTest object.
    *
    *
    *
    *
    @return the goodsDAO (type GoodsDAO) of this JdbcTemplateGoodsDAOTest object.
    */
    public GoodsDAO getGoodsDAO() {
    return goodsDAO;
    }

    /**
    * Method setGoodsDAO sets the goodsDAO of this JdbcTemplateGoodsDAOTest object.
    *
    *
    *
    *
    @param goodsDAO the goodsDAO of this JdbcTemplateGoodsDAOTest object.
    *
    */
    public void setGoodsDAO(GoodsDAO goodsDAO) {
    this.goodsDAO = goodsDAO;
    }

    /**
    * Method testGetGoodsList ...
    *
    @throws DAOException when
    */
    @Test
    public void testGetGoodsList() throws DAOException {

    List
    <Goods> list = this.goodsDAO.getGoodsList();
    System.out.println(list.size());
    System.out.println(list.get(
    0).getName());
    }

    /**
    * Method testGetGoodsById ...
    *
    @throws Exception when
    */
    @Test
    public void testGetGoodsById() throws Exception {
    Goods goods
    = this.goodsDAO.getGoodsById(1L);
    Assert.assertNotNull(goods);
    System.out.println(goods.getName());
    }

    /**
    * Method afterPropertiesSet ...
    *
    @throws Exception when
    */
    @BeforeTest
    public void afterPropertiesSet() throws Exception {
    ApplicationContext context
    = new ClassPathXmlApplicationContext("/applicationContext.xml");
    this.goodsDAO = (GoodsDAO) context.getBean("goodsDAO");
    }

    }
    主程序代码:
    主程序代码
    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配置都正确。
  • 相关阅读:
    获取股票行情API 接口
    使用百度地图来展示自定义的GPS点,用pyechart 框架实例
    C 语言基础笔记
    GPS 测试汇总和python GPS 导航地图实现
    用python 来炒股二 BeautifulSoup爬虫信息新闻文章
    Python tkinter 笔记 [pack,place,grid 布局管理]
    RSS 订阅精选 2020
    用python来炒股<三> 炒股交易系统(法则)
    使用python 来实现炒股
    鼠须管输入法的配置介绍
  • 原文地址:https://www.cnblogs.com/xiziyin/p/1664051.html
Copyright © 2020-2023  润新知