ZK Listbox控件从数据库获取数据

文章展示了如何在ZK框架中自定义一个DBListModel,用于从数据库分块加载数据到Listbox,并实现缓存机制。同时,文章还提供了相应的ViewModel实现,包括数据总大小的获取、数据块的加载以及清除缓存的功能。
摘要由CSDN通过智能技术生成
<zk>

	<window title="ZK Listbox  - Load model from DB" border="normal"
		apply="org.zkoss.bind.BindComposer"
		viewModel="@id('vm') @init('fiddle.DBViewModel')">
		
		<listbox mold="paging" model="@load(vm.model)">
			<listhead>
				<listheader label="name"/>
				<listheader label="name again"/>
			</listhead>
			<template name="model">
				<listitem>
					<listcell>
                                          <label value="@load(each)"/>
                                  </listcell>
				</listitem>
			</template>
		</listbox>
                  
                 
		<button label="Clear Cache" onClick="@command('clearCache')"/>
	</window>

</zk>

自定义一个DBListModel:

package fiddle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zul.AbstractListModel;

public abstract class DBListModel<T> extends AbstractListModel<T> {
	private static final long serialVersionUID = 1L;

	private int chunkSize;

	private Integer totalSize = null;
	private Map<Integer, List<T>> chunkCache = new HashMap<Integer, List<T>>();
	
	public DBListModel(int chunkSize) {
		super();
		this.chunkSize = chunkSize;
	}

	@Override
	public T getElementAt(int index) {
		int chunkIndex = index / chunkSize;
		List<T> chunkElements = chunkCache.get(chunkIndex);
		if(chunkElements == null) {
			chunkElements = loadChunkFromDB(chunkIndex * chunkSize, chunkSize);
			chunkCache.put(chunkIndex, chunkElements);
		}
		return chunkElements.get(index % chunkSize);
	}

	@Override
	public int getSize() {
		if(totalSize == null) {
			totalSize = loadTotalSize();
		}
		return totalSize;
	}

	public void clearCaches() {
		totalSize = null;
		chunkCache.clear();
	}
	
	protected abstract int loadTotalSize();

	protected abstract List<T> loadChunkFromDB(int startIndex, int size);
}

viewmodel实现:

package fiddle;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.util.Clients;


public class DBViewModel {

	fiddle.DBListModel<String> model;
	
	@Init 
	public void init() {
	
		model = new fiddle.DBListModel<String>(100) {
			@Override
			protected int loadTotalSize() {
				//in the real app you would call a service and load the count from the DB
				//SQL could be: SELECT COUNT(name) FROM person_table;
				return 1350;
			}
			
			@Override
			protected List<String> loadChunkFromDB(int startIndex, int size) {
				//in the real app you would call a service and load the chunk of data from the DB
				//SQL could be: SELECT name FROM person_table LIMIT $size OFFSET $startIndex; 
				Clients.showNotification(String.format("loaded %d elements starting from position %d", size, startIndex));
				List<String> results = new ArrayList<String>(size);
				for(int i = startIndex; i < startIndex + size; i++) {
					results.add("Name #" + i);
				}
				return results;
			}
		};
	}

	@Command("clearCache")
	@NotifyChange("model")
	public void doClearCache() {
		model.clearCaches();
	}
	
	public DBListModel<String> getModel() {
		return model;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shibushi114

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值