贴个全的代码
package com.naxxm.service.impl;
import java.text.ParseException;
import java.util.List;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.FieldFilter;
import org.nutz.dao.FieldMatcher;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.Param;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;
import com.naxxm.MyDataSource;
import com.naxxm.bean.company.SysEmployee;
import com.naxxm.bean.system.SysCompany;
import com.naxxm.bean.system.SysDB;
import com.naxxm.service.DataService;
import com.naxxm.utils.DataUtils2;
import com.naxxm.utils.Transform2;
/**
* @ClassName: DataServiceImpl
* @Description: TODO
* @author: liuyc
* @date: 2018-1-18 下午7:38:28
*/
@IocBean
public class DataServiceImpl implements DataService {
@Inject
private Dao dao;
@Inject
private Dao mydao;
public static final Log Log = Logs.get();
public JSONObject query(Integer limit, Integer offset, String searchRange, String searchVal, Class<?> type, String actived, String locked, Boolean ignoreNull) {
Cnd cnd = Cnd.NEW();
if (Strings.isNotBlank(searchVal) && Strings.isNotBlank(searchRange)) {
searchVal = "%" + searchVal + "%";
cnd.or(searchRange, "like", searchVal);
}
int pageNumber = (int) Math.ceil(offset / limit) + 1;
Pager pager = new Pager();
pager.setPageNumber(pageNumber);
pager.setPageSize(limit);
FieldMatcher rule = FieldMatcher.make(actived, locked, true);
List<?> rows = Daos.ext(dao, FieldFilter.create(type, rule)).query(type, cnd, pager);
Log.debug(Json.toJson(rows));
// 设置总个数
pager.setRecordCount(dao.count(type, cnd));
JSONObject table = new JSONObject();
table.put("@type", "table");
table.put("rows", rows);
JSONObject data = new JSONObject();
data.put("sys.count", pager.getRecordCount());
table.put("data", data);
return table;
}
public void save(@Param("..") JSONObject params) throws ParseException {
Table table = Transform.jsonToTable(params.getJSONObject("table"));
// 更新
List<?> list = Transform2.tableToPojo(table, RowState.EDIT, params.getClass());
dao.update(list, DataUtils2.arrayJoin4Regex(table.getColumnNames().toArray(), "%s", "|"));
// 添加
list = Transform2.tableToPojo(table, RowState.NEW, params.getClass());
dao.insert(list);
// 删除
list = Transform2.tableToPojo(table, RowState.DELETE, params.getClass());
dao.delete(list);
}
public void dataSourceChange(String company) throws ClassNotFoundException {
SysCompany companyDB = dao.fetch(SysCompany.class, Cnd.where("bm", "=", company));
if (companyDB != null && companyDB.getDBInfo() != null) {
String[] DBInfo = companyDB.getDBInfo().split("/");
System.out.println("------------------------------------------------------------------------------------------------");
System.out.println(companyDB.getMc() + "---" + DBInfo[0] + "---" + DBInfo[1] + "---" + DBInfo[2] + "---" + DBInfo[3].split(",")[0]);
System.out.println("------------------------------------------------------------------------------------------------");
// 判断用户数据库是否初始化对象获取
SysDB sysDB = dao.fetch(SysDB.class, Cnd.where("db", "=", DBInfo[3].split(",")[0]));
Mvcs.setSessionAttrSafe("companyDB", companyDB, true);
MyDataSource myDataSource = new MyDataSource();
NutDao mydao = new NutDao(myDataSource);
// 判断用户数据库是否初始化
if (sysDB != null && sysDB.getState().equals("0")) {
System.out.println("--->数据库未初始化,初始化数据库");
Daos.createTablesInPackage(mydao, "com.naxxm.bean.company", false);
sysDB.setState("1");
dao.update(sysDB);
} else {
System.out.println("--->数据库已经初始化");
}
}
}
}