今天刚刚接触Ibatis,写了一个超级简单的实例.却出错了,很悲哀。。。具体看我写的代码
配置文件:
<sqlMap>
<typeAlias type="com.air.Products" alias="prod" />
<!-- 获得一个用户 -->
<select id="getOnePro" parameterClass="string" resultClass="prod">
select * from product where pro_id=#proID#
</select>
<!--获得全部用户 -->
<select id="getAllPro" resultClass="prod" >
select * from product order by pro_number
</select>
</sqlMap>
Dao层:
@SuppressWarnings("unchecked")
public List<Products> getAllPro(){
List<Products> list=new ArrayList<Products>();
try {
list= (List<Products>)this.queryForList("getAllPro");
//return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
main函数:
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String resource="./SqlMapConfig.xml";
Reader reader = null ;
reader = Resources.getResourceAsReader(resource);
SqlMapClientImpl smc=(SqlMapClientImpl)SqlMapClientBuilder.buildSqlMapClient(reader);
ProductsService ps=new ProductsService(smc.delegate);
List<Products> list=ps.getAllPro();
System.out.println(list.size());
Products pr=new Products();
for (int i=0;i<20;i++){
pr=list.get(i);
System.out.println(pr.getProId()+" "+
pr.getProName()+" "+pr.getProPrice());
}
}
}
显示:
212992
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0
0 null 0.0