NutzCN Logo
精华 原生sql list问题
发布于 3186天前 作者 javanan 2179 次浏览 复制 上一个帖子 下一个帖子
标签:

service 层

public List<HoseOrderVo> getHoseNameAndId() {
		Sql sql = Sqls.queryRecord("select  t1.houseid,t1.title,o.begintime,o.endtime  FROM house AS t1 left join orderinfo AS o on t1.houseid=o.houseid");
		dao().execute(sql);
		return sql.getList(HoseOrderVo.class);
	}

controller

	public void getHouseStatue(String yearMonth){
		List<HoseOrderVo> list=houseService.getHoseNameAndId();
		System.out.println(list);
	}

vo层

public class HoseOrderVo implements Serializable {
	private String houseid;
	private String title;

	List<BEtime> time;

	public List<BEtime> getTime() {
		return time;
	}

	public void setTime(List<BEtime> time) {
		this.time = time;
	}

	public String getHouseid() {
		return houseid;
	}

	public void setHouseid(String houseid) {
		this.houseid = houseid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}

class BEtime {

	private String begintime;
	private String endtime;

	public String getBegintime() {
		return begintime;
	}

	public void setBegintime(String begintime) {
		this.begintime = begintime;
	}

	public String getEndtime() {
		return endtime;
	}

	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}

}

运行结果
[{
"houseid" :"7b235124b2fe4c9aa7f9f9b051c5acfe",
"title" :"1",
"begintime" :"1456366485",
"endtime" :"1456366485"
}, {
"houseid" :"7b235124b2fe4c9aa7f9f9b051c5acfe",
"title" :"1",
"begintime" :"1456366485",
"endtime" :"1456366485"
}, {
"houseid" :"7b235124b2fe4c9aa7f9f9b051c5acfe",
"title" :"1",
"begintime" :"1456366485",
"endtime" :"1456366485"
}, {
"houseid" :"7b235124b2fe4c9aa7f9f9b051c5acfe",
"title" :"1",
"begintime" :"1456366485",
"endtime" :"1456366485"
}, {
"houseid" :"0e1e3ed9cdba4e729520e5235d83d3ed",
"title" :"test",
"begintime" :"1458144000",
"endtime" :"1458489600"
}, {
"houseid" :"a000a8273c124d499ac4b3866a251594",
"title" :"图片",
"begintime" :"1456848000",
"endtime" :"1456934400"
}, {

原生sql 不会把结果自动装进 list 吗?

11 回复

@wendal SQL without entity :

@javanan 自然还得setEntity

@wendal
设置成这样了

Entity entity = dao().getEntity(HoseOrderVo.class);
		sql.setEntity(entity);
		dao().execute(sql);
		return sql.getList(HoseOrderVo.class);

返回的 结果
[{
"houseid" :"7b235124b2fe4c9aa7f9f9b051c5acfe",
"title" :"1"
}, {
"houseid" :"7b235124b2fe4c9aa7f9f9b051c5acfe",
"title" :"1"
}, {
"houseid" :"7b235124b2fe4c9aa7f9f9b051c5acfe",
"title" :"1"
}, {

@javanan 哦,对,不支持。。。

@wendal 那么 nutz是怎么做 的?

@wendal 用什么方式实现这样的 需求?

需要自定义callback了

还是不行哦

public List<HousePojo> randFetch() {
		Sql sql = Sqls.create("SELECT t1.houseid,t1.pricebyday FROM house AS t1 where isshouwindex=1;");
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<HousePojo> list = new LinkedList<HousePojo>();
				while (rs.next()) {
					HousePojo house = new HousePojo();
					house.setHouseid(rs.getString("houseid"));
					house.setPriceByDay(new BigDecimal(rs.getString("pricebyday")));
					list.add(house);
				}

				return list;
			}
		});
		dao().execute(sql);
		return sql.getList(HousePojo.class);
	}

发错了 是这个。。。。

	public List<HoseOrderVo> getHoseNameAndId() {
		Sql sql = Sqls.create(
				"select  t1.houseid,t1.title,o.begintime,o.endtime  FROM house AS t1 left join orderinfo AS o on t1.houseid=o.houseid");
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<HoseOrderVo> list = new LinkedList<HoseOrderVo>();
				while (rs.next()) {
					HoseOrderVo house = new HoseOrderVo();
					house.setHouseid(rs.getString("houseid"));
					house.setTitle(rs.getString("title"));
					List<BEtime> belist = new ArrayList<BEtime>();
					/*BEtime be=new BEtime();
					be.setBegintime(begintime);*/
					house.setTime(belist);
					list.add(house);
				}

				return list;
			}
		});
		dao().execute(sql);
		return sql.getList(HoseOrderVo.class);
	}
添加回复
请先登陆
回到顶部