三表联查的sql为“SELECT s.shop_id, s.shop_name, p.pro_id, p.inventory, u.user_type FROM tb_user u INNER JOIN tb_shop s ON u.user_id=s.user_id INNER JOIN tbt_product p ON u.user_type=p.user_type;”
第四张表为tbt_shop_detail
11 回复
public void addToShopDetail(@Param("..")TbAgentShopDetail tbAgentShopDetail) {
// 三表联查,获取相应字段
Sql sql = Sqls.queryRecord("SELECT s.shop_id, s.shop_name, p.pro_id, p.inventory, u.user_type "
+ "FROM tb_user u "
+ "INNER JOIN tb_shop s ON u.user_id=s.user_id "
+ "INNER JOIN tbt_product p ON u.user_type=p.user_type");
dao.execute(sql);
List<Record> list = sql.getList(Record.class);
for (Record record : list) {
Integer shop_id = record.getInt("s.shop_id");
String shop_name = record.getString("s.shop_name");
Integer pro_id = record.getInt("p.pro_id");
Integer inventory = record.getInt("p.inventory");
Cnd cnd = Cnd.where(TbAgentShopDetail.SHOP_ID, "=", shop_id);
cnd.and(TbAgentShopDetail.SHOP_NAME, "=", shop_name);
cnd.and(TbAgentShopDetail.PRO_ID, "=", pro_id);
cnd.and(TbAgentShopDetail.INVENTORY, "=", inventory);
if (tbAgentShopDetailDao.count(cnd) > 0) {
return;
}else {
tbAgentShopDetail.setShop_id(shop_id);
tbAgentShopDetail.setShop_name(shop_name);
tbAgentShopDetail.setPro_id(pro_id);
tbAgentShopDetail.setInventory(inventory);
// 获取当前时间
tbAgentShopDetail.setCreate_time(BurroKit.current());
tbAgentShopDetailDao.fastInsert(tbAgentShopDetail);
}
}
}
在tbAgentShopDetail.setShop_id(shop_id);处报空指针异常,是怎么回事?是for (Record record : list) {}里面
Integer shop_id = record.getInt("s.shop_id");
String shop_name = record.getString("s.shop_name");
Integer pro_id = record.getInt("p.pro_id");
Integer inventory = record.getInt("p.inventory");
这里有问题吗?应该怎么改?
我打印record有值
[{
"shop_id" :17,
"shop_name" :"111",
"pro_id" :30,
"inventory" :100,
"user_type" :"初级代理商"
}, {
"shop_id" :17,
"shop_name" :"111",
"pro_id" :4,
"inventory" :56,
"user_type" :"初级代理商"
}, {
"shop_id" :1,
"shop_name" :"11",
"pro_id" :4,
"inventory" :44,
"user_type" :"中级代理商"
}, {
"shop_id" :17,
"shop_name" :"111",
"pro_id" :16,
"inventory" :22,
"user_type" :"初级代理商"
}, {
"shop_id" :1,
"shop_name" :"11",
"pro_id" :30,
"inventory" :11,
"user_type" :"中级代理商"
}, {
"shop_id" :17,
"shop_name" :"111",
"pro_id" :21,
"inventory" :0,
"user_type" :"初级代理商"
}, {
"shop_id" :17,
"shop_name" :"111",
"pro_id" :11,
"inventory" :0,
"user_type" :"初级代理商"
}, {
"shop_id" :17,
"shop_name" :"111",
"pro_id" :1,
"inventory" :0,
"user_type" :"初级代理商"
}, {
"shop_id" :17,
"shop_name" :"111",
"pro_id" :2,
"inventory" :0,
"user_type" :"初级代理商"
}, {
"shop_id" :17,
"shop_name" :"111",
"pro_id" :10,
"inventory" :0,
"user_type" :"初级代理商"
}, {
"shop_id" :1,
"shop_name" :"11",
"pro_id" :9,
"inventory" :0,
"user_type" :"中级代理商"
}, {
"shop_id" :1,
"shop_name" :"11",
"pro_id" :8,
"inventory" :0,
"user_type" :"中级代理商"
}, {
"shop_id" :1,
"shop_name" :"11",
"pro_id" :2,
"inventory" :0,
"user_type" :"中级代理商"
}, {
"shop_id" :1,
"shop_name" :"11",
"pro_id" :16,
"inventory" :0,
"user_type" :"中级代理商"
}, {
"shop_id" :1,
"shop_name" :"11",
"pro_id" :10,
"inventory" :0,
"user_type" :"中级代理商"
}, {
"shop_id" :1,
"shop_name" :"11",
"pro_id" :3,
"inventory" :0,
"user_type" :"中级代理商"
}, {
"shop_id" :17,
"shop_name" :"111",
"pro_id" :3,
"inventory" :0,
"user_type" :"初级代理商"
报错是在if条件的else语句的第一行
tbAgentShopDetail.setShop_id(shop_id);
是地方
添加回复
请先登陆