master
WuXianChaoPin 6 years ago
parent 0cbe030f24
commit c4b6841ee3
  1. 8
      db/src/main/java/db/form/JsonResult.java
  2. 87
      web/src/main/java/web/controller/BaseController.java
  3. 7
      web/src/main/java/web/controller/DataController.java
  4. 2
      web/src/main/java/web/controller/MenuController.java
  5. 13
      web/src/main/java/web/controller/TableController.java
  6. 39
      web/src/main/java/web/service/BaseService.java
  7. 2
      web/src/test/java/SpringMVCTest.java
  8. 11
      web/src/test/java/SpringTestThrift.java
  9. 10
      web/src/test/java/Test.java
  10. 6
      web/src/test/java/ThriftTest.java

@ -12,9 +12,15 @@ public class JsonResult<T> extends ToJSON {
private DBAction action; private DBAction action;
public JsonResult(T data, Type type, DBAction action) { public void setData(T data) {
this.data = data; this.data = data;
}
public void setType(Type type) {
this.type = type; this.type = type;
}
public void setAction(DBAction action) {
this.action = action; this.action = action;
} }

@ -15,10 +15,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import web.service.BaseService; import web.service.BaseService;
/** /**
@ -49,38 +46,64 @@ public abstract class BaseController<T extends AbstractModel,A extends T,E exten
} }
@ResponseBody @ResponseBody
@RequestMapping(value = "{action}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.POST)
public JsonResult curd(@PathVariable DBAction action, T model) { public JsonResult save(T model){
log.info(tableName+ "进行" + action.getCh() + "操作请求"); JsonResult result=new JsonResult();
if(checkAction(action)) { result.setAction(DBAction.C);
try { try {
return new JsonResult<>(service.curd(action, model), Type.success, action); service.save(model);
}catch (DataAccessException e){ result.setType(Type.success);
log.error(e); } catch (DataAccessException e) {
return new JsonResult<>("非法操作", Type.fail, action); e.printStackTrace();
} result.setType(Type.fail);
}else{
return new JsonResult<>(tableName + "不允许" + action.getCh() + "操作", Type.fail,action);
} }
return result;
}
@ResponseBody
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.GET)
public JsonResult get(T model){
JsonResult result=new JsonResult();
result.setAction(DBAction.R);
try {
service.get(model);
result.setType(Type.success);
} catch (DataAccessException e) {
e.printStackTrace();
result.setType(Type.fail);
}
return result;
} }
@ResponseBody @ResponseBody
@RequestMapping(value = "batch/{action}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.PUT)
public JsonResult find(@PathVariable DBAction action, A queryCommand, @RequestParam Integer firstResult, @RequestParam Integer maxResults){ public JsonResult update(T model){
log.info(tableName + "进行批量" + action.getCh() + "操作请求"); JsonResult result=new JsonResult();
if(checkAction(action)) { result.setAction(DBAction.U);
try { try {
service.update(model);
return new JsonResult<>(service.find(getDetachedCriteria(queryCommand), firstResult, maxResults), Type.success, action); result.setType(Type.success);
} catch (DataAccessException e) { } catch (DataAccessException e) {
log.error(e); e.printStackTrace();
return new JsonResult<>("非法操作", Type.fail, action); result.setType(Type.fail);
}
}else{
return new JsonResult<>(tableName + "不允许" + action.getCh() + "操作", Type.fail,action);
} }
return result;
}
@ResponseBody
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.DELETE)
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() { public Class<?> getResolvableType() {
@ -95,5 +118,9 @@ public abstract class BaseController<T extends AbstractModel,A extends T,E exten
return DetachedCriteria.forClass(getResolvableType()); return DetachedCriteria.forClass(getResolvableType());
} }
protected abstract boolean checkAction(DBAction action);
protected boolean checkAction(DBAction action) {
return false;
}
} }

@ -40,7 +40,7 @@ public class DataController extends TableController<DataModel, DataModelForm, Da
private static final String key = "regionJson"; private static final String key = "regionJson";
public DataController() throws IOException, InstantiationException, IllegalAccessException { public DataController() throws IOException {
super(); super();
} }
@ -124,4 +124,9 @@ public class DataController extends TableController<DataModel, DataModelForm, Da
protected void setModel(Model model) { protected void setModel(Model model) {
model.addAttribute(key, regionJson); model.addAttribute(key, regionJson);
} }
@Override
protected DataTable<DataModel> getNewTable() {
return new DataTable<>();
}
} }

@ -17,7 +17,7 @@ public class MenuController extends BiliController<MenuModel> {
@RequestMapping @RequestMapping
public String find(Model model, MenuModel queryCommand, @RequestParam Integer firstResult, @RequestParam Integer maxResults){ public String find(Model model, MenuModel queryCommand, @RequestParam Integer firstResult, @RequestParam Integer maxResults){
model.addAttribute("menus", super.find(DBAction.R, queryCommand, firstResult, maxResults).toString()); model.addAttribute("menus", service.find(getDetachedCriteria(queryCommand), firstResult, maxResults).toString());
return "menu"; return "menu";
} }

@ -15,23 +15,16 @@ import java.util.List;
public abstract class TableController<T extends AbstractModel,A extends T,E extends BaseService,D extends OtherResult,B extends Table<D,T>> extends BaseController<T,A,E> { public abstract class TableController<T extends AbstractModel,A extends T,E extends BaseService,D extends OtherResult,B extends Table<D,T>> extends BaseController<T,A,E> {
private static final String otherColumns="otherColumns";
private static final int typeIndex=4;
protected final B result = (B) getResolvableType(typeIndex).newInstance();
protected TableController() throws IllegalAccessException, InstantiationException {
}
@RequestMapping("table") @RequestMapping("table")
public String find(Model model,@ModelAttribute("command") A command,B pageResult) { public String find(Model model,@ModelAttribute("command") A command,B pageResult) {
DBAction action=DBAction.R; DBAction action=DBAction.R;
log.info(tableName+ "进行批量" + action.getCh() + "操作请求"); log.info(tableName+ "进行批量" + action.getCh() + "操作请求");
B result = getNewTable();
if(checkAction(action)) { if(checkAction(action)) {
try { try {
Long rowCount=rowCount(command); Long rowCount=rowCount(command);
if(rowCount>0) { if(rowCount>0) {
List list = find(command, pageResult.getCurrentPage()*pageResult.getPageSize(), pageResult.getPageSize()); List list = find(command, pageResult.getCurrentPage()*pageResult.getPageSize(), pageResult.getPageSize());
List<D> otherDatas= otherDatas(command); List<D> otherDatas= otherDatas(command);
result.setCount(rowCount); result.setCount(rowCount);
@ -64,6 +57,8 @@ public abstract class TableController<T extends AbstractModel,A extends T,E exte
} }
protected abstract B getNewTable();
@Override @Override
protected boolean checkAction(DBAction action) { protected boolean checkAction(DBAction action) {
return false; return false;

@ -2,7 +2,6 @@ package web.service;
import db.annotation.Model; import db.annotation.Model;
import db.config.HibernateConfig; import db.config.HibernateConfig;
import db.form.DBAction;
import db.model.AbstractModel; import db.model.AbstractModel;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -12,6 +11,7 @@ import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import java.io.Serializable;
import java.util.List; import java.util.List;
/** /**
@ -31,31 +31,32 @@ public abstract class BaseService{
return hibernateTemplate; return hibernateTemplate;
} }
public final <T extends AbstractModel> T curd(DBAction action, T command) throws DataAccessException { public final <T extends AbstractModel> Serializable save(T command) throws DataAccessException {
switch (action) { return getHibernateTemplate().save(command);
case C: }
getHibernateTemplate().save(command);break;
case U: public final <T extends AbstractModel> void update(T command) throws DataAccessException{
getHibernateTemplate().update(command);break; getHibernateTemplate().update(command);
case R: }
T model=(T)(getHibernateTemplate().get(command.getClass(), command.primaryKey()));
if(model!=null){ public final <T extends AbstractModel> T get(T command) throws DataAccessException{
return model.cast(); T model=(T)getHibernateTemplate().get(command.getClass(),command.primaryKey());
} if(model!=null){
case D: return model.cast();
getHibernateTemplate().delete(command);break; }else{
default: return null;
throw new RuntimeException("非法操作:" + action);
} }
return null; }
public final <T extends AbstractModel> void delete(T command) throws DataAccessException{
getHibernateTemplate().delete(command);
} }
public final List find(DetachedCriteria criteria, int firstResult,int maxResults){ public final List find(DetachedCriteria criteria, int firstResult,int maxResults) throws DataAccessException{
return getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults<getMaxResults()?maxResults:getMaxResults()); return getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults<getMaxResults()?maxResults:getMaxResults());
} }
public final List find(DetachedCriteria criteria){ public final List find(DetachedCriteria criteria) throws DataAccessException{
return getHibernateTemplate().findByCriteria(criteria); return getHibernateTemplate().findByCriteria(criteria);
} }

@ -102,7 +102,7 @@ public class SpringMVCTest {
} }
@Test @Test
public void test7(){ public void findAll(){
biliService.find(DetachedCriteria.forClass(ScheduledTaskEntity.class)).forEach(a->log.info(a)); biliService.find(DetachedCriteria.forClass(ScheduledTaskEntity.class)).forEach(a->log.info(a));
} }

@ -0,0 +1,11 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@PropertySource({"classpath:config.properties"})
@ComponentScan("core.thrift")
public class SpringTestThrift implements WebMvcConfigurer {
}

@ -1,10 +0,0 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test {
private static Logger log = LogManager.getLogger();
public static void main(String[] args) throws Exception {
}
}

@ -6,17 +6,18 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import web.config.SpringConfig;
import java.util.ArrayList; import java.util.ArrayList;
//让测试运行于Spring环境 //让测试运行于Spring环境
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitWebConfig @SpringJUnitWebConfig
@ContextConfiguration(classes = {SpringConfig.class}) @ContextConfiguration(classes = SpringTestThrift.class)
@ComponentScan("core.thrift")
public class ThriftTest { public class ThriftTest {
private static Logger log = LogManager.getLogger(); private static Logger log = LogManager.getLogger();
@ -45,3 +46,4 @@ public class ThriftTest {
} }
} }

Loading…
Cancel
Save