diff --git a/db/pom.xml b/db/pom.xml index 52cf62e..6084835 100644 --- a/db/pom.xml +++ b/db/pom.xml @@ -59,6 +59,12 @@ org.apache.commons commons-lang3 + + + commons-io + commons-io + + org.pqh core diff --git a/db/src/main/java/db/annotation/BiliBili.java b/db/src/main/java/db/annotation/Aliyun.java similarity index 81% rename from db/src/main/java/db/annotation/BiliBili.java rename to db/src/main/java/db/annotation/Aliyun.java index 084693a..bc8c831 100644 --- a/db/src/main/java/db/annotation/BiliBili.java +++ b/db/src/main/java/db/annotation/Aliyun.java @@ -4,5 +4,5 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.TYPE}) -public @interface BiliBili { +public @interface Aliyun { } diff --git a/db/src/main/java/db/annotation/HuaWeiCloud.java b/db/src/main/java/db/annotation/HuaWeiCloud.java new file mode 100644 index 0000000..72e32de --- /dev/null +++ b/db/src/main/java/db/annotation/HuaWeiCloud.java @@ -0,0 +1,8 @@ +package db.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +public @interface HuaWeiCloud { +} diff --git a/db/src/main/java/db/annotation/Model.java b/db/src/main/java/db/annotation/Model.java new file mode 100644 index 0000000..39767a2 --- /dev/null +++ b/db/src/main/java/db/annotation/Model.java @@ -0,0 +1,12 @@ +package db.annotation; + +import java.lang.annotation.*; + +/** + * 实体类注解标记 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Model { + Class value(); +} diff --git a/db/src/main/java/db/annotation/TableInfo.java b/db/src/main/java/db/annotation/TableInfo.java index 3f30f28..3588426 100644 --- a/db/src/main/java/db/annotation/TableInfo.java +++ b/db/src/main/java/db/annotation/TableInfo.java @@ -7,6 +7,10 @@ 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/AddConfig.java b/db/src/main/java/db/config/AddConfig.java deleted file mode 100644 index f9f7ae2..0000000 --- a/db/src/main/java/db/config/AddConfig.java +++ /dev/null @@ -1,7 +0,0 @@ -package db.config; - -import org.springframework.orm.hibernate5.LocalSessionFactoryBean; - -public interface AddConfig { - void addConfig(LocalSessionFactoryBean bean); -} diff --git a/db/src/main/java/db/config/HibernateConfig.java b/db/src/main/java/db/config/HibernateConfig.java index a56bdbd..f957d60 100644 --- a/db/src/main/java/db/config/HibernateConfig.java +++ b/db/src/main/java/db/config/HibernateConfig.java @@ -1,14 +1,17 @@ package db.config; import com.alibaba.druid.pool.DruidDataSource; -import core.util.ClassScaner; +import db.annotation.Model; import db.annotation.TableInfo; import db.model.DataSourceModel; +import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; @@ -18,7 +21,10 @@ import javax.annotation.Resource; import javax.sql.DataSource; import java.io.IOException; import java.lang.annotation.Annotation; -import java.util.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; /** * Created by reborn on 2017/7/28. @@ -42,23 +48,27 @@ public class HibernateConfig { @Resource private DefaultListableBeanFactory beanFactory; - private static final String rootKey = "ROOT"; - - private static final String tableNote = "tableNote"; - private static final Class rootClass = DataSourceModel.class; - private static final String annotation = "db.annotation."; - private static final String basePackage = "db.model"; - private static final Map map = new HashMap<>(); + private static HibernateTemplate rootHibernateTemplate; - public static final HibernateTemplate get(String key) { - return map.get(key); + private static final Map, HibernateTemplate> map = new HashMap<>(); + + public final static HibernateTemplate getRootHibernateTemplate() { + return rootHibernateTemplate; } - public static final Map get(){ + public static final HibernateTemplate get(Class key) { + if(map.containsKey(key)){ + return map.get(key); + }else{ + throw new RuntimeException("找不到key="+key+"的HibernateTemplate对象"); + } + } + + public static final Map, HibernateTemplate> get(){ return map; } @@ -68,6 +78,13 @@ public class HibernateConfig { return tableNotes.get(c); } + /** + * 注册数据源 + * @param url + * @param username + * @param password + * @return + */ private DruidDataSource dataSource(String url, String username, String password) { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); @@ -83,6 +100,10 @@ public class HibernateConfig { return dataSource(url, dataSourceModel.getUsername(), dataSourceModel.getDbPassword()); } + /** + * 初始化根数据源 + * @return + */ private HibernateTemplate initRootDB() { SessionFactory rootdb_sessionFactory = initSessionFactory(dataSource(url, username, password), (LocalSessionFactoryBean bean) -> { bean.setAnnotatedClasses(rootClass); @@ -90,78 +111,112 @@ public class HibernateConfig { return new HibernateTemplate(rootdb_sessionFactory); } + /** + * 初始化数据源 + * @param dataSourceModel + * @return + */ private HibernateTemplate initDB(DataSourceModel dataSourceModel) { SessionFactory sessionFactory = initSessionFactory(dataSource(dataSourceModel), (LocalSessionFactoryBean bean) -> { - Set classes; - Class c; try { - c = Class.forName(annotation + dataSourceModel.getAnnotation()); + //按照规则拼接实体类注解标记全类名 + c = Class.forName( dataSourceModel.getAnnotation()); + if(!Annotation.class.isAssignableFrom(c)){ + log.error(c+"不是注解标记!!!"); + return; + } } catch (ClassNotFoundException e) { + log.error(e); return; } - 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+"不是注解!!!"); - } - - }); + //按照注解标记从指定包扫描实体类,并且注册到session工厂 + bean.setPackagesToScan(basePackage); + bean.setEntityTypeFilters(new AnnotationTypeFilter(c)); + bean.setEntityInterceptor(new MyEmptyInterceptor()); + },dataSourceModel.getDbName()); if (sessionFactory != null) { HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); hibernateTransactionManager.setSessionFactory(sessionFactory); - beanFactory.registerSingleton(DBBeanNameManager.getName(dataSourceModel.getDbDesc(),DBBeanNameManager.transactionManager),hibernateTransactionManager); + //添加事务管理器 + beanFactory.registerSingleton(DBBeanNameManager.getName(dataSourceModel.getDbId(),DBBeanNameManager.transactionManager),hibernateTransactionManager); return new HibernateTemplate(sessionFactory); } else { return null; } } + /** + * bean命名规则 + */ public enum DBBeanNameManager { - hibernateTemplate, - transactionManager; + hibernateTemplate("H"), + transactionManager("T"); - public static String getName(String key,DBBeanNameManager type){ - return type.name()+"_"+key; + private String prefix; + + DBBeanNameManager(String prefix) { + this.prefix = prefix; + } + + public static String getName(String key, DBBeanNameManager type){ + return type.prefix+key; } public static String getName(DBBeanNameManager type){ - return type.name()+"_root"; + return type.prefix+"root"; } } + /** + * 初始化数据源,session工厂 + */ @PostConstruct - private void init() { + private void init() throws ClassNotFoundException { - HibernateTemplate root = initRootDB(); - beanFactory.registerSingleton(DBBeanNameManager.getName(DBBeanNameManager.hibernateTemplate),root); - map.put(rootKey, root); - - List dataSourceModels = root.loadAll(rootClass); + //连接根数据源 + rootHibernateTemplate = initRootDB(); + beanFactory.registerSingleton(DBBeanNameManager.getName(DBBeanNameManager.hibernateTemplate),rootHibernateTemplate); + //从根数据源读取其他数据源信息,并初始化 + List dataSourceModels = rootHibernateTemplate.loadAll(rootClass); for (DataSourceModel dataSourceModel : dataSourceModels) { HibernateTemplate hibernateTemplate = initDB(dataSourceModel); if (hibernateTemplate != null) { - beanFactory.registerSingleton(DBBeanNameManager.getName(dataSourceModel.getDbDesc(),DBBeanNameManager.hibernateTemplate),hibernateTemplate); - map.put(dataSourceModel.getDbDesc(), hibernateTemplate); + //添加事务管理器 + beanFactory.registerSingleton(DBBeanNameManager.getName(dataSourceModel.getDbId(),DBBeanNameManager.hibernateTemplate),hibernateTemplate); + + + Class c=ClassUtils.getClass(dataSourceModel.getDbId()); + if(c.isAnnotationPresent(Model.class)){ + map.put(((Model) c.getAnnotation(Model.class)).value(), hibernateTemplate); + }else{ + throw new RuntimeException(dataSourceModel.getAnnotation()+"不是注解标记!!"); + } } } } - - private SessionFactory initSessionFactory(DataSource dataSource, AddConfig config) { + /** + * 初始化session工厂 + * @param dataSource + * @param config + * @return + */ + private SessionFactory initSessionFactory(DataSource dataSource, SessionFactoryConfig config,String defaultSchema) { LocalSessionFactoryBean bean = new LocalSessionFactoryBean(); Properties properties = new Properties(); properties.setProperty("hibernate.dialect", dialect); properties.setProperty("hibernate.format_sql", "true"); + if(StringUtils.isNotEmpty(defaultSchema)) { + properties.setProperty("hibernate.default_schema", defaultSchema); + } bean.setHibernateProperties(properties); bean.setDataSource(dataSource); try { - config.addConfig(bean); + config.call(bean); bean.afterPropertiesSet(); return bean.getObject(); } catch (IOException e) { @@ -170,6 +225,15 @@ public class HibernateConfig { return null; } + private SessionFactory initSessionFactory(DataSource dataSource, SessionFactoryConfig config) { + return initSessionFactory(dataSource, config,null); + } + + + /** + * 遍历表备注信息注解 + * @param c + */ private void checkClass(Class c) { if(c.isAnnotationPresent(TableInfo.class)){ TableInfo info= (TableInfo) c.getAnnotation(TableInfo.class); diff --git a/db/src/main/java/db/config/MyEmptyInterceptor.java b/db/src/main/java/db/config/MyEmptyInterceptor.java new file mode 100644 index 0000000..78514fb --- /dev/null +++ b/db/src/main/java/db/config/MyEmptyInterceptor.java @@ -0,0 +1,14 @@ +package db.config; + +import org.hibernate.EmptyInterceptor; + +public class MyEmptyInterceptor extends EmptyInterceptor { + + + + + @Override + public String onPrepareStatement(String sql) { + return super.onPrepareStatement(sql); + } +} diff --git a/db/src/main/java/db/config/SessionFactoryConfig.java b/db/src/main/java/db/config/SessionFactoryConfig.java new file mode 100644 index 0000000..076831b --- /dev/null +++ b/db/src/main/java/db/config/SessionFactoryConfig.java @@ -0,0 +1,10 @@ +package db.config; + +import org.springframework.orm.hibernate5.LocalSessionFactoryBean; + +/** + * 每个工厂有个性化配置单独封装一个回调函数 + */ +public interface SessionFactoryConfig { + void call(LocalSessionFactoryBean bean); +} diff --git a/db/src/main/java/db/model/DataSourceModel.java b/db/src/main/java/db/model/DataSourceModel.java index 72728bc..ab7c835 100644 --- a/db/src/main/java/db/model/DataSourceModel.java +++ b/db/src/main/java/db/model/DataSourceModel.java @@ -1,28 +1,28 @@ package db.model; import javax.persistence.*; -import java.io.Serializable; import java.util.Objects; @Entity -@Table(name = "datasource", schema = "rootdb", catalog = "") -public class DataSourceModel extends AbstractModel { - private String dbDesc; +@Table(name = "datasource") +public class DataSourceModel { + private String dbId; private String hostname; private int dbPort; private String username; private String dbPassword; private String dbName; private String annotation; + private String dbDesc; @Id - @Column(name = "db_desc", nullable = false, length = 10) - public String getDbDesc() { - return dbDesc; + @Column(name = "db_id", nullable = false, length = 32) + public String getDbId() { + return dbId; } - public void setDbDesc(String dbDesc) { - this.dbDesc = dbDesc; + public void setDbId(String dbId) { + this.dbId = dbId; } @Basic @@ -85,29 +85,34 @@ public class DataSourceModel extends AbstractModel { this.annotation = annotation; } + @Basic + @Column(name = "db_desc", nullable = true, length = 20) + public String getDbDesc() { + return dbDesc; + } + + public void setDbDesc(String dbDesc) { + this.dbDesc = dbDesc; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DataSourceModel that = (DataSourceModel) o; return dbPort == that.dbPort && - Objects.equals(dbDesc, that.dbDesc) && + Objects.equals(dbId, that.dbId) && Objects.equals(hostname, that.hostname) && Objects.equals(username, that.username) && Objects.equals(dbPassword, that.dbPassword) && Objects.equals(dbName, that.dbName) && - Objects.equals(annotation, that.annotation); + Objects.equals(annotation, that.annotation) && + Objects.equals(dbDesc, that.dbDesc); } @Override public int hashCode() { - return Objects.hash(dbDesc, hostname, dbPort, username, dbPassword, dbName, annotation); + return Objects.hash(dbId, hostname, dbPort, username, dbPassword, dbName, annotation, dbDesc); } - - @Override - public Serializable primaryKey() { - return getDbDesc(); - } - } diff --git a/db/src/main/java/db/model/bilibili/CidEntity.java b/db/src/main/java/db/model/bilibili/CidEntity.java index 77b01af..b2aed1a 100644 --- a/db/src/main/java/db/model/bilibili/CidEntity.java +++ b/db/src/main/java/db/model/bilibili/CidEntity.java @@ -1,5 +1,6 @@ package db.model.bilibili; +import db.annotation.Aliyun; import db.model.AbstractModel; import javax.persistence.*; @@ -7,7 +8,8 @@ import java.io.Serializable; import java.util.Objects; @Entity -@Table(name = "cid", schema = "bilibili", catalog = "bilibili") +@Table(name = "cid") +@Aliyun public class CidEntity extends AbstractModel { private int cid; private Integer maxlimit; diff --git a/db/src/main/java/db/model/bilibili/DataModel.java b/db/src/main/java/db/model/bilibili/DataModel.java index 8448235..f3b4ac9 100644 --- a/db/src/main/java/db/model/bilibili/DataModel.java +++ b/db/src/main/java/db/model/bilibili/DataModel.java @@ -1,6 +1,6 @@ package db.model.bilibili; -import db.annotation.BiliBili; +import db.annotation.HuaWeiCloud; import db.annotation.TableInfo; import db.model.AbstractModel; @@ -8,10 +8,11 @@ import javax.persistence.*; import java.io.Serializable; import java.util.Objects; -@BiliBili +@HuaWeiCloud @Entity -@Table(name = "data", schema = "bilibili", catalog = "bilibili") +@Table(name = "data" ) @TableInfo(comment = "历史接口") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class DataModel extends AbstractModel { private int cid; private Integer aid; @@ -380,4 +381,749 @@ public class DataModel extends AbstractModel { public Serializable primaryKey() { return getCid(); } + + @HuaWeiCloud + @Entity + @Table(name = "data_0") + public static class DataModel0 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_1") + public static class DataModel1 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_3") + public static class DataModel3 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_4") + public static class DataModel4 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_5") + public static class DataModel5 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_11") + public static class DataModel11 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_12") + public static class DataModel12 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_13") + public static class DataModel13 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_15") + public static class DataModel15 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_16") + public static class DataModel16 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_17") + public static class DataModel17 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_18") + public static class DataModel18 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_19") + public static class DataModel19 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_20") + public static class DataModel20 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_21") + public static class DataModel21 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_22") + public static class DataModel22 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_23") + public static class DataModel23 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_24") + public static class DataModel24 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_25") + public static class DataModel25 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_26") + public static class DataModel26 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_27") + public static class DataModel27 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_28") + public static class DataModel28 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_29") + public static class DataModel29 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_30") + public static class DataModel30 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_31") + public static class DataModel31 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_32") + public static class DataModel32 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_33") + public static class DataModel33 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_34") + public static class DataModel34 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_37") + public static class DataModel37 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_39") + public static class DataModel39 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_40") + public static class DataModel40 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_41") + public static class DataModel41 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_43") + public static class DataModel43 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_44") + public static class DataModel44 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_45") + public static class DataModel45 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_46") + public static class DataModel46 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_47") + public static class DataModel47 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_48") + public static class DataModel48 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_49") + public static class DataModel49 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_50") + public static class DataModel50 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_51") + public static class DataModel51 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_52") + public static class DataModel52 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_53") + public static class DataModel53 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_54") + public static class DataModel54 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_55") + public static class DataModel55 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_56") + public static class DataModel56 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_57") + public static class DataModel57 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_58") + public static class DataModel58 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_59") + public static class DataModel59 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_60") + public static class DataModel60 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_61") + public static class DataModel61 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_63") + public static class DataModel63 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_64") + public static class DataModel64 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_65") + public static class DataModel65 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_66") + public static class DataModel66 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_67") + public static class DataModel67 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_70") + public static class DataModel70 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_71") + public static class DataModel71 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_72") + public static class DataModel72 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_73") + public static class DataModel73 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_74") + public static class DataModel74 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_75") + public static class DataModel75 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_76") + public static class DataModel76 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_77") + public static class DataModel77 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_78") + public static class DataModel78 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_79") + public static class DataModel79 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_80") + public static class DataModel80 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_81") + public static class DataModel81 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_82") + public static class DataModel82 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_83") + public static class DataModel83 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_85") + public static class DataModel85 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_86") + public static class DataModel86 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_87") + public static class DataModel87 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_88") + public static class DataModel88 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_89") + public static class DataModel89 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_90") + public static class DataModel90 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_91") + public static class DataModel91 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_92") + public static class DataModel92 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_94") + public static class DataModel94 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_95") + public static class DataModel95 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_96") + public static class DataModel96 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_97") + public static class DataModel97 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_98") + public static class DataModel98 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_99") + public static class DataModel99 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_100") + public static class DataModel100 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_101") + public static class DataModel101 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_102") + public static class DataModel102 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_103") + public static class DataModel103 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_104") + public static class DataModel104 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_105") + public static class DataModel105 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_107") + public static class DataModel107 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_108") + public static class DataModel108 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_110") + public static class DataModel110 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_111") + public static class DataModel111 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_112") + public static class DataModel112 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_113") + public static class DataModel113 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_114") + public static class DataModel114 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_115") + public static class DataModel115 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_116") + public static class DataModel116 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_117") + public static class DataModel117 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_118") + public static class DataModel118 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_119") + public static class DataModel119 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_120") + public static class DataModel120 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_121") + public static class DataModel121 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_122") + public static class DataModel122 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_123") + public static class DataModel123 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_124") + public static class DataModel124 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_125") + public static class DataModel125 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_126") + public static class DataModel126 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_127") + public static class DataModel127 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_128") + public static class DataModel128 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_130") + public static class DataModel130 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_131") + public static class DataModel131 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_132") + public static class DataModel132 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_133") + public static class DataModel133 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_134") + public static class DataModel134 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_135") + public static class DataModel135 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_136") + public static class DataModel136 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_137") + public static class DataModel137 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_138") + public static class DataModel138 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_139") + public static class DataModel139 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_140") + public static class DataModel140 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_141") + public static class DataModel141 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_142") + public static class DataModel142 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_143") + public static class DataModel143 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_144") + public static class DataModel144 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_145") + public static class DataModel145 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_146") + public static class DataModel146 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_147") + public static class DataModel147 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_151") + public static class DataModel151 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_152") + public static class DataModel152 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_153") + public static class DataModel153 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_154") + public static class DataModel154 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_155") + public static class DataModel155 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_156") + public static class DataModel156 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_157") + public static class DataModel157 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_158") + public static class DataModel158 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_159") + public static class DataModel159 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_160") + public static class DataModel160 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_161") + public static class DataModel161 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_162") + public static class DataModel162 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_163") + public static class DataModel163 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_164") + public static class DataModel164 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_165") + public static class DataModel165 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_166") + public static class DataModel166 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_168") + public static class DataModel168 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_169") + public static class DataModel169 extends DataModel{} + + @HuaWeiCloud + @Entity + @Table(name = "data_170") + public static class DataModel170 extends DataModel{} + + + + } + diff --git a/db/src/main/java/db/model/bilibili/MenuModel.java b/db/src/main/java/db/model/bilibili/MenuModel.java index 2908146..0178396 100644 --- a/db/src/main/java/db/model/bilibili/MenuModel.java +++ b/db/src/main/java/db/model/bilibili/MenuModel.java @@ -1,6 +1,6 @@ package db.model.bilibili; -import db.annotation.BiliBili; +import db.annotation.Aliyun; import db.model.AbstractModel; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Order; @@ -11,9 +11,9 @@ import java.io.Serializable; import java.util.List; import java.util.Objects; -@BiliBili +@Aliyun @Entity -@Table(name = "menu", schema = "bilibili", catalog = "bilibili") +@Table(name = "menu") public class MenuModel extends AbstractModel { private int id; private String href; diff --git a/db/src/main/java/db/model/bilibili/ScheduledTaskEntity.java b/db/src/main/java/db/model/bilibili/ScheduledTaskEntity.java index 78813b2..f8662ff 100644 --- a/db/src/main/java/db/model/bilibili/ScheduledTaskEntity.java +++ b/db/src/main/java/db/model/bilibili/ScheduledTaskEntity.java @@ -8,17 +8,15 @@ import java.sql.Timestamp; import java.util.Objects; @Entity -@Table(name = "scheduled_task", schema = "bilibili", catalog = "bilibili") +@Table(name = "scheduled_task") public class ScheduledTaskEntity extends AbstractModel { private int id; - private int taskId; private String description; private String expression; private Timestamp nextRuntime; private Timestamp lastRuntime; private String lastReturnMsg; - private TaskEntity taskEntity; @Id @Column(name = "id") @@ -30,16 +28,6 @@ public class ScheduledTaskEntity extends AbstractModel { this.id = id; } - @Basic - @Column(name = "task_id") - public int getTaskId() { - return taskId; - } - - public void setTaskId(int taskId) { - this.taskId = taskId; - } - @Basic @Column(name = "description") public String getDescription() { @@ -90,23 +78,12 @@ public class ScheduledTaskEntity extends AbstractModel { this.lastReturnMsg = lastReturnMsg; } - @ManyToOne - @JoinColumn(name = "task_id",insertable = false,updatable = false) - public TaskEntity getTaskEntity() { - return taskEntity; - } - - public void setTaskEntity(TaskEntity taskEntity) { - this.taskEntity = taskEntity; - } - @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ScheduledTaskEntity that = (ScheduledTaskEntity) o; return id == that.id && - taskId == that.taskId && Objects.equals(description, that.description) && Objects.equals(expression, that.expression) && Objects.equals(nextRuntime, that.nextRuntime) && @@ -117,7 +94,7 @@ public class ScheduledTaskEntity extends AbstractModel { @Override public int hashCode() { - return Objects.hash(id, taskId, description, expression, nextRuntime, lastRuntime, lastReturnMsg); + return Objects.hash(id, description, expression, nextRuntime, lastRuntime, lastReturnMsg); } @Override diff --git a/db/src/main/java/db/model/bilibili/TaskEntity.java b/db/src/main/java/db/model/bilibili/TaskEntity.java deleted file mode 100644 index 6224d28..0000000 --- a/db/src/main/java/db/model/bilibili/TaskEntity.java +++ /dev/null @@ -1,56 +0,0 @@ -package db.model.bilibili; - -import db.model.AbstractModel; - -import javax.persistence.*; -import java.io.Serializable; -import java.util.Objects; - -@Entity -@Table(name = "task", schema = "bilibili", catalog = "bilibili") -public class TaskEntity extends AbstractModel { - private int id; - private String api; - - @Id - @Column(name = "id") - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - - @Basic - @Column(name = "api") - public String getApi() { - return api; - } - - public void setApi(String api) { - this.api = api; - } - - - @Override - public Serializable primaryKey() { - return getId(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - TaskEntity that = (TaskEntity) o; - return id == that.id && - Objects.equals(api, that.api); - } - - @Override - public int hashCode() { - - return Objects.hash(id, api); - } -} diff --git a/db/src/main/java/db/util/ExportUtil.java b/db/src/main/java/db/util/ExportUtil.java new file mode 100644 index 0000000..eccad3a --- /dev/null +++ b/db/src/main/java/db/util/ExportUtil.java @@ -0,0 +1,140 @@ +package db.util; + +import db.model.bilibili.CidEntity; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.criterion.DetachedCriteria; +import org.hibernate.criterion.Projections; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.schema.TargetType; +import org.springframework.orm.hibernate5.HibernateTemplate; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; + +public class ExportUtil { + + private HibernateTemplate template; + + private String fileName; + + private final List typeid; + + public ExportUtil(HibernateTemplate template, String fileName) { + this.template = template; + this.fileName = fileName; + this.typeid= (List) template.findByCriteria(DetachedCriteria.forClass(CidEntity.class).setProjection(Projections.groupProperty("typeid"))); + } + + public void exportType() throws IOException { + FileUtils.writeStringToFile(new File(fileName),StringUtils.join(typeid,"\n"),"UTF-8"); + } + + public void exportTableFromDB(Class ...c){ + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(template.getSessionFactory().getProperties()).build(); + MetadataSources metadataSources=new MetadataSources(serviceRegistry); + for(Class t:c){ + metadataSources.addAnnotatedClass(t); + } + Metadata metadata = metadataSources.buildMetadata(); + SchemaExport schemaExport = new SchemaExport(); + schemaExport.setOutputFile(fileName); + schemaExport.setFormat(true); + schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata); + } + + public void exportTable() { + List tableNames = new ArrayList<>(); + StringBuffer sb = new StringBuffer(); + + sb.append("create table data_1\n" + + "(\n" + + "\tcid int not null\n" + + "\t\tprimary key,\n" + + "\taid int null,\n" + + "\tauthor varchar(255) null,\n" + + "\tbackup_vid varchar(255) null,\n" + + "\tcache varchar(255) null,\n" + + "\tcover varchar(255) null,\n" + + "\tdispatch int null,\n" + + "\tdispatch_servers int null,\n" + + "\tdp_done tinyint null,\n" + + "\tdp_done_flv tinyint null,\n" + + "\tdp_done_hdmp4 tinyint null,\n" + + "\tdp_done_mp4 tinyint null,\n" + + "\tduration double null,\n" + + "\tfiles int null,\n" + + "\tletv_addr varchar(255) null,\n" + + "\tletv_vid int null,\n" + + "\tletv_vu varchar(255) null,\n" + + "\tmid int null,\n" + + "\tpage int null,\n" + + "\tstorage int null,\n" + + "\tstorage_server int null,\n" + + "\tsubtitle varchar(255) null,\n" + + "\ttitle varchar(255) null,\n" + + "\ttype varchar(255) null,\n" + + "\tupload int null,\n" + + "\tupload_meta int null,\n" + + "\tvid varchar(255) null,\n" + + "\tvp int null,\n" + + "\ttype_id int null\n" + + ")\n" + + "engine=MyISAM\n" + + ";\n" + + "\n"); + + for (Integer id : typeid) { + String tableName = "data_" + id; + sb.append("create table " + tableName + " like data_1;\n"); + tableNames.add(tableName); + } + + sb.append("create table data (\n" + + " cid integer not null,\n" + + " aid integer,\n" + + " author varchar(255),\n" + + " backup_vid varchar(255),\n" + + " cache varchar(255),\n" + + " cover varchar(255),\n" + + " dispatch integer,\n" + + " dispatch_servers integer,\n" + + " dp_done tinyint,\n" + + " dp_done_flv tinyint,\n" + + " dp_done_hdmp4 tinyint,\n" + + " dp_done_mp4 tinyint,\n" + + " duration double precision,\n" + + " files integer,\n" + + " letv_addr varchar(255),\n" + + " letv_vid integer,\n" + + " letv_vu varchar(255),\n" + + " mid integer,\n" + + " page integer,\n" + + " storage integer,\n" + + " storage_server integer,\n" + + " subtitle varchar(255),\n" + + " title varchar(255),\n" + + " type varchar(255),\n" + + " upload integer,\n" + + " upload_meta integer,\n" + + " vid varchar(255),\n" + + " vp integer,\n" + + " type_id integer,\n" + + " primary key (cid)\n" + + " ) engine=MERGE UNION=(" + StringUtils.join(tableNames, ",") + ") insert_method=no;"); + + + try { + FileUtils.writeStringToFile(new File(fileName), sb.toString(), "utf-8", true); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/pom.xml b/pom.xml index fb70be8..6773dbb 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ RELEASE RELEASE RELEASE + RELEASE @@ -97,6 +98,12 @@ ${lang3.version} + + commons-io + commons-io + ${commons-io.version} + + diff --git a/web/pom.xml b/web/pom.xml index 8eabbd6..c5c8577 100644 --- a/web/pom.xml +++ b/web/pom.xml @@ -18,7 +18,7 @@ 1.2 1.1.2 RELEASE - RELEASE + @@ -85,9 +85,8 @@ - commons-io - commons-io - ${commons-io.version} + com.alibaba + druid diff --git a/web/src/main/java/web/aop/Advice.java b/web/src/main/java/web/aop/Advice.java index f4525fa..22fd022 100644 --- a/web/src/main/java/web/aop/Advice.java +++ b/web/src/main/java/web/aop/Advice.java @@ -1,9 +1,12 @@ package web.aop; +import db.annotation.Model; +import db.config.HibernateConfig; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.stereotype.Component; +import web.service.BaseService; import java.lang.reflect.Method; import java.util.Arrays; @@ -13,9 +16,18 @@ import java.util.Arrays; */ @Component public class Advice implements MethodBeforeAdvice { - private static Logger log=LogManager.getLogger(); + private static Logger log = LogManager.getLogger(); + @Override public void before(Method method, Object[] objects, Object o) { - log.info("类:" + o.getClass() + ",方法:" + method.getName() + "被调用,传入参数:" + Arrays.asList(objects)); + log.info(method.getName()); + if ("getHibernateTemplate".equals(method.getName())) { + BaseService baseService = (BaseService) o; + baseService.setHibernateTemplate(HibernateConfig.get(o.getClass().getAnnotation(Model.class).value())); + } else { + log.info("类:" + o.getClass() + ",方法:" + method.getName() + "被调用,传入参数:" + Arrays.asList(objects)); + } + + } } diff --git a/web/src/main/java/web/aop/Advisor.java b/web/src/main/java/web/aop/Advisor.java index 0e149d4..5138b1c 100644 --- a/web/src/main/java/web/aop/Advisor.java +++ b/web/src/main/java/web/aop/Advisor.java @@ -2,6 +2,7 @@ package web.aop; import org.aopalliance.aop.Advice; import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import web.service.BaseService; @@ -13,6 +14,7 @@ import java.lang.reflect.Method; @Component public class Advisor extends StaticMethodMatcherPointcutAdvisor { + @Autowired public Advisor(Advice advice) { super(advice); } diff --git a/web/src/main/java/web/config/AppConfig.java b/web/src/main/java/web/config/AppConfig.java index b121dc6..47691a2 100644 --- a/web/src/main/java/web/config/AppConfig.java +++ b/web/src/main/java/web/config/AppConfig.java @@ -1,14 +1,17 @@ package web.config; import db.config.HibernateConfig; -import org.springframework.context.annotation.*; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableAspectJAutoProxy(proxyTargetClass = true) @EnableTransactionManagement @PropertySource("classpath:config.properties") -@ComponentScan({"core"}) +//@ComponentScan({"db.config"}) @Import(HibernateConfig.class) public class AppConfig { diff --git a/web/src/main/java/web/config/SpringConfig.java b/web/src/main/java/web/config/SpringConfig.java index adfb2fd..d522fd3 100644 --- a/web/src/main/java/web/config/SpringConfig.java +++ b/web/src/main/java/web/config/SpringConfig.java @@ -19,8 +19,7 @@ import java.util.List; @Configuration @EnableWebMvc @PropertySource({"classpath:config.properties"}) -@ComponentScan({"web.controller","web.service","web.util"}) - +@ComponentScan({"web.controller","web.service","web.util","web.aop"}) public class SpringConfig implements WebMvcConfigurer { @Override diff --git a/web/src/main/java/web/controller/AdminController.java b/web/src/main/java/web/controller/AdminController.java index 690b1b2..79f1e82 100644 --- a/web/src/main/java/web/controller/AdminController.java +++ b/web/src/main/java/web/controller/AdminController.java @@ -31,4 +31,5 @@ public class AdminController extends BiliController{ } + } diff --git a/web/src/main/java/web/controller/BaseController.java b/web/src/main/java/web/controller/BaseController.java index bca28e6..c3cfa5a 100644 --- a/web/src/main/java/web/controller/BaseController.java +++ b/web/src/main/java/web/controller/BaseController.java @@ -10,6 +10,7 @@ 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.dao.DataAccessException; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; @@ -26,10 +27,9 @@ import java.lang.reflect.ParameterizedType; public abstract class BaseController{ protected static Logger log=LogManager.getLogger(); + @Autowired protected E service; - protected abstract void setService(E service); - private Class tClass; protected final String tableName; diff --git a/web/src/main/java/web/controller/BiliController.java b/web/src/main/java/web/controller/BiliController.java index a13ee26..6e647f4 100644 --- a/web/src/main/java/web/controller/BiliController.java +++ b/web/src/main/java/web/controller/BiliController.java @@ -2,21 +2,12 @@ package web.controller; import db.form.DBAction; import db.model.AbstractModel; -import org.springframework.beans.factory.annotation.Autowired; import web.service.BiliService; public abstract class BiliController extends BaseController{ - @Override - @Autowired - protected void setService(BiliService service) { - this.service=service; - } - @Override protected boolean checkAction(DBAction action) { return false; } - - } diff --git a/web/src/main/java/web/controller/DataController.java b/web/src/main/java/web/controller/DataController.java index a08b28c..3636365 100644 --- a/web/src/main/java/web/controller/DataController.java +++ b/web/src/main/java/web/controller/DataController.java @@ -7,7 +7,6 @@ import org.apache.commons.lang3.StringUtils; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Property; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; @@ -46,12 +45,6 @@ public class DataController extends TableController extends BaseController { + @RequestMapping("table") public String find(Model model,@ModelAttribute("command") A command, @RequestParam Integer firstResult, @RequestParam Integer maxResults){ DBAction action=DBAction.R; @@ -43,10 +43,6 @@ public abstract class TableController T curd(DBAction action, T command) throws DataAccessException { + public final T curd(DBAction action, T command) throws DataAccessException { switch (action) { case C: getHibernateTemplate().save(command);break; @@ -46,7 +55,7 @@ public abstract class BaseService { - public Long rowCount(DetachedCriteria criteria){ + public final 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 5c05f22..14012f7 100644 --- a/web/src/main/java/web/service/BiliService.java +++ b/web/src/main/java/web/service/BiliService.java @@ -1,14 +1,12 @@ package web.service; -import db.config.HibernateConfig; +import db.annotation.Aliyun; +import db.annotation.Model; import db.model.AbstractModel; -import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.stereotype.Service; @Service -public class BiliService extends BaseService { - @Override - protected HibernateTemplate getHibernateTemplate() { - return HibernateConfig.get("test2"); - } +@Model(Aliyun.class) +public class BiliService extends BaseService{ + } diff --git a/web/src/main/java/web/service/DataService.java b/web/src/main/java/web/service/DataService.java index b50c38d..81442ec 100644 --- a/web/src/main/java/web/service/DataService.java +++ b/web/src/main/java/web/service/DataService.java @@ -1,13 +1,13 @@ package web.service; -import db.config.HibernateConfig; +import db.annotation.HuaWeiCloud; +import db.annotation.Model; import db.model.bilibili.CidEntity; import db.model.bilibili.DataModel; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.hibernate.Session; import org.hibernate.query.NativeQuery; -import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; @@ -20,17 +20,13 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.Future; @Service -public class DataService extends BaseService { +@Model(HuaWeiCloud.class) +public class DataService extends BaseService{ private ThreadPoolTaskExecutor executor; private boolean isStop; - @Override - protected HibernateTemplate getHibernateTemplate() { - return HibernateConfig.get("test1"); - } - public void start(Integer threadSize){ isStop=false; executor = new ThreadPoolTaskExecutor(); diff --git a/web/src/main/java/web/util/DynamicTimer.java b/web/src/main/java/web/util/DynamicTimer.java index d9cb182..1fd7843 100644 --- a/web/src/main/java/web/util/DynamicTimer.java +++ b/web/src/main/java/web/util/DynamicTimer.java @@ -1,7 +1,6 @@ package web.util; import db.model.bilibili.ScheduledTaskEntity; -import db.model.bilibili.TaskEntity; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.orm.hibernate5.HibernateTemplate; @@ -34,10 +33,7 @@ public class DynamicTimer extends ScheduledTaskRegistrar { public void addTriggerTask(ScheduledTaskEntity scheduledTaskEntity) { super.scheduleTriggerTask(new TriggerTask(()->{ - TaskEntity taskEntity=scheduledTaskEntity.getTaskEntity(); - if(taskEntity!=null&&taskEntity.getApi()!=null&&!taskEntity.getApi().isEmpty()) { -// doGet(taskEntity.getApi()); - } + },(TriggerContext triggerContext)->{ CronTrigger trigger = new CronTrigger(scheduledTaskEntity.getExpression()); log.debug(triggerContext+""); diff --git a/web/src/test/java/SpringTest.java b/web/src/test/java/SpringTest.java index 6feaa98..e074a73 100644 --- a/web/src/test/java/SpringTest.java +++ b/web/src/test/java/SpringTest.java @@ -1,18 +1,10 @@ -import db.model.bilibili.CidEntity; import db.model.bilibili.DataModel; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.StringUtils; +import db.util.ExportUtil; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hibernate.Session; -import org.hibernate.boot.Metadata; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Projections; -import org.hibernate.service.ServiceRegistry; -import org.hibernate.tool.hbm2ddl.SchemaExport; -import org.hibernate.tool.schema.TargetType; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -33,12 +25,7 @@ import web.config.AppConfig; import web.config.SpringConfig; import web.service.DataService; -import javax.annotation.Resource; -import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.EnumSet; -import java.util.List; //让测试运行于Spring环境 @RunWith(SpringJUnit4ClassRunner.class) @@ -52,16 +39,19 @@ public class SpringTest { @Autowired WebApplicationContext wac; - @Resource + @Autowired private DefaultListableBeanFactory beanFactory; - @Resource + @Autowired private DataService dataService; + @Autowired + @Qualifier("Hweb.service.DataService") + private HibernateTemplate template1; @Autowired - @Qualifier("hibernateTemplate_test1") - private HibernateTemplate baseDao1; + @Qualifier("Hweb.service.BiliService") + private HibernateTemplate template2; @Before public void setup() { @@ -81,73 +71,22 @@ public class SpringTest { resultActions.andDo(MockMvcResultHandlers.print()).andReturn(); } - // @Test - public void exportTableSql() { - - ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(baseDao1.getSessionFactory().getProperties()).build(); - Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(DataModel.class).buildMetadata(); - SchemaExport schemaExport = new SchemaExport(); - schemaExport.setOutputFile("test.sql"); - schemaExport.setFormat(true); - schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata); - - - List typeid = (List) baseDao1.findByCriteria(DetachedCriteria.forClass(CidEntity.class).setProjection(Projections.groupProperty("typeid"))); - - List tableNames = new ArrayList<>(); - StringBuffer sb = new StringBuffer(); - for (Integer id : typeid) { - String tableName = "data_" + id; - sb.append("create table " + tableName + " like data_1;\n"); - tableNames.add(tableName); - } - - sb.append("create table data (\n" + - " cid integer not null,\n" + - " aid integer,\n" + - " author varchar(255),\n" + - " backup_vid varchar(255),\n" + - " cache varchar(255),\n" + - " cover varchar(255),\n" + - " dispatch integer,\n" + - " dispatch_servers integer,\n" + - " dp_done tinyint,\n" + - " dp_done_flv tinyint,\n" + - " dp_done_hdmp4 tinyint,\n" + - " dp_done_mp4 tinyint,\n" + - " duration double precision,\n" + - " files integer,\n" + - " letv_addr varchar(255),\n" + - " letv_vid integer,\n" + - " letv_vu varchar(255),\n" + - " mid integer,\n" + - " page integer,\n" + - " storage integer,\n" + - " storage_server integer,\n" + - " subtitle varchar(255),\n" + - " title varchar(255),\n" + - " type varchar(255),\n" + - " upload integer,\n" + - " upload_meta integer,\n" + - " vid varchar(255),\n" + - " vp integer,\n" + - " type_id integer,\n" + - " primary key (cid)\n" + - " ) engine=MERGE UNION=(" + StringUtils.join(tableNames, ",") + ") insert_method=no;"); - - - try { - FileUtils.writeStringToFile(new File("test.sql"), sb.toString(), "utf-8", true); - } catch (IOException e) { - e.printStackTrace(); - } - } @Test - public void test4() throws IOException { + public void test3() { // 查询总条数 + Class c=DataModel.DataModel1.class; + Long count = template1.executeWithNativeSession((Session session) -> (Long) DetachedCriteria.forClass(c).setProjection(Projections.rowCount()).getExecutableCriteria(session).uniqueResult()); + log.info("记录数:"+count); + } - Long count = baseDao1.executeWithNativeSession((Session session) -> (Long) DetachedCriteria.forClass(DataModel.class).setProjection(Projections.rowCount()).getExecutableCriteria(session).uniqueResult()); - log.info(count); + @Test + public void test4() throws IOException { + ExportUtil sql=new ExportUtil(template2,"E:\\Projects\\JAVA\\WebCrawler\\init\\typeid"); + sql.exportType(); } + + + + } diff --git a/web/src/test/java/Test.java b/web/src/test/java/Test.java index fffe62e..2b91644 100644 --- a/web/src/test/java/Test.java +++ b/web/src/test/java/Test.java @@ -1,13 +1,14 @@ -import db.model.bilibili.DataModel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import web.service.BaseService; +import web.service.BiliService; + +import java.io.IOException; 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); + public static void main(String[] args) throws IOException { + log.info(org.springframework.util.ClassUtils.isAssignable(BiliService.class,BaseService.class)); } } diff --git a/web/src/test/java/db/model/TaskModel.java b/web/src/test/java/db/model/TaskModel.java index 1e5550c..98d3be5 100644 --- a/web/src/test/java/db/model/TaskModel.java +++ b/web/src/test/java/db/model/TaskModel.java @@ -5,7 +5,7 @@ import java.io.Serializable; import java.util.Objects; @Entity -@Table(name = "task", schema = "bilibili", catalog = "bilibili") +@Table(name = "task") public class TaskModel extends AbstractModel { private int id; private String api; diff --git a/web/src/test/java/web/controller/TestController.java b/web/src/test/java/web/controller/TestController.java index fbed1ca..17a82b9 100644 --- a/web/src/test/java/web/controller/TestController.java +++ b/web/src/test/java/web/controller/TestController.java @@ -9,8 +9,6 @@ import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/test") public class TestController extends BiliController{ - - @Override public boolean checkAction(DBAction action) { return true;