master
WuXianChaoPin 7 years ago
parent 0cbe030f24
commit c4b6841ee3
  1. 8
      db/src/main/java/db/form/JsonResult.java
  2. 77
      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. 33
      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;
public JsonResult(T data, Type type, DBAction action) {
public void setData(T data) {
this.data = data;
}
public void setType(Type type) {
this.type = type;
}
public void setAction(DBAction action) {
this.action = action;
}

@ -15,10 +15,7 @@ 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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import web.service.BaseService;
/**
@ -49,38 +46,64 @@ public abstract class BaseController<T extends AbstractModel,A extends T,E exten
}
@ResponseBody
@RequestMapping(value = "{action}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public JsonResult curd(@PathVariable DBAction action, T model) {
log.info(tableName+ "进行" + action.getCh() + "操作请求");
if(checkAction(action)) {
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.POST)
public JsonResult save(T model){
JsonResult result=new JsonResult();
result.setAction(DBAction.C);
try {
return new JsonResult<>(service.curd(action, model), Type.success, action);
}catch (DataAccessException e){
log.error(e);
return new JsonResult<>("非法操作", Type.fail, action);
service.save(model);
result.setType(Type.success);
} catch (DataAccessException e) {
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
@RequestMapping(value = "batch/{action}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public JsonResult find(@PathVariable DBAction action, A queryCommand, @RequestParam Integer firstResult, @RequestParam Integer maxResults){
log.info(tableName + "进行批量" + action.getCh() + "操作请求");
if(checkAction(action)) {
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.PUT)
public JsonResult update(T model){
JsonResult result=new JsonResult();
result.setAction(DBAction.U);
try {
return new JsonResult<>(service.find(getDetachedCriteria(queryCommand), firstResult, maxResults), Type.success, action);
service.update(model);
result.setType(Type.success);
} catch (DataAccessException e) {
log.error(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.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() {
@ -95,5 +118,9 @@ public abstract class BaseController<T extends AbstractModel,A extends T,E exten
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";
public DataController() throws IOException, InstantiationException, IllegalAccessException {
public DataController() throws IOException {
super();
}
@ -124,4 +124,9 @@ public class DataController extends TableController<DataModel, DataModelForm, Da
protected void setModel(Model model) {
model.addAttribute(key, regionJson);
}
@Override
protected DataTable<DataModel> getNewTable() {
return new DataTable<>();
}
}

@ -17,7 +17,7 @@ public class MenuController extends BiliController<MenuModel> {
@RequestMapping
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";
}

@ -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> {
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")
public String find(Model model,@ModelAttribute("command") A command,B pageResult) {
DBAction action=DBAction.R;
log.info(tableName+ "进行批量" + action.getCh() + "操作请求");
B result = getNewTable();
if(checkAction(action)) {
try {
Long rowCount=rowCount(command);
if(rowCount>0) {
List list = find(command, pageResult.getCurrentPage()*pageResult.getPageSize(), pageResult.getPageSize());
List<D> otherDatas= otherDatas(command);
result.setCount(rowCount);
@ -64,6 +57,8 @@ public abstract class TableController<T extends AbstractModel,A extends T,E exte
}
protected abstract B getNewTable();
@Override
protected boolean checkAction(DBAction action) {
return false;

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

@ -102,7 +102,7 @@ public class SpringMVCTest {
}
@Test
public void test7(){
public void findAll(){
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.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import web.config.SpringConfig;
import java.util.ArrayList;
//让测试运行于Spring环境
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitWebConfig
@ContextConfiguration(classes = {SpringConfig.class})
@ContextConfiguration(classes = SpringTestThrift.class)
@ComponentScan("core.thrift")
public class ThriftTest {
private static Logger log = LogManager.getLogger();
@ -45,3 +46,4 @@ public class ThriftTest {
}
}

Loading…
Cancel
Save