master
WuXianChaoPin 7 years ago
parent 41d46398c3
commit cc40427b45
  1. 3
      core/src/main/java/core/aop/Advice.java
  2. 53
      core/src/main/java/core/dao/BaseDao1.java
  3. 37
      core/src/main/java/core/service/BaseService.java
  4. 11
      core/src/main/java/core/util/DBAction.java
  5. 5
      db/pom.xml
  6. 24
      db/src/main/java/db/config/HibernateConfig.java
  7. 31
      db/src/main/java/db/model/AbstractModel.java
  8. 4
      db/src/main/java/db/model/CidEntity.java
  9. 4
      db/src/main/java/db/model/DataModel.java
  10. 2
      db/src/main/java/db/model/ScheduledTaskEntity.java
  11. 2
      db/src/main/java/db/model/TaskEntity.java
  12. 5
      web/src/main/java/web/controller/AdminController.java
  13. 19
      web/src/main/java/web/controller/BaseController.java
  14. 5
      web/src/main/java/web/controller/CommentController.java
  15. 7
      web/src/main/java/web/controller/DataController.java
  16. 4
      web/src/main/resources/log4j2.xml
  17. 28
      web/src/test/java/SpringTest.java
  18. 8
      web/src/test/java/Test.java

@ -1,5 +1,6 @@
package core.aop; package core.aop;
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;
import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.MethodBeforeAdvice;
@ -15,7 +16,7 @@ import java.util.Arrays;
public class Advice implements MethodBeforeAdvice { public class Advice implements MethodBeforeAdvice {
private Logger log=LogManager.getLogger(); private Logger log=LogManager.getLogger();
@Override @Override
public void before(Method method, Object[] objects, Object o) throws Throwable { public void before(Method method, Object[] objects, Object o) {
log.info("类:" + o.getClass() + ",方法:" + method.getName() + "被调用,传入参数:" + Arrays.asList(objects)); log.info("类:" + o.getClass() + ",方法:" + method.getName() + "被调用,传入参数:" + Arrays.asList(objects));
} }
} }

@ -9,6 +9,8 @@ import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/** /**
* Created by reborn on 2017/7/31. * Created by reborn on 2017/7/31.
@ -22,11 +24,54 @@ public class BaseDao1 extends HibernateTemplate{
super(aliyun); super(aliyun);
} }
public <T extends AbstractModel> void delete(Class<T> tClass, Serializable key) throws DataAccessException {
T model = load(tClass, key); public <T extends AbstractModel> T save(T entity) {
if (model != null) { try {
delete(model); super.save(entity);
}catch (DataAccessException e){
entity.setDataAccessException(e);
}
return entity;
}
public <T extends AbstractModel> T update(T entity){
try {
super.update(entity);
}catch (DataAccessException e){
entity.setDataAccessException(e);
}
return entity;
}
public <T extends AbstractModel> T get(T entity){
try{
entity=(T)(super.get(entity.getClass(),entity.primaryKey()));
}catch (DataAccessException e){
entity.setDataAccessException(e);
}
return entity;
}
public <T extends AbstractModel> T delete(T entity) {
T model = get(entity);
try {
if ( entity.getDataAccessException()!= null) {
super.delete(model);
}
} catch (DataAccessException e) {
entity.setDataAccessException(e);
}
return entity;
}
public <T extends AbstractModel> List<T> delete(List<T> entitys) {
List<T> result=new ArrayList<>();
for(T entity:entitys){
result.add(delete(entity));
} }
return result;
} }

@ -10,6 +10,9 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import static core.util.DBAction.BD;
/** /**
* Created by reborn on 2017/7/28. * Created by reborn on 2017/7/28.
@ -24,37 +27,25 @@ public class BaseService {
@Resource @Resource
protected BaseDao2 baseDao2; protected BaseDao2 baseDao2;
public<T extends AbstractModel> String curd(DBAction action, T command) { public<T extends AbstractModel> T curd(DBAction action, T command) {
Class<T> tClass = (Class<T>) command.getClass();
try {
switch (action) { switch (action) {
case C: case C:
return baseDao1.save(command);
case U: case U:
baseDao1.saveOrUpdate(command); return baseDao1.update(command);
break;
case R: case R:
if(command.primaryKey()!=null) { return baseDao1.get(command);
T model = baseDao1.get(tClass, command.primaryKey());
return model == null ? "找不到主键=" + command.primaryKey() + "" : model.toString();
}else{
throw new RuntimeException(command.getClass()+".primaryKey()方法返回null");
}
case D: case D:
if(command.getCheckeds()==null) { return baseDao1.delete(command);
baseDao1.delete(tClass, command.primaryKey()); default:
}else{ throw new RuntimeException("非法操作:" + action);
for (Serializable checked:command.getCheckeds()){
baseDao1.delete(tClass,checked);
}
} }
break;
} }
return action.getEn() + " success";
} catch (Exception e) { public <T extends AbstractModel> List<T> find(T command, int firstResult, int maxResults){
log.error((command.tableNote()==null?command.getClass():command.tableNote()) + "进行" + action.getCh() + "操作失败,原因是:" + e.getMessage()); return (List<T>) baseDao1.findByCriteria(command.getCriteria(),firstResult,maxResults);
}
return action.getEn() + " fail";
} }
} }

@ -11,7 +11,16 @@ public enum DBAction {
R("读取","Retrieve"), R("读取","Retrieve"),
D("删除","Delete"); D("删除","Delete"),
BC("批量创建","BatchCreate"),
BU("批量更新","BatchUpdate"),
BR("批量读取","BatchRetrieve"),
BD("批量删除","BatchDelete");
private String ch; private String ch;

@ -59,5 +59,10 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid</artifactId> <artifactId>druid</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -1,6 +1,7 @@
package db.config; package db.config;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@ -8,10 +9,14 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties; import java.util.Properties;
/** /**
@ -43,7 +48,7 @@ public class HibernateConfig{
@Value("${sessionFactory.scan}") @Value("${sessionFactory.scan}")
private String packagesToScan[]; private String packagesToScan[];
private static final String tableNote="tableNote";
private LocalSessionFactoryBean aliyun = new LocalSessionFactoryBean(); private LocalSessionFactoryBean aliyun = new LocalSessionFactoryBean();
@ -76,6 +81,7 @@ public class HibernateConfig{
bean.setPackagesToScan(packagesToScan); bean.setPackagesToScan(packagesToScan);
try { try {
bean.afterPropertiesSet(); bean.afterPropertiesSet();
checkClass(bean);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -106,7 +112,21 @@ public class HibernateConfig{
return hibernateTransactionManager; return hibernateTransactionManager;
} }
private void checkClass(LocalSessionFactoryBean bean){
for(Class c:bean.getMetadataSources().getAnnotatedClasses()){
try {
Method method=ReflectionUtils.findMethod(c,tableNote);
Object result=ReflectionUtils.invokeMethod(method,c.newInstance());
if(ObjectUtils.isEmpty(result)){
throw new RuntimeException(method+"没有正确重写");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
} }

@ -1,9 +1,13 @@
package db.model; package db.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import java.io.Serializable; import java.io.Serializable;
@ -18,9 +22,18 @@ public abstract class AbstractModel {
public abstract String tableNote(); public abstract String tableNote();
private DataAccessException dataAccessException;
private static ObjectMapper objectMapper=new ObjectMapper(); private static ObjectMapper objectMapper=new ObjectMapper();
public Serializable[] checkeds; static {
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
private Serializable[] checkeds;
private DetachedCriteria criteria;
public Serializable[] getCheckeds() { public Serializable[] getCheckeds() {
return checkeds; return checkeds;
@ -30,6 +43,22 @@ public abstract class AbstractModel {
this.checkeds = checkeds; this.checkeds = checkeds;
} }
public DataAccessException getDataAccessException() {
return dataAccessException;
}
public void setDataAccessException(DataAccessException dataAccessException) {
this.dataAccessException = dataAccessException;
}
public DetachedCriteria getCriteria() {
return criteria;
}
public void setCriteria(DetachedCriteria criteria) {
this.criteria = criteria;
}
@Override @Override
public String toString() { public String toString() {
try { try {

@ -5,7 +5,7 @@ import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Table(name = "cid", schema = "bilibili", catalog = "") @Table(name = "cid", schema = "bilibili", catalog = "bilibili")
public class CidEntity extends AbstractModel { public class CidEntity extends AbstractModel {
private int cid; private int cid;
private Integer maxlimit; private Integer maxlimit;
@ -279,6 +279,6 @@ public class CidEntity extends AbstractModel {
@Override @Override
public String tableNote() { public String tableNote() {
return null; return "CID表";
} }
} }

@ -5,7 +5,7 @@ import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Table(name = "data", schema = "bilibili", catalog = "") @Table(name = "data", schema = "bilibili", catalog = "bilibili")
public class DataModel extends AbstractModel { public class DataModel extends AbstractModel {
private int cid; private int cid;
private Integer aid; private Integer aid;
@ -376,6 +376,6 @@ public class DataModel extends AbstractModel {
@Override @Override
public String tableNote() { public String tableNote() {
return null; return "history接口数据表";
} }
} }

@ -6,7 +6,7 @@ import java.sql.Timestamp;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Table(name = "scheduled_task", schema = "webcrawler", catalog = "") @Table(name = "scheduled_task", schema = "bilibili", catalog = "bilibili")
public class ScheduledTaskEntity extends AbstractModel { public class ScheduledTaskEntity extends AbstractModel {
private int id; private int id;
private int taskId; private int taskId;

@ -5,7 +5,7 @@ import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Table(name = "task", schema = "webcrawler", catalog = "") @Table(name = "task", schema = "bilibili", catalog = "bilibili")
public class TaskEntity extends AbstractModel { public class TaskEntity extends AbstractModel {
private int id; private int id;
private String api; private String api;

@ -1,5 +1,6 @@
package web.controller; package web.controller;
import core.util.DBAction;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -25,4 +26,8 @@ public class AdminController extends BaseController{
return "test2"; return "test2";
} }
@Override
public boolean checkAction(DBAction action) {
return true;
}
} }

@ -5,16 +5,18 @@ import core.util.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;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
/** /**
* Created by reborn on 2017/9/14. * Created by reborn on 2017/9/14.
*/ */
public class BaseController<T extends AbstractModel>{ public abstract class BaseController<T extends AbstractModel>{
protected Logger log=LogManager.getLogger(); protected Logger log=LogManager.getLogger();
@Resource @Resource
@ -22,10 +24,21 @@ public class BaseController<T extends AbstractModel>{
@ResponseBody @ResponseBody
@RequestMapping(value = "{action}", produces = "application/json;charset=UTF-8") @RequestMapping(value = "{action}", produces = "application/json;charset=UTF-8")
public String curd(@PathVariable DBAction action, T model) { public <T extends AbstractModel> T curd(@PathVariable DBAction action,T model) {
log.info((model.tableNote()==null?model.getClass():model.tableNote()) + "进行" + action.getCh() + "操作请求"); log.info(model.tableNote()+ "进行" + action.getCh() + "操作请求");
return baseService.curd(action, model); return baseService.curd(action, model);
} }
@ResponseBody
@RequestMapping(value = "batch/{action}", produces = "application/json;charset=UTF-8")
public <T extends AbstractModel> List<T> find(@PathVariable DBAction action,T queryCommand,Integer firstResult,Integer maxResults){
if(action.equals(DBAction.BR)) {
return baseService.find(queryCommand, firstResult, maxResults);
}else{
throw new RuntimeException("非法操作:" + action);
}
}
public abstract boolean checkAction(DBAction action);
} }

@ -1,5 +1,6 @@
package web.controller; package web.controller;
import core.util.DBAction;
import db.model.DataModel; import db.model.DataModel;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -8,4 +9,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/comment") @RequestMapping("/comment")
public class CommentController extends BaseController<DataModel>{ public class CommentController extends BaseController<DataModel>{
@Override
public boolean checkAction(DBAction action) {
return true;
}
} }

@ -1,5 +1,6 @@
package web.controller; package web.controller;
import core.util.DBAction;
import db.model.DataModel; import db.model.DataModel;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -7,6 +8,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import web.service.DataService; import web.service.DataService;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.EnumSet;
@Controller @Controller
@RequestMapping("/data") @RequestMapping("/data")
@ -25,4 +27,9 @@ public class DataController extends BaseController<DataModel> {
public void stop(){ public void stop(){
dataService.stop(); dataService.stop();
} }
@Override
public boolean checkAction(DBAction action) {
return !EnumSet.of(DBAction.D,DBAction.BD).contains(action);
}
} }

@ -16,10 +16,10 @@
</Appenders> </Appenders>
<Loggers> <Loggers>
<Logger name="test1" level="TRACE" additivity="false"> <Logger name="test1" level="debug" additivity="false">
<AppenderRef ref="STDOUT"/> <AppenderRef ref="STDOUT"/>
</Logger> </Logger>
<!--<Logger name="org.hibernate.SQL" level="debug" additivity="false">--> <!--<Logger name="org.hibernate" level="debug" additivity="false">-->
<!--<AppenderRef ref="STDOUT"/>--> <!--<AppenderRef ref="STDOUT"/>-->
<!--</Logger>--> <!--</Logger>-->
<Logger name="test2" level="debug" additivity="false"> <Logger name="test2" level="debug" additivity="false">

@ -14,11 +14,20 @@ import org.hibernate.criterion.Projections;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType; import org.hibernate.tool.schema.TargetType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
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 org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import web.config.AppConfig; import web.config.AppConfig;
import web.config.SpringConfig;
import web.controller.DataController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
@ -30,13 +39,30 @@ import java.util.List;
//让测试运行于Spring环境 //让测试运行于Spring环境
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitWebConfig @SpringJUnitWebConfig
@ContextConfiguration(classes = {AppConfig.class}) @ContextConfiguration(classes = {AppConfig.class,SpringConfig.class})
public class SpringTest { public class SpringTest {
private Logger log = LogManager.getLogger(); private Logger log = LogManager.getLogger();
@Resource @Resource
private BaseDao1 baseDao1; private BaseDao1 baseDao1;
@Resource
private DataController dataController;
private MockMvc mockMvc;
@Before
public void setup(){
mockMvc = MockMvcBuilders.standaloneSetup(dataController).build();
}
@Test
public void test() throws Exception {
ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.get("/data/R.do").param("cid", "103"));
MvcResult mvcResult = resultActions.andReturn();
String result = mvcResult.getResponse().getContentAsString();
log.info("=====客户端获得反馈数据:" + result);
}
// @Test // @Test
public void exportTableSql() { public void exportTableSql() {

@ -0,0 +1,8 @@
import db.model.AbstractModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test {
private static Logger log = LogManager.getLogger();
}
Loading…
Cancel
Save