diff --git a/core/pom.xml b/core/pom.xml index 9ee5a8f..0be9455 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -11,6 +11,9 @@ core https://gitee.com/WuXianChaoPin/webCrawler + + RELEASE + @@ -24,71 +27,22 @@ spring-webmvc - - org.springframework - spring-orm - org.apache.commons - commons-dbcp2 - - - - org.hibernate - hibernate-core - - - - mysql - mysql-connector-java - - - - com.fasterxml.jackson.core - jackson-databind - - - - org.aspectj - aspectjweaver - - - - org.apache.logging.log4j - log4j-api - - - - org.apache.logging.log4j - log4j-core - - - - org.apache.logging.log4j - log4j-web - - - - commons-io - commons-io + commons-lang3 - com.alibaba - druid + fastjson + - org.apache.commons - commons-lang3 - - - org.pqh - db - 1.0-SNAPSHOT - compile + org.apache.thrift + libthrift + ${thrift.version} diff --git a/web/src/main/java/web/thrift/QryResult.java b/core/src/main/java/core/thrift/QryResult.java similarity index 99% rename from web/src/main/java/web/thrift/QryResult.java rename to core/src/main/java/core/thrift/QryResult.java index fbd6b8d..fffed76 100644 --- a/web/src/main/java/web/thrift/QryResult.java +++ b/core/src/main/java/core/thrift/QryResult.java @@ -4,7 +4,7 @@ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING * @generated */ -package web.thrift; +package core.thrift; @SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"}) @javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.11.0)", date = "2018-04-28") diff --git a/web/src/main/java/web/thrift/QueryImp.java b/core/src/main/java/core/thrift/QueryImp.java similarity index 95% rename from web/src/main/java/web/thrift/QueryImp.java rename to core/src/main/java/core/thrift/QueryImp.java index 0684185..d8a22f5 100644 --- a/web/src/main/java/web/thrift/QueryImp.java +++ b/core/src/main/java/core/thrift/QueryImp.java @@ -1,4 +1,4 @@ -package web.thrift; +package core.thrift; import org.apache.thrift.TException; diff --git a/web/src/main/java/web/thrift/TestQry.java b/core/src/main/java/core/thrift/TestQry.java similarity index 99% rename from web/src/main/java/web/thrift/TestQry.java rename to core/src/main/java/core/thrift/TestQry.java index 7c56e15..c5d2402 100644 --- a/web/src/main/java/web/thrift/TestQry.java +++ b/core/src/main/java/core/thrift/TestQry.java @@ -4,7 +4,7 @@ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING * @generated */ -package web.thrift; +package core.thrift; @SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"}) @javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.11.0)", date = "2018-04-28") diff --git a/web/src/main/java/web/thrift/ThriftClientDemo.java b/core/src/main/java/core/thrift/ThriftClientDemo.java similarity index 98% rename from web/src/main/java/web/thrift/ThriftClientDemo.java rename to core/src/main/java/core/thrift/ThriftClientDemo.java index 112b374..91ab943 100644 --- a/web/src/main/java/web/thrift/ThriftClientDemo.java +++ b/core/src/main/java/core/thrift/ThriftClientDemo.java @@ -1,4 +1,4 @@ -package web.thrift; +package core.thrift; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; diff --git a/web/src/main/java/web/thrift/ThriftServerDemo.java b/core/src/main/java/core/thrift/ThriftServerDemo.java similarity index 98% rename from web/src/main/java/web/thrift/ThriftServerDemo.java rename to core/src/main/java/core/thrift/ThriftServerDemo.java index 782ed12..45ec5a0 100644 --- a/web/src/main/java/web/thrift/ThriftServerDemo.java +++ b/core/src/main/java/core/thrift/ThriftServerDemo.java @@ -1,4 +1,4 @@ -package web.thrift; +package core.thrift; import org.apache.thrift.TProcessorFactory; import org.apache.thrift.protocol.TBinaryProtocol; diff --git a/db/src/main/java/db/util/ClassScaner.java b/core/src/main/java/core/util/ClassScaner.java similarity index 99% rename from db/src/main/java/db/util/ClassScaner.java rename to core/src/main/java/core/util/ClassScaner.java index 9bcee1a..6c8d773 100644 --- a/db/src/main/java/db/util/ClassScaner.java +++ b/core/src/main/java/core/util/ClassScaner.java @@ -1,4 +1,4 @@ -package db.util; +package core.util; import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.BeanDefinitionStoreException; diff --git a/core/src/main/java/core/util/ToJSON.java b/core/src/main/java/core/util/ToJSON.java new file mode 100644 index 0000000..e8806f9 --- /dev/null +++ b/core/src/main/java/core/util/ToJSON.java @@ -0,0 +1,19 @@ +package core.util; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; + +import java.util.ArrayList; +import java.util.List; + +public class ToJSON { + + protected List serializerFeatures(){ + return new ArrayList<>(); + } + + @Override + public String toString() { + return JSON.toJSONString(this,serializerFeatures().toArray(new SerializerFeature[0])); + } +} diff --git a/db/pom.xml b/db/pom.xml index be5757b..52cf62e 100644 --- a/db/pom.xml +++ b/db/pom.xml @@ -30,11 +30,6 @@ log4j-web - - com.fasterxml.jackson.core - jackson-databind - - org.hibernate hibernate-core @@ -64,5 +59,12 @@ org.apache.commons commons-lang3 + + org.pqh + core + 1.0-SNAPSHOT + compile + + \ No newline at end of file diff --git a/db/src/main/java/db/AbstractModel.java b/db/src/main/java/db/AbstractModel.java deleted file mode 100644 index 34d5bb4..0000000 --- a/db/src/main/java/db/AbstractModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package db; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import org.hibernate.criterion.DetachedCriteria; - -import java.io.Serializable; - -/** - * Created by reborn on 2017/8/3. - */ - -public abstract class AbstractModel extends ToJSON{ - - public abstract Serializable primaryKey(); - - public abstract String tableNote(); - - @JsonIgnore - public DetachedCriteria getCriteria() { - return DetachedCriteria.forClass(this.getClass()); - } - @SuppressWarnings("unchecked") - public T cast() throws ClassCastException{ - return (T) this; - } -} diff --git a/db/src/main/java/db/ToJSON.java b/db/src/main/java/db/ToJSON.java deleted file mode 100644 index 9f31b66..0000000 --- a/db/src/main/java/db/ToJSON.java +++ /dev/null @@ -1,33 +0,0 @@ -package db; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -public class ToJSON { - private static ObjectMapper mapper=new ObjectMapper(); - - private static Logger log=LogManager.getLogger(); - - static { - init(); - } - - protected static void init(){ - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.enable(SerializationFeature.INDENT_OUTPUT); - } - - @Override - public String toString() { - try { - return mapper.writeValueAsString(this); - } catch (JsonProcessingException e) { - log.error(e); - return super.toString(); - } - } -} diff --git a/db/src/main/java/db/annotation/BiliBili.java b/db/src/main/java/db/annotation/BiliBili.java index 74e3eb4..084693a 100644 --- a/db/src/main/java/db/annotation/BiliBili.java +++ b/db/src/main/java/db/annotation/BiliBili.java @@ -1,4 +1,8 @@ package db.annotation; +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) public @interface BiliBili { } diff --git a/db/src/main/java/db/annotation/TableInfo.java b/db/src/main/java/db/annotation/TableInfo.java new file mode 100644 index 0000000..3f30f28 --- /dev/null +++ b/db/src/main/java/db/annotation/TableInfo.java @@ -0,0 +1,12 @@ +package db.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface TableInfo { + String comment(); +} diff --git a/db/src/main/java/db/config/HibernateConfig.java b/db/src/main/java/db/config/HibernateConfig.java index 185f95c..a56bdbd 100644 --- a/db/src/main/java/db/config/HibernateConfig.java +++ b/db/src/main/java/db/config/HibernateConfig.java @@ -1,9 +1,9 @@ package db.config; import com.alibaba.druid.pool.DruidDataSource; -import db.AbstractModel; +import core.util.ClassScaner; +import db.annotation.TableInfo; import db.model.DataSourceModel; -import db.util.ClassScaner; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hibernate.SessionFactory; @@ -12,15 +12,12 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; -import org.springframework.util.ObjectUtils; -import org.springframework.util.ReflectionUtils; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.sql.DataSource; import java.io.IOException; import java.lang.annotation.Annotation; -import java.lang.reflect.Method; import java.util.*; /** @@ -65,6 +62,12 @@ public class HibernateConfig { return map; } + private static final Map tableNotes=new HashMap<>(); + + public static TableInfo getTableNote(Class c){ + return tableNotes.get(c); + } + private DruidDataSource dataSource(String url, String username, String password) { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); @@ -100,10 +103,12 @@ public class HibernateConfig { if (Annotation.class.isAssignableFrom(c)) { classes = ClassScaner.scan(basePackage, (Class) c); + classes.forEach(this::checkClass); bean.setAnnotatedClasses(classes.toArray(new Class[classes.size()])); } else { log.error(c+"不是注解!!!"); } + }); if (sessionFactory != null) { @@ -158,7 +163,6 @@ public class HibernateConfig { try { config.addConfig(bean); bean.afterPropertiesSet(); - checkClass(bean); return bean.getObject(); } catch (IOException e) { log.error(e); @@ -166,27 +170,11 @@ public class HibernateConfig { return null; } - private void checkClass(LocalSessionFactoryBean bean) { - for (Class c : bean.getMetadataSources().getAnnotatedClasses()) { - if (AbstractModel.class.isAssignableFrom(c)) { - try { - Method method = ReflectionUtils.findMethod(c, tableNote); - Object result = ReflectionUtils.invokeMethod(method, c.newInstance()); - if (ObjectUtils.isEmpty(result)) { - throw new RuntimeException(method + "没有正确重写"); - }else{ - log.info("扫描实体类:"+c+",映射表:"+result); - } - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } - } else { - log.error(c + "没有继承" + AbstractModel.class); - } - } - + private void checkClass(Class c) { + if(c.isAnnotationPresent(TableInfo.class)){ + TableInfo info= (TableInfo) c.getAnnotation(TableInfo.class); + tableNotes.put(c,info); + } } diff --git a/db/src/main/java/db/DBAction.java b/db/src/main/java/db/form/DBAction.java similarity index 96% rename from db/src/main/java/db/DBAction.java rename to db/src/main/java/db/form/DBAction.java index 5eb52fb..597a0bc 100644 --- a/db/src/main/java/db/DBAction.java +++ b/db/src/main/java/db/form/DBAction.java @@ -1,4 +1,4 @@ -package db; +package db.form; /** * Created by reborn on 2017/8/2. diff --git a/db/src/main/java/db/JsonResult.java b/db/src/main/java/db/form/JsonResult.java similarity index 81% rename from db/src/main/java/db/JsonResult.java rename to db/src/main/java/db/form/JsonResult.java index d6653f2..1008641 100644 --- a/db/src/main/java/db/JsonResult.java +++ b/db/src/main/java/db/form/JsonResult.java @@ -1,6 +1,9 @@ -package db; +package db.form; -public class JsonResult extends ToJSON{ + +import core.util.ToJSON; + +public class JsonResult extends ToJSON { private T data; @@ -8,6 +11,7 @@ public class JsonResult extends ToJSON{ private DBAction action; + public JsonResult(T data, Type type, DBAction action) { this.data = data; this.type = type; diff --git a/db/src/main/java/db/PageResult.java b/db/src/main/java/db/form/PageResult.java similarity index 89% rename from db/src/main/java/db/PageResult.java rename to db/src/main/java/db/form/PageResult.java index 4b6a6c4..1157dea 100644 --- a/db/src/main/java/db/PageResult.java +++ b/db/src/main/java/db/form/PageResult.java @@ -1,4 +1,6 @@ -package db; +package db.form; + +import db.model.AbstractModel; import java.util.List; @@ -39,6 +41,9 @@ public class PageResult { this.errorMsg = errorMsg; } + public PageResult() { + } + public long getCount() { return count; } @@ -66,4 +71,8 @@ public class PageResult { public String getErrorMsg() { return errorMsg; } + + public void setDatas(List datas) { + this.datas = datas; + } } diff --git a/db/src/main/java/db/Type.java b/db/src/main/java/db/form/Type.java similarity index 93% rename from db/src/main/java/db/Type.java rename to db/src/main/java/db/form/Type.java index ceb8a57..63d1649 100644 --- a/db/src/main/java/db/Type.java +++ b/db/src/main/java/db/form/Type.java @@ -1,4 +1,4 @@ -package db; +package db.form; import java.io.Serializable; diff --git a/db/src/main/java/db/model/AbstractModel.java b/db/src/main/java/db/model/AbstractModel.java new file mode 100644 index 0000000..5046e5e --- /dev/null +++ b/db/src/main/java/db/model/AbstractModel.java @@ -0,0 +1,34 @@ +package db.model; + +import com.alibaba.fastjson.annotation.JSONField; +import com.alibaba.fastjson.serializer.SerializerFeature; +import core.util.ToJSON; +import org.hibernate.criterion.DetachedCriteria; + +import java.io.Serializable; +import java.util.List; + +/** + * Created by reborn on 2017/8/3. + */ + +public abstract class AbstractModel extends ToJSON { + + public abstract Serializable primaryKey(); + + @Override + protected List serializerFeatures() { + List list=super.serializerFeatures(); + list.add(SerializerFeature.PrettyFormat); + return list; + } + + @JSONField(serialize = false) + public DetachedCriteria getCriteria() { + return DetachedCriteria.forClass(this.getClass()); + } + @SuppressWarnings("unchecked") + public T cast() throws ClassCastException{ + return (T) this; + } +} diff --git a/db/src/main/java/db/model/DataSourceModel.java b/db/src/main/java/db/model/DataSourceModel.java index 7e3bf83..72728bc 100644 --- a/db/src/main/java/db/model/DataSourceModel.java +++ b/db/src/main/java/db/model/DataSourceModel.java @@ -1,7 +1,5 @@ package db.model; -import db.AbstractModel; - import javax.persistence.*; import java.io.Serializable; import java.util.Objects; @@ -112,8 +110,4 @@ public class DataSourceModel extends AbstractModel { return getDbDesc(); } - @Override - public String tableNote() { - return "数据源"; - } } diff --git a/db/src/main/java/db/model/bilibili/CidEntity.java b/db/src/main/java/db/model/bilibili/CidEntity.java index 174ddbe..77b01af 100644 --- a/db/src/main/java/db/model/bilibili/CidEntity.java +++ b/db/src/main/java/db/model/bilibili/CidEntity.java @@ -1,6 +1,6 @@ package db.model.bilibili; -import db.AbstractModel; +import db.model.AbstractModel; import javax.persistence.*; import java.io.Serializable; @@ -278,9 +278,4 @@ public class CidEntity extends AbstractModel { public Serializable primaryKey() { return getCid(); } - - @Override - public String tableNote() { - return "CID表"; - } } diff --git a/db/src/main/java/db/model/bilibili/DataModel.java b/db/src/main/java/db/model/bilibili/DataModel.java index 0596400..8448235 100644 --- a/db/src/main/java/db/model/bilibili/DataModel.java +++ b/db/src/main/java/db/model/bilibili/DataModel.java @@ -1,7 +1,8 @@ package db.model.bilibili; -import db.AbstractModel; import db.annotation.BiliBili; +import db.annotation.TableInfo; +import db.model.AbstractModel; import javax.persistence.*; import java.io.Serializable; @@ -10,6 +11,7 @@ import java.util.Objects; @BiliBili @Entity @Table(name = "data", schema = "bilibili", catalog = "bilibili") +@TableInfo(comment = "历史接口") public class DataModel extends AbstractModel { private int cid; private Integer aid; @@ -41,6 +43,7 @@ public class DataModel extends AbstractModel { private Integer vp; private Integer typeId; + @Id @Column(name = "cid") public int getCid() { @@ -377,9 +380,4 @@ public class DataModel extends AbstractModel { public Serializable primaryKey() { return getCid(); } - - @Override - public String tableNote() { - return "history接口数据表"; - } } diff --git a/db/src/main/java/db/model/bilibili/MenuModel.java b/db/src/main/java/db/model/bilibili/MenuModel.java index 08e25fb..2908146 100644 --- a/db/src/main/java/db/model/bilibili/MenuModel.java +++ b/db/src/main/java/db/model/bilibili/MenuModel.java @@ -1,7 +1,7 @@ package db.model.bilibili; -import db.AbstractModel; import db.annotation.BiliBili; +import db.model.AbstractModel; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Order; import org.hibernate.criterion.Property; @@ -87,11 +87,6 @@ public class MenuModel extends AbstractModel { return getId(); } - @Override - public String tableNote() { - return "菜单表"; - } - @Basic @Column(name = "parent_id") public int getParentId() { diff --git a/db/src/main/java/db/model/bilibili/ScheduledTaskEntity.java b/db/src/main/java/db/model/bilibili/ScheduledTaskEntity.java index 4309e2d..78813b2 100644 --- a/db/src/main/java/db/model/bilibili/ScheduledTaskEntity.java +++ b/db/src/main/java/db/model/bilibili/ScheduledTaskEntity.java @@ -1,6 +1,6 @@ package db.model.bilibili; -import db.AbstractModel; +import db.model.AbstractModel; import javax.persistence.*; import java.io.Serializable; @@ -125,8 +125,4 @@ public class ScheduledTaskEntity extends AbstractModel { return getId(); } - @Override - public String tableNote() { - return "定时任务"; - } } diff --git a/db/src/main/java/db/model/bilibili/TaskEntity.java b/db/src/main/java/db/model/bilibili/TaskEntity.java index 10d0dad..6224d28 100644 --- a/db/src/main/java/db/model/bilibili/TaskEntity.java +++ b/db/src/main/java/db/model/bilibili/TaskEntity.java @@ -1,6 +1,6 @@ package db.model.bilibili; -import db.AbstractModel; +import db.model.AbstractModel; import javax.persistence.*; import java.io.Serializable; @@ -39,11 +39,6 @@ public class TaskEntity extends AbstractModel { return getId(); } - @Override - public String tableNote() { - return "脚本任务"; - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/pom-webjar.xml b/pom-webjar.xml index 77fa04b..654eff9 100644 --- a/pom-webjar.xml +++ b/pom-webjar.xml @@ -3,7 +3,7 @@ 4.0.0 org.pqh webjar - pom + jar 1.0-SNAPSHOT webjar @@ -14,6 +14,9 @@ RELEASE RELEASE RELEASE + 1.8 + UTF-8 + UTF-8 @@ -56,4 +59,23 @@ + + + + + maven-war-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index d56200a..fb70be8 100644 --- a/pom.xml +++ b/pom.xml @@ -20,12 +20,9 @@ RELEASE 6.0.6 - RELEASE RELEASE - RELEASE - RELEASE + RELEASE RELEASE - RELEASE RELEASE RELEASE @@ -51,11 +48,7 @@ ${spring.version} - - org.apache.commons - commons-dbcp2 - ${commons-dbcp2.version} - + org.hibernate hibernate-core @@ -69,15 +62,9 @@ - com.fasterxml.jackson.core - jackson-databind - ${jackson-databind.version} - - - - org.aspectj - aspectjweaver - ${aspectjweaver.version} + com.alibaba + fastjson + ${fastjson.version} @@ -98,12 +85,6 @@ ${log4j.version} - - commons-io - commons-io - ${commons-io.version} - - com.alibaba druid diff --git a/web/pom.xml b/web/pom.xml index ae1bae8..8eabbd6 100644 --- a/web/pom.xml +++ b/web/pom.xml @@ -17,7 +17,8 @@ RELEASE 1.2 1.1.2 - RELEASE + RELEASE + RELEASE @@ -27,6 +28,12 @@ 1.0-SNAPSHOT + + org.pqh + db + 1.0-SNAPSHOT + + org.springframework spring-web @@ -42,11 +49,6 @@ spring-orm - - org.apache.commons - commons-dbcp2 - - org.hibernate hibernate-core @@ -58,33 +60,40 @@ - com.fasterxml.jackson.core - jackson-databind + org.apache.logging.log4j + log4j-api - org.aspectj - aspectjweaver + org.apache.logging.log4j + log4j-core org.apache.logging.log4j - log4j-api + log4j-web - org.apache.logging.log4j - log4j-core + com.alibaba + fastjson - org.apache.logging.log4j - log4j-web + com.alibaba + druid commons-io commons-io + ${commons-io.version} + + + + org.aspectj + aspectjweaver + ${aspectjweaver.version} @@ -94,18 +103,6 @@ provided - - org.apache.httpcomponents - httpclient - ${httpclient.version} - - - - org.pqh - core - 1.0-SNAPSHOT - - jstl jstl @@ -118,21 +115,6 @@ ${standard.version} - - org.apache.thrift - libthrift - ${thrift.version} - - - httpclient - org.apache.httpcomponents - - - httpcore - org.apache.httpcomponents - - - junit diff --git a/core/src/main/java/core/aop/Advice.java b/web/src/main/java/web/aop/Advice.java similarity index 97% rename from core/src/main/java/core/aop/Advice.java rename to web/src/main/java/web/aop/Advice.java index 190a71c..f4525fa 100644 --- a/core/src/main/java/core/aop/Advice.java +++ b/web/src/main/java/web/aop/Advice.java @@ -1,4 +1,4 @@ -package core.aop; +package web.aop; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/core/src/main/java/core/aop/Advisor.java b/web/src/main/java/web/aop/Advisor.java similarity index 65% rename from core/src/main/java/core/aop/Advisor.java rename to web/src/main/java/web/aop/Advisor.java index 82a5dc9..0e149d4 100644 --- a/core/src/main/java/core/aop/Advisor.java +++ b/web/src/main/java/web/aop/Advisor.java @@ -1,12 +1,10 @@ -package core.aop; +package web.aop; -import core.service.BaseService; import org.aopalliance.aop.Advice; import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; import org.springframework.stereotype.Component; +import web.service.BaseService; -import javax.annotation.PostConstruct; -import javax.annotation.Resource; import java.lang.reflect.Method; /** @@ -14,16 +12,13 @@ import java.lang.reflect.Method; */ @Component public class Advisor extends StaticMethodMatcherPointcutAdvisor { - @Resource - private Advice advice; + + public Advisor(Advice advice) { + super(advice); + } @Override public boolean matches(Method method, Class aClass) { return BaseService.class.isAssignableFrom(aClass); } - - @PostConstruct - public void init(){ - this.setAdvice(advice); - } } diff --git a/web/src/main/java/web/config/SpringConfig.java b/web/src/main/java/web/config/SpringConfig.java index f05d043..adfb2fd 100644 --- a/web/src/main/java/web/config/SpringConfig.java +++ b/web/src/main/java/web/config/SpringConfig.java @@ -1,15 +1,16 @@ package web.config; +import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import db.config.HibernateConfig; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor; import org.springframework.web.servlet.config.annotation.*; +import java.util.Arrays; import java.util.List; /** @@ -45,10 +46,13 @@ public class SpringConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List> converters) { - Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); - builder.indentOutput(true); - converters.add(new MappingJackson2HttpMessageConverter(builder.build())); + FastJsonHttpMessageConverter fastJsonHttpMessageConverter=new FastJsonHttpMessageConverter(); + fastJsonHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8)); + converters.add(fastJsonHttpMessageConverter); } + @Override + public void extendMessageConverters(List> converters) { + } } diff --git a/web/src/main/java/web/controller/AdminController.java b/web/src/main/java/web/controller/AdminController.java index faee02c..690b1b2 100644 --- a/web/src/main/java/web/controller/AdminController.java +++ b/web/src/main/java/web/controller/AdminController.java @@ -1,6 +1,5 @@ package web.controller; -import db.DBAction; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @@ -31,8 +30,5 @@ public class AdminController extends BiliController{ return "test2"; } - @Override - public boolean checkAction(DBAction action) { - return true; - } + } diff --git a/web/src/main/java/web/controller/BaseController.java b/web/src/main/java/web/controller/BaseController.java index 8ee9a3d..bca28e6 100644 --- a/web/src/main/java/web/controller/BaseController.java +++ b/web/src/main/java/web/controller/BaseController.java @@ -1,33 +1,53 @@ package web.controller; -import core.service.BaseService; -import db.AbstractModel; -import db.DBAction; -import db.JsonResult; -import db.Type; +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.dao.DataAccessException; import org.springframework.http.MediaType; 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 web.service.BaseService; + +import java.lang.reflect.ParameterizedType; /** * Created by reborn on 2017/9/14. */ -public abstract class BaseController{ +public abstract class BaseController{ protected static Logger log=LogManager.getLogger(); protected E service; protected abstract void setService(E service); + private Class tClass; + + protected final String tableName; + + + public BaseController() { + TableInfo tableInfo=HibernateConfig.getTableNote(getCommandClass()); + if(tableInfo!=null&&StringUtils.isNotEmpty(tableInfo.comment())){ + tableName=tableInfo.comment(); + }else { + tableName=getCommandClass().getName(); + } + } + @ResponseBody - @RequestMapping(value = "{action}", produces = MediaType.APPLICATION_JSON_VALUE) - public JsonResult curd(@PathVariable DBAction action, T model) { - log.info(model.tableNote()+ "进行" + action.getCh() + "操作请求"); + @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)) { try { return new JsonResult<>(service.curd(action, model), Type.success, action); @@ -36,28 +56,44 @@ public abstract class BaseController("非法操作", Type.fail, action); } }else{ - return new JsonResult<>(model.tableNote() + "不允许" + action.getCh() + "操作", Type.fail,action); + return new JsonResult<>(tableName + "不允许" + action.getCh() + "操作", Type.fail,action); } } @ResponseBody - @RequestMapping(value = "batch/{action}", produces = MediaType.APPLICATION_JSON_VALUE) - public JsonResult find(@PathVariable DBAction action, T queryCommand,@RequestParam Integer firstResult,@RequestParam Integer maxResults){ - log.info(queryCommand.tableNote()+ "进行批量" + action.getCh() + "操作请求"); + @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)) { try { - return new JsonResult<>(service.find(queryCommand, firstResult, maxResults), Type.success, action); + + return new JsonResult<>(service.find(getDetachedCriteria(queryCommand), firstResult, maxResults), Type.success, action); } catch (DataAccessException e) { log.error(e); return new JsonResult<>("非法操作", Type.fail, action); } }else{ - return new JsonResult<>(queryCommand.tableNote() + "不允许" + action.getCh() + "操作", Type.fail,action); + return new JsonResult<>(tableName + "不允许" + action.getCh() + "操作", Type.fail,action); } } + public Class getCommandClass(){ +// log.info(ResolvableType.forField(ReflectionUtils.findField(this.getClass(),"service"))); + if(tClass==null){ + if(this.getClass().getGenericSuperclass() instanceof ParameterizedType){ + tClass=(Class) ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; + }else{ + tClass= (Class) AbstractModel.class; + } + } + return tClass; + } + + protected DetachedCriteria getDetachedCriteria(A queryCommand){ + return DetachedCriteria.forClass(getCommandClass()); + } protected abstract boolean checkAction(DBAction action); } diff --git a/web/src/main/java/web/controller/BiliController.java b/web/src/main/java/web/controller/BiliController.java index b9fccb8..a13ee26 100644 --- a/web/src/main/java/web/controller/BiliController.java +++ b/web/src/main/java/web/controller/BiliController.java @@ -1,11 +1,11 @@ package web.controller; -import db.AbstractModel; -import db.DBAction; +import db.form.DBAction; +import db.model.AbstractModel; import org.springframework.beans.factory.annotation.Autowired; import web.service.BiliService; -public class BiliController extends BaseController{ +public abstract class BiliController extends BaseController{ @Override @Autowired @@ -17,4 +17,6 @@ public class BiliController extends BaseController { +public class DataController extends TableController { + private final String regionJson=IOUtils.toString(DataController.class.getResourceAsStream("/region.json"), Charset.forName("UTF-8")); - private static JsonNode jsonNode; - - static { - try { - jsonNode = new ObjectMapper().readTree(DataController.class.getResourceAsStream("/region.json")); - } catch (IOException e) { - e.printStackTrace(); - } + public DataController() throws IOException { } - ; - @Resource - private DataService dataService; @RequestMapping - public String find(Model model, DataModel queryCommand){ - model.addAttribute("regionJson",jsonNode.toString()); - return super.find(model,queryCommand,0,10); + public String find(HttpSession session,Model model) { + session.setAttribute("regionJson",regionJson); + return super.find(model,new web.model.DataModel(),0,10); } @RequestMapping("start/{size}") public void start(@PathVariable Integer size){ stop(); - dataService.start(size); + service.start(size); } @RequestMapping("stop") public void stop(){ - dataService.stop(); + service.stop(); } @Override @@ -62,4 +56,18 @@ public class DataController extends TableController { public boolean checkAction(DBAction action) { return !EnumSet.of(DBAction.D).contains(action); } + + @Override + protected DetachedCriteria getDetachedCriteria(web.model.DataModel queryCommand) { + DetachedCriteria criteria= super.getDetachedCriteria(queryCommand); + if(StringUtils.isNotEmpty(queryCommand.getTitle())){ + criteria.add(Property.forName("title").like(queryCommand.getTitle(),MatchMode.ANYWHERE)); + } +// if(queryCommand.getTypeIds()!=null){ +// criteria.add(Property.forName("typeId").in(queryCommand.getTypeIds())); +// } + return criteria; + } + + } diff --git a/web/src/main/java/web/controller/MenuController.java b/web/src/main/java/web/controller/MenuController.java index 35971c5..faeeca0 100644 --- a/web/src/main/java/web/controller/MenuController.java +++ b/web/src/main/java/web/controller/MenuController.java @@ -1,8 +1,10 @@ package web.controller; -import db.AbstractModel; -import db.DBAction; +import db.form.DBAction; import db.model.bilibili.MenuModel; +import org.hibernate.criterion.DetachedCriteria; +import org.hibernate.criterion.Order; +import org.hibernate.criterion.Property; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @@ -13,8 +15,8 @@ import org.springframework.web.bind.annotation.RequestParam; public class MenuController extends BiliController { - @RequestMapping() - public String find(Model model, MenuModel queryCommand, @RequestParam Integer firstResult, @RequestParam Integer maxResults){ + @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()); return "menu"; } @@ -23,4 +25,9 @@ public class MenuController extends BiliController { public boolean checkAction(DBAction action) { return DBAction.R.equals(action); } + + @Override + protected DetachedCriteria getDetachedCriteria(MenuModel queryCommand) { + return super.getDetachedCriteria(queryCommand).add(Property.forName("menuLevel").eq(0)).addOrder(Order.asc("sort")); + } } diff --git a/web/src/main/java/web/controller/TableController.java b/web/src/main/java/web/controller/TableController.java index b309fcd..77f7a44 100644 --- a/web/src/main/java/web/controller/TableController.java +++ b/web/src/main/java/web/controller/TableController.java @@ -1,29 +1,32 @@ package web.controller; -import core.service.BaseService; -import db.AbstractModel; -import db.DBAction; -import db.PageResult; -import db.Type; +import db.form.DBAction; +import db.form.PageResult; +import db.form.Type; +import db.model.AbstractModel; import org.springframework.dao.DataAccessException; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import web.service.BaseService; +import javax.servlet.http.HttpServletRequest; import java.util.List; -public abstract class TableController extends BaseController { +public abstract class TableController extends BaseController { @RequestMapping("table") - public String find(Model model, T queryCommand, @RequestParam Integer firstResult, @RequestParam Integer maxResults){ + public String find(Model model,@ModelAttribute("command") A command, @RequestParam Integer firstResult, @RequestParam Integer maxResults){ DBAction action=DBAction.R; - log.info(queryCommand.tableNote()+ "进行批量" + action.getCh() + "操作请求"); + log.info(tableName+ "进行批量" + action.getCh() + "操作请求"); PageResult result; if(checkAction(action)) { try { - Long rowCount=service.rowCount(queryCommand.getClass()); + Long rowCount=service.rowCount(getDetachedCriteria(command)); if(rowCount>0) { - List list = service.find(queryCommand, firstResult*maxResults, maxResults); + List list = service.find(getDetachedCriteria(command), firstResult*maxResults, maxResults); + result=new PageResult(rowCount, firstResult,list,maxResults , Type.success); }else{ result=new PageResult(Type.fail,"没有记录"); @@ -33,13 +36,16 @@ public abstract class TableController typeIds; + + public List getTypeIds() { + return typeIds; + } + + public void setTypeIds(List typeIds) { + this.typeIds = typeIds; + } +} diff --git a/core/src/main/java/core/service/BaseService.java b/web/src/main/java/web/service/BaseService.java similarity index 62% rename from core/src/main/java/core/service/BaseService.java rename to web/src/main/java/web/service/BaseService.java index 39cfae7..be62275 100644 --- a/core/src/main/java/core/service/BaseService.java +++ b/web/src/main/java/web/service/BaseService.java @@ -1,7 +1,7 @@ -package core.service; +package web.service; -import db.AbstractModel; -import db.DBAction; +import db.form.DBAction; +import db.model.AbstractModel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hibernate.criterion.DetachedCriteria; @@ -20,14 +20,14 @@ public abstract class BaseService { protected abstract HibernateTemplate getHibernateTemplate() ; - public T curd(DBAction action, T command) throws DataAccessException { + public T curd(DBAction action, T command) throws DataAccessException { switch (action) { case C: getHibernateTemplate().save(command);break; case U: getHibernateTemplate().update(command);break; case R: - AbstractModel model=getHibernateTemplate().get(command.getClass(), command.primaryKey()); + T model=(T)(getHibernateTemplate().get(command.getClass(), command.primaryKey())); if(model!=null){ return model.cast(); } @@ -40,12 +40,14 @@ public abstract class BaseService { } - public List find(T command, int firstResult, int maxResults){ - return getHibernateTemplate().findByCriteria(command.getCriteria(),firstResult,maxResults); + public List find(DetachedCriteria criteria, int firstResult, int maxResults){ + return getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults); } - public Long rowCount(Class tClass){ - return getHibernateTemplate().executeWithNativeSession(session -> (Long) DetachedCriteria.forClass(tClass).setProjection(Projections.rowCount()).getExecutableCriteria(session).uniqueResult()); + + + public Long rowCount(DetachedCriteria criteria){ + return getHibernateTemplate().executeWithNativeSession(session -> (Long) criteria.setProjection(Projections.rowCount()).getExecutableCriteria(session).uniqueResult()); } } diff --git a/web/src/main/java/web/service/BiliService.java b/web/src/main/java/web/service/BiliService.java index d9e3909..5c05f22 100644 --- a/web/src/main/java/web/service/BiliService.java +++ b/web/src/main/java/web/service/BiliService.java @@ -1,12 +1,12 @@ package web.service; -import core.service.BaseService; import db.config.HibernateConfig; +import db.model.AbstractModel; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.stereotype.Service; @Service -public class BiliService extends BaseService { +public class BiliService extends BaseService { @Override protected HibernateTemplate getHibernateTemplate() { return HibernateConfig.get("test2"); diff --git a/web/src/main/java/web/service/DataService.java b/web/src/main/java/web/service/DataService.java index 7982c0d..b50c38d 100644 --- a/web/src/main/java/web/service/DataService.java +++ b/web/src/main/java/web/service/DataService.java @@ -1,6 +1,5 @@ package web.service; -import core.service.BaseService; import db.config.HibernateConfig; import db.model.bilibili.CidEntity; import db.model.bilibili.DataModel; diff --git a/web/src/main/java/web/util/DynamicTimer.java b/web/src/main/java/web/util/DynamicTimer.java index fab1597..d9cb182 100644 --- a/web/src/main/java/web/util/DynamicTimer.java +++ b/web/src/main/java/web/util/DynamicTimer.java @@ -2,10 +2,6 @@ package web.util; import db.model.bilibili.ScheduledTaskEntity; import db.model.bilibili.TaskEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.orm.hibernate5.HibernateTemplate; @@ -16,7 +12,6 @@ import org.springframework.scheduling.support.CronTrigger; import javax.annotation.PostConstruct; import javax.annotation.Resource; -import java.io.IOException; import java.util.Date; /** @@ -41,7 +36,7 @@ public class DynamicTimer extends ScheduledTaskRegistrar { super.scheduleTriggerTask(new TriggerTask(()->{ TaskEntity taskEntity=scheduledTaskEntity.getTaskEntity(); if(taskEntity!=null&&taskEntity.getApi()!=null&&!taskEntity.getApi().isEmpty()) { - doGet(taskEntity.getApi()); +// doGet(taskEntity.getApi()); } },(TriggerContext triggerContext)->{ CronTrigger trigger = new CronTrigger(scheduledTaskEntity.getExpression()); @@ -52,21 +47,5 @@ public class DynamicTimer extends ScheduledTaskRegistrar { } - /** - * httpclient get请求封装 - * @param href - * @return - */ - private CloseableHttpResponse doGet(String href){ - log.debug("向地址:"+href+"发送get请求"); - CloseableHttpClient closeableHttpClient = HttpClients.createDefault(); - HttpGet httpGet = new HttpGet(href); - try { - return closeableHttpClient.execute(httpGet); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } } \ No newline at end of file diff --git a/web/src/main/webapp/WEB-INF/jsp/form.jsp b/web/src/main/webapp/WEB-INF/jsp/form.jsp index 60c7645..0f683da 100644 --- a/web/src/main/webapp/WEB-INF/jsp/form.jsp +++ b/web/src/main/webapp/WEB-INF/jsp/form.jsp @@ -1,3 +1,4 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: 10295 @@ -8,15 +9,23 @@ <%@ page pageEncoding="utf-8" %> + + - - @@ -24,37 +12,36 @@ - + - - - - - - - - + - - - - +
cidaidtitlesubtitleauthor
+ - - - - - + + + + + + + + + + + + + + + + - + - -
${data.cid}${data.aid}${data.title}${data.subtitle}${data.author}cidaidtitlesubtitleauthor
${data.cid}${data.aid}${data.title}${data.subtitle}${data.author}
+ + - - -

