改造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);
}
}