本项目基于SpringBoot+Jfinal
关于整合方案请参考http://www.jfinal.com/share/1402
BaseDao代码
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Model; import com.jfinal.plugin.activerecord.Page; import com.jfinal.plugin.activerecord.Record; import com.jfinal.plugin.activerecord.SqlPara; import com.jfinal.plugin.activerecord.Table; import com.jfinal.plugin.activerecord.TableMapping; /** * 顶级baseDao * @author 小李子 * * @param <M> */ public abstract class BaseDao <M extends Model<?>>{ /** * 实体 */ protected M model; /** * 过滤列 */ protected List<String> filterColumns; /** * 设置Model */ private void setModelIn() { model=setModel(); } /** * 设置过滤不查询的列 */ private void setFilterColumnsIn() { filterColumns=setFilterColumns(); } /** * 设置Model * 子类需实现该方法 * 不然会报空指针异常 * @return M */ public abstract M setModel(); /** * 设置不查询的列 * 子类若实现该方法并返回非空集合 * 则查询时过滤该列 * @return */ public abstract List<String> setFilterColumns(); /** * 基础增删改方法 * @param m * @return boolean */ public boolean insert(M m) { return m.save(); } public boolean update(M m) { return m.update(); } public boolean del(M m) { return m.delete(); } /** * 根据id查询 * @param id * @return M */ @SuppressWarnings("unchecked") public M findById(Object id) { HashMap<String, Object> params=new HashMap<String, Object>(); params.put("id", id); String sql = getSql(params, null, "limit 1"); SqlPara sqlPara = getSqlPara(sql); return (M) model.findFirst(sqlPara); } /** * 根据id删除 * @param id * @return boolean */ public boolean delById(Object id) { setModelIn(); return model.deleteById(id); } /** * 查询方法 * @param params 参数 * @param orderBy 排序 * @param sql otherSql 如 limit * @return List<M> */ @SuppressWarnings("unchecked") public List<M> select(HashMap<String, Object> params,HashMap<String, Object> orderBy,String sql){ String sql2 = getSql(params, orderBy, sql); List<M> find = (List<M>) model.find(sql2); return find; } /** * 分页查询 * @param params 参数 * @param orderBy 排序 * @param sql otherSql 如 limit * @param pageNumber 页码 * @param pageSize 页大小 * @return */ public Page<Map<String,Object>> page(HashMap<String, Object> params,HashMap<String, Object> orderBy,String sql,int pageNumber,int pageSize){ String sql2 = getSql(params, orderBy, sql); SqlPara sqlPara=new SqlPara(); sqlPara.setSql(sql2); Page<Record> paginate = Db.paginate(pageNumber, pageSize, sqlPara); return toMapPage(paginate); } /** * 获取SqlPara * @param sql sql语句 * @return SqlPara */ private SqlPara getSqlPara(String sql) { SqlPara sqlPara = new SqlPara(); sqlPara.setSql(sql); return sqlPara; } /** * 转换sql语句 * @param params 参数 * @param orderBy 排序 * @param sql otherSql 如 limit * @return sql */ private String getSql(HashMap<String, Object> params,HashMap<String, Object> orderBy,String sql) { setModelIn(); StringBuffer s=new StringBuffer(); s.append("select "); Set<String> columnNameSet = getTable().getColumnNameSet(); String columnNames = columnNameSet.toString().replace("[", "").replace("]", "").replace(" ", ""); setFilterColumnsIn(); if (filterColumns!=null&&filterColumns.size()!=0) { for (String column : filterColumns) { if (columnNames.contains(column)) { columnNames=columnNames.replace(","+column, ""); if (columnNames.contains(column)) { columnNames=columnNames.replace(column+",", ""); } if (columnNames.contains(column)) { columnNames=columnNames.replace(column, ""); } } } } s.append(columnNames); s.append(" from "); s.append(getTable().getName()); s.append(" where 1=1 "); if (params!=null) { Set<String> keySet = params.keySet(); for (String key : keySet) { s.append("and "); s.append(key); s.append("="); s.append(params.get(key)); } } if (sql!=null) { s.append(" "); s.append(sql); } if (orderBy!=null) { Set<String> keySet = orderBy.keySet(); s.append(" "); s.append("order by"); s.append(" "); for (String key : keySet) { if (orderBy.get(key)!=null) { s.append(key); s.append(" "); s.append(orderBy.get(key)); } } } return s.toString(); } /** * 获取table * @return */ private Table getTable() { return TableMapping.me().getTable(model.getClass()); } /** * 将Page<Record>列表转换成Page<Map>列表 * @param Page<Record> * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) private Page<Map<String,Object>> toMapPage(Page<Record> recordPage){ List<Map<String,Object>> list = toMapList(recordPage.getList()); Page<Map<String,Object>> result = new Page( list, recordPage.getPageNumber(), recordPage.getPageSize(), recordPage.getTotalPage(), recordPage.getTotalRow()); return result; } /** * 将Record列表转换成Map列表 * @param recordList * @return */ private List<Map<String,Object>> toMapList(List<Record> recordList){ List<Map<String,Object>> result = new ArrayList<Map<String,Object>>(); if(null!=recordList && !recordList.isEmpty()){ for(Record record:recordList){ result.add(record.getColumns()); } } return result; } }
使用方法
创建Dao层
import java.util.List; import org.springframework.stereotype.Repository; import com.wx.base.BaseDao; import com.wx.model.Configs; @Repository /** * 继承BaseDao * 并实现其抽象方法 * setModel() 方法为必须返回一个非空值 * @author 小李子 * */ public class ConfigDao extends BaseDao<Configs>{ @Override public Configs setModel() { return new Configs(); } @Override public List<String> setFilterColumns() { return null; } }
Controller调用
为了方便省略service层
实际开发中不建议省略
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSONObject; import com.wx.dao.ConfigDao; import com.wx.entity.ResultBean; import com.wx.model.Configs; @RequestMapping("config") @RestController public class ConfigController { @Autowired private ConfigDao cd; /** * 根据id查询 * @param id * @return */ @RequestMapping("test") public Object test(String id) { return cd.findById(id); } /** * 查询全部 * @return */ @RequestMapping("getConfig") public ResultBean getConfig() { try { List<Configs> select = cd.select(null, null,null); return ResultBean.success(select.get(0)); } catch (Exception e) { e.printStackTrace(); return ResultBean.fail("系统错误,请稍后再试"); } } /** * 修改 * @param json * @return */ @RequestMapping("modify") public ResultBean modify(String json) { try { Configs config = JSONObject.parseObject(json, Configs.class); boolean update = cd.update(config); if (update) { return ResultBean.success(); }else { return ResultBean.fail("修改失败,请稍后再试"); } } catch (Exception e) { e.printStackTrace(); return ResultBean.fail("系统错误,请稍后再试"); } } }
生成的sql为
Sql: select msg,img,id,title from configs where 1=1
查询结果为
{"id":1,"msg":"test","img":"test","title":"test"}
若设置了过滤列
Dao层实现方法
@Override public List<String> setFilterColumns() { List<String> strs=new ArrayList<String>(); strs.add("title"); return strs; }
生成sql为
Sql: select msg,img,id from configs where 1=1 and id=1 limit 1
查询结果为
{"id":1,"msg":"test","img":"test","title":null}
希望各位大佬给点改进建议
jfinal 的 active record 模块早已发布为独立的项目,可以单独引入,可以进一步降低 jar 包的大小:
https://www.jfinal.com/doc/5-15
https://www.jfinal.com/doc/6-10
谢谢分享