@@ -70,14 +57,16 @@ - - + +

- + diff --git a/web/src/test/java/SpringTest.java b/web/src/test/java/SpringTest.java index e2c25e9..6feaa98 100644 --- a/web/src/test/java/SpringTest.java +++ b/web/src/test/java/SpringTest.java @@ -43,9 +43,9 @@ import java.util.List; //让测试运行于Spring环境 @RunWith(SpringJUnit4ClassRunner.class) @SpringJUnitWebConfig -@ContextConfiguration(classes = {AppConfig.class,SpringConfig.class}) +@ContextConfiguration(classes = {AppConfig.class, SpringConfig.class}) public class SpringTest { - private static Logger log = LogManager.getLogger(); + private static Logger log = LogManager.getLogger(); private MockMvc mockMvc; @@ -64,14 +64,20 @@ public class SpringTest { private HibernateTemplate baseDao1; @Before - public void setup(){ + public void setup() { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void test() throws Exception { - ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.get("/data/table").param("firstResult","0").param("maxResults","10")); + ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.get("/data/table").param("firstResult", "0").param("maxResults", "10").param("typeIds","123").param("typeIds","456")); + resultActions.andDo(MockMvcResultHandlers.print()).andReturn(); + } + + @Test + public void test2() throws Exception { + ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.get("/data/R").param("cid","9987062")); resultActions.andDo(MockMvcResultHandlers.print()).andReturn(); } diff --git a/web/src/test/java/Test.java b/web/src/test/java/Test.java index d9caf23..fffe62e 100644 --- a/web/src/test/java/Test.java +++ b/web/src/test/java/Test.java @@ -1,3 +1,4 @@ +import db.model.bilibili.DataModel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -5,6 +6,8 @@ public class Test { private static Logger log = LogManager.getLogger(); public static void main(String[] args) { - + DataModel dataModel=new DataModel(); + dataModel.setTypeId(123); + log.info("哈哈"+dataModel); } } diff --git a/web/src/test/java/db/model/TaskModel.java b/web/src/test/java/db/model/TaskModel.java index 20e0822..1e5550c 100644 --- a/web/src/test/java/db/model/TaskModel.java +++ b/web/src/test/java/db/model/TaskModel.java @@ -1,7 +1,5 @@ package db.model; -import db.AbstractModel; - import javax.persistence.*; import java.io.Serializable; import java.util.Objects; @@ -52,8 +50,4 @@ public class TaskModel extends AbstractModel { return getId(); } - @Override - public String tableNote() { - return "测试表"; - } } diff --git a/web/src/test/java/web/controller/TestController.java b/web/src/test/java/web/controller/TestController.java index 555ec73..fbed1ca 100644 --- a/web/src/test/java/web/controller/TestController.java +++ b/web/src/test/java/web/controller/TestController.java @@ -1,6 +1,6 @@ package web.controller; -import db.DBAction; +import db.form.DBAction; import db.model.TaskModel; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping; public class TestController extends BiliController{ + @Override public boolean checkAction(DBAction action) { return true;