经过 的提示,改进了下BaseController
================================
3月10号又有改进
================================
直接是上 BaseController 的代码
import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import com.jfinal.core.Controller;import com.jfinal.plugin.activerecord.Db;import com.jfinal.plugin.activerecord.Model;import com.jfinal.plugin.activerecord.Page;import com.jfinal.plugin.activerecord.Record;public class BaseController> extends Controller { /** * 获取M的class * * @return M */ @SuppressWarnings("unchecked") public Class getClazz() { Type t = getClass().getGenericSuperclass(); Type[] params = ((ParameterizedType) t).getActualTypeArguments(); return (Class ) params[0]; } /** * 通用分页查找 */ public void getByPage() { Page list = Db.paginate(getParaToInt("pageNumber"), getParaToInt("pageSize"), "select *", "from " + getClazz().getSimpleName() + " order by id desc"); renderJson(list); } /** * 通用查找全部 */ public void getAll() { renderJson(Db.find("select * from " + getClazz().getSimpleName() + " order by id asc;")); } /** * 通用根据id查找 */ public void getById() { renderJson(Db.findById(getClazz().getSimpleName(), getParaToInt("id"))); } /** * 通用新增 * * @throws Exception */ public void save() throws Exception { renderText(getModel( ((Model ) Class.forName(getClazz().getName()).newInstance()) .getClass()).save() + ""); } /** * 通用修改 * * @throws Exception */ public void update() throws Exception { renderText(getModel( ((Model ) Class.forName(getClazz().getName()).newInstance()) .getClass()).update() + ""); } /** * 通用删除 * * @throws Exception */ public void delete() throws Exception { renderText(getModel( ((Model ) Class.forName(getClazz().getName()).newInstance()) .getClass()).delete() + ""); }
然后 Controller 的代码如下
public class CardController extends BaseController{}
这样 CardController 就自动有了基本的增删改查功能了!