You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.4 KiB
141 lines
4.4 KiB
package web.controller;
|
|
|
|
import db.annotation.TableInfo;
|
|
import db.config.HibernateConfig;
|
|
import db.form.DBAction;
|
|
import db.form.JsonResult;
|
|
import db.form.Type;
|
|
import db.model.AbstractModel;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.hibernate.criterion.DetachedCriteria;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.core.ResolvableType;
|
|
import org.springframework.dao.DataAccessException;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.util.ClassUtils;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import web.service.BaseService;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Created by reborn on 2017/9/14.
|
|
*/
|
|
|
|
public abstract class BaseController<T extends AbstractModel,A extends T,E extends BaseService>{
|
|
protected static Logger log=LogManager.getLogger();
|
|
|
|
@Autowired
|
|
protected E service;
|
|
|
|
private ResolvableType resolvableType=ResolvableType.forClass(ClassUtils.getUserClass(this.getClass()));
|
|
|
|
protected final String tableName;
|
|
|
|
|
|
public BaseController() {
|
|
if(getResolvableType()!=AbstractModel.class){
|
|
TableInfo tableInfo=HibernateConfig.getTableNote(getResolvableType());
|
|
if(tableInfo!=null&&StringUtils.isNotEmpty(tableInfo.comment())){
|
|
tableName=tableInfo.comment();
|
|
}else {
|
|
tableName=getResolvableType().getName();
|
|
}
|
|
}else{
|
|
tableName=null;
|
|
}
|
|
}
|
|
|
|
public JsonResult save(T model){
|
|
JsonResult result=new JsonResult();
|
|
result.setAction(DBAction.C);
|
|
try {
|
|
service.save(model);
|
|
result.setType(Type.success);
|
|
} catch (DataAccessException e) {
|
|
e.printStackTrace();
|
|
result.setType(Type.fail);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@RequestMapping(value = "C",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.GET)
|
|
public JsonResult get(T model){
|
|
JsonResult<T> result=new JsonResult();
|
|
result.setAction(DBAction.R);
|
|
try {
|
|
result.setData(service.get(model));
|
|
result.setType(Type.success);
|
|
} catch (DataAccessException e) {
|
|
e.printStackTrace();
|
|
result.setType(Type.fail);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@ResponseBody
|
|
@RequestMapping(value = "batch_C",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.GET)
|
|
public JsonResult get(A queryCommand, @RequestParam(defaultValue = "0") int firstResult,@RequestParam(defaultValue = "0") int maxResults){
|
|
JsonResult<List> result=new JsonResult();
|
|
result.setAction(DBAction.R);
|
|
try {
|
|
result.setData(service.find(getDetachedCriteria(queryCommand), firstResult, maxResults));
|
|
result.setType(Type.success);
|
|
} catch (DataAccessException e) {
|
|
e.printStackTrace();
|
|
result.setType(Type.fail);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public JsonResult update(T model){
|
|
JsonResult result=new JsonResult();
|
|
result.setAction(DBAction.U);
|
|
try {
|
|
service.update(model);
|
|
result.setType(Type.success);
|
|
} catch (DataAccessException e) {
|
|
e.printStackTrace();
|
|
result.setType(Type.fail);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public JsonResult delete(T model){
|
|
JsonResult result=new JsonResult();
|
|
result.setAction(DBAction.D);
|
|
try {
|
|
service.delete(model);
|
|
result.setType(Type.success);
|
|
} catch (DataAccessException e) {
|
|
e.printStackTrace();
|
|
result.setType(Type.fail);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
public Class<?> getResolvableType() {
|
|
return getResolvableType(0);
|
|
}
|
|
|
|
public Class<?> getResolvableType(int index) {
|
|
return resolvableType.getSuperType().getGeneric(index).resolve();
|
|
}
|
|
|
|
protected DetachedCriteria getDetachedCriteria(A queryCommand){
|
|
return DetachedCriteria.forClass(getResolvableType());
|
|
}
|
|
|
|
|
|
protected boolean checkAction(DBAction action) {
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|