给JFinal的Model添加search,简化简单多条件查询。

改造com.jfinal.plugin.activerecord.Model 类。

这个对于简单的多查询条件的检索页面有一定的帮助。

添加表名变量及设定方法。

private String tableName;

	protected void setTableName(String tableName){
		this.tableName = tableName;
	}
添加以下方法:
private void checkTableName(){
		if (StringKit.isBlank(tableName))
			throw new IllegalArgumentException("tableName can not be blank,please setTableName(tableName)");
	}

	public M searchFirst(String key, Object value) {
		List<M> mList =  search(key, value, "");
		return mList!=null && mList.size()>0 ? mList.get(0):null;
	}

	public List<M> search(String key, Object value) {
		return search(key, value, "");
	}

	public List<M> search(String key, Object value, String orderBy) {
		checkTableName();
		String sql = "select * from " + tableName + " where "+ key +"=? " + orderBy;
		return find(sql, value);
	}

	public M searchFirst(Map<String, Object> maps) {
		List<M> mList =  search(maps, "");
		return mList!=null && mList.size()>0 ? mList.get(0):null;
	}

	public List<M> search(Map<String, Object> maps) {
		return search(maps, "");
	}

	public List<M> search(Map<String, Object> maps, String orderBy) {
		checkTableName();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from ").append(tableName).append(" where 1=1 ");
		List<Object> values = new ArrayList<Object>();
		for(Entry<String,Object> entry:maps.entrySet()){
			if(entry.getValue() != null){
				sb.append(" and ").append(entry.getKey()).append("=?");
				values.add(entry.getValue());
			}
		}
		sb.append(" ").append(orderBy);
		return find(sb.toString(), values.toArray());
	}

	public List<M> searchByCache(String cacheName, Object key, Map<String, Object> maps){
		return this.searchByCache(cacheName, key, maps,"");
	}

	public List<M> searchByCache(String cacheName, Object key, Map<String, Object> maps, String orderBy) {
		checkTableName();
		ICache cache = DbKit.getCache();
		List<M> result = cache.get(cacheName, key);
		if (result == null) {
			result = search(maps);
			cache.put(cacheName, key, result);
		}
		return result;
	}

	public Page<M> searchPaginate(int pageNumber, int pageSize, Map<String, Object> maps){
		return this.searchPaginate(pageNumber, pageSize, maps, "");
	}

	public Page<M> searchPaginate(int pageNumber, int pageSize, Map<String, Object> maps, String orderBy) {
		checkTableName();
		StringBuilder sb = new StringBuilder();
		sb.append("from ").append(tableName).append(" where 1=1");
		List<Object> values = new ArrayList<Object>();
		for(Entry<String,Object> entry:maps.entrySet()){
			if(entry.getValue() != null){
				sb.append(" and ").append(entry.getKey()).append("=?");
				values.add(entry.getValue());
			}
		}
		return paginate(pageNumber, pageSize, "select *", sb.toString(),values.toArray());
	}

	public Page<M> searchPaginateByCache(String cacheName, Object key, int pageNumber, int pageSize, Map<String, Object> maps) {
		return this.searchPaginateByCache(cacheName, key, pageNumber, pageSize, maps, "");
	}

	public Page<M> searchPaginateByCache(String cacheName, Object key, int pageNumber, int pageSize, Map<String, Object> maps, String orderBy) {
		checkTableName();
		ICache cache = DbKit.getCache();
		Page<M> result = cache.get(cacheName, key);
		if (result == null) {
			result = searchPaginate(pageNumber, pageSize, maps, orderBy);
			cache.put(cacheName, key, result);
		}
		return result;
	}
调用示例:
public class Children extends Model<Children>{

	private static final long serialVersionUID = -146846348250148850L;

	private static final String KEY_SCHOOL_ID = "school_id";
	private static final String KEY_NAME = "name";
	private static final String KEY_CLASS_ID= "class_id";
	private static final String KEY_STATUS = "status";
	private static final int STATUS_OK = 0;

	public static final Children dao = new Children();

	public Children(){
		this.setTableName("children");
	}

	public List<Children> getChildrenOfClass(int classId){
		return Children.dao.search(KEY_CLASS_ID, classId);
	}

	public List<Children> findChildrenByName(int schoolId, String name){
		Map<String, Object> conds= new HashMap <String, Object>();
		conds.put(KEY_SCHOOL_ID, schoolId);
		conds.put(KEY_NAME, name);
		conds.put(KEY_STATUS, STATUS_OK);
		return dao.search(conds);
	}
}

转载于:https://my.oschina.net/myaniu/blog/137065

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值