搞了一个晚上,未果。
测试代码如下:
测试代码
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配置都正确。