From 9fbb9a6e42e7521be9ebd1800b2a9fc3a5cef58e Mon Sep 17 00:00:00 2001 From: 10295 <1029559041@qq.com> Date: Sun, 3 Jun 2018 07:24:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=B9=E5=B9=95=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/core/thrift/ThriftClientDemo.java | 2 +- .../src/main/resources/TestQry.thrift | 0 db/src/main/java/db/form/PageResult.java | 39 +++++++++-- db/src/main/java/db/model/AbstractModel.java | 6 -- .../java/db/model/bilibili/MenuModel.java | 9 --- web/src/main/java/web/aop/Advice.java | 13 +--- web/src/main/java/web/config/AppConfig.java | 1 - .../main/java/web/config/SpringConfig.java | 4 -- .../java/web/controller/DataController.java | 23 +++++-- .../java/web/controller/TableController.java | 19 ++++-- .../{DataModel.java => DataModelForm.java} | 2 +- .../main/java/web/service/BaseService.java | 33 ++++++--- .../main/java/web/service/DataService.java | 67 ++++++++++++++++--- web/src/main/webapp/WEB-INF/jsp/form.jsp | 9 ++- web/src/main/webapp/WEB-INF/jsp/table.jsp | 10 +-- web/src/test/java/SpringTest.java | 26 ++++--- 16 files changed, 177 insertions(+), 86 deletions(-) rename {web => core}/src/main/resources/TestQry.thrift (100%) rename web/src/main/java/web/model/{DataModel.java => DataModelForm.java} (78%) diff --git a/core/src/main/java/core/thrift/ThriftClientDemo.java b/core/src/main/java/core/thrift/ThriftClientDemo.java index 91ab943..bd2f56b 100644 --- a/core/src/main/java/core/thrift/ThriftClientDemo.java +++ b/core/src/main/java/core/thrift/ThriftClientDemo.java @@ -21,7 +21,7 @@ public class ThriftClientDemo { } private static TTransport getTTransport() throws Exception{ try{ - TTransport tTransport = getTTransport("mikuhime.xyz", 2233, 5000); + TTransport tTransport = getTTransport("sukura.top", 2233, 5000); if(!tTransport.isOpen()){ tTransport.open(); } diff --git a/web/src/main/resources/TestQry.thrift b/core/src/main/resources/TestQry.thrift similarity index 100% rename from web/src/main/resources/TestQry.thrift rename to core/src/main/resources/TestQry.thrift diff --git a/db/src/main/java/db/form/PageResult.java b/db/src/main/java/db/form/PageResult.java index 1157dea..516ab12 100644 --- a/db/src/main/java/db/form/PageResult.java +++ b/db/src/main/java/db/form/PageResult.java @@ -19,6 +19,9 @@ public class PageResult { private String errorMsg; + public PageResult() { + } + /** * * @param count 总行数 @@ -41,38 +44,64 @@ public class PageResult { this.errorMsg = errorMsg; } - public PageResult() { + public PageResult(int currentPage, int pageSize) { + this.currentPage = currentPage; + this.pageSize = pageSize; } public long getCount() { return count; } + public void setCount(long count) { + this.count = count; + } + public int getCurrentPage() { return currentPage; } + public void setCurrentPage(int currentPage) { + this.currentPage = currentPage; + } + public List getDatas() { return datas; } - public Type getType() { - return type; + public void setDatas(List datas) { + this.datas = datas; } public int getPageSize() { return pageSize; } + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } + public long getPages() { return pages; } + public void setPages(long pages) { + this.pages = pages; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + public String getErrorMsg() { return errorMsg; } - public void setDatas(List datas) { - this.datas = datas; + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; } } diff --git a/db/src/main/java/db/model/AbstractModel.java b/db/src/main/java/db/model/AbstractModel.java index 5046e5e..4de8e8f 100644 --- a/db/src/main/java/db/model/AbstractModel.java +++ b/db/src/main/java/db/model/AbstractModel.java @@ -1,9 +1,7 @@ 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; @@ -23,10 +21,6 @@ public abstract class AbstractModel extends ToJSON { 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/bilibili/MenuModel.java b/db/src/main/java/db/model/bilibili/MenuModel.java index 0178396..5c151be 100644 --- a/db/src/main/java/db/model/bilibili/MenuModel.java +++ b/db/src/main/java/db/model/bilibili/MenuModel.java @@ -2,9 +2,6 @@ package db.model.bilibili; import db.annotation.Aliyun; import db.model.AbstractModel; -import org.hibernate.criterion.DetachedCriteria; -import org.hibernate.criterion.Order; -import org.hibernate.criterion.Property; import javax.persistence.*; import java.io.Serializable; @@ -118,12 +115,6 @@ public class MenuModel extends AbstractModel { this.menuLevel = menuLevel; } - @Override - @Transient - public DetachedCriteria getCriteria() { - return super.getCriteria().add(Property.forName("menuLevel").eq(0)).addOrder(Order.asc("sort")); - } - @Basic @Column(name = "sort") public int getSort() { diff --git a/web/src/main/java/web/aop/Advice.java b/web/src/main/java/web/aop/Advice.java index 22fd022..5b044ee 100644 --- a/web/src/main/java/web/aop/Advice.java +++ b/web/src/main/java/web/aop/Advice.java @@ -1,12 +1,9 @@ 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; @@ -20,14 +17,6 @@ public class Advice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) { - 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)); - } - - + log.info("类:" + o.getClass() + ",方法:" + method.getName() + "被调用,传入参数:" + Arrays.asList(objects)); } } diff --git a/web/src/main/java/web/config/AppConfig.java b/web/src/main/java/web/config/AppConfig.java index 47691a2..f9c2574 100644 --- a/web/src/main/java/web/config/AppConfig.java +++ b/web/src/main/java/web/config/AppConfig.java @@ -11,7 +11,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableAspectJAutoProxy(proxyTargetClass = true) @EnableTransactionManagement @PropertySource("classpath:config.properties") -//@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 d522fd3..c94f319 100644 --- a/web/src/main/java/web/config/SpringConfig.java +++ b/web/src/main/java/web/config/SpringConfig.java @@ -50,8 +50,4 @@ public class SpringConfig implements WebMvcConfigurer { converters.add(fastJsonHttpMessageConverter); } - @Override - public void extendMessageConverters(List> converters) { - - } } diff --git a/web/src/main/java/web/controller/DataController.java b/web/src/main/java/web/controller/DataController.java index 3636365..ad256db 100644 --- a/web/src/main/java/web/controller/DataController.java +++ b/web/src/main/java/web/controller/DataController.java @@ -1,6 +1,7 @@ package web.controller; import db.form.DBAction; +import db.form.PageResult; import db.model.bilibili.DataModel; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -11,16 +12,18 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import web.model.DataModelForm; import web.service.DataService; import javax.servlet.http.HttpSession; import java.io.IOException; import java.nio.charset.Charset; import java.util.EnumSet; +import java.util.List; @Controller @RequestMapping("/data") -public class DataController extends TableController { +public class DataController extends TableController { private final String regionJson=IOUtils.toString(DataController.class.getResourceAsStream("/region.json"), Charset.forName("UTF-8")); @@ -31,12 +34,12 @@ public class DataController extends TableController0) { - List list = service.find(getDetachedCriteria(command), firstResult*maxResults, maxResults); - - result=new PageResult(rowCount, firstResult,list,maxResults , Type.success); + List list = find(command, pageResult.getCurrentPage()*pageResult.getPageSize(), pageResult.getPageSize()); + result=new PageResult(rowCount, pageResult.getCurrentPage(),list,pageResult.getPageSize() , Type.success); }else{ result=new PageResult(Type.fail,"没有记录"); } @@ -47,4 +45,13 @@ public abstract class TableController typeIds; public List getTypeIds() { diff --git a/web/src/main/java/web/service/BaseService.java b/web/src/main/java/web/service/BaseService.java index be6a7c1..1949eb2 100644 --- a/web/src/main/java/web/service/BaseService.java +++ b/web/src/main/java/web/service/BaseService.java @@ -1,5 +1,7 @@ package web.service; +import db.annotation.Model; +import db.config.HibernateConfig; import db.form.DBAction; import db.model.AbstractModel; import org.apache.logging.log4j.LogManager; @@ -19,16 +21,15 @@ public abstract class BaseService{ protected static Logger log=LogManager.getLogger(); - public HibernateTemplate hibernateTemplate; + protected HibernateTemplate hibernateTemplate; - public HibernateTemplate getHibernateTemplate() { + public final HibernateTemplate getHibernateTemplate() { + if(hibernateTemplate==null) { + hibernateTemplate=HibernateConfig.get(this.getClass().getAnnotation(Model.class).value()); + } return hibernateTemplate; } - public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { - this.hibernateTemplate = hibernateTemplate; - } - public final T curd(DBAction action, T command) throws DataAccessException { switch (action) { case C: @@ -49,14 +50,26 @@ public abstract class BaseService{ } - public List find(DetachedCriteria criteria, int firstResult, int maxResults){ - return getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults); + public final List find(DetachedCriteria criteria, int firstResult,int maxResults){ + return getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults (Long) criteria.setProjection(Projections.rowCount()).getExecutableCriteria(session).uniqueResult()); } + } diff --git a/web/src/main/java/web/service/DataService.java b/web/src/main/java/web/service/DataService.java index 81442ec..e386f35 100644 --- a/web/src/main/java/web/service/DataService.java +++ b/web/src/main/java/web/service/DataService.java @@ -10,9 +10,11 @@ import org.hibernate.Session; import org.hibernate.query.NativeQuery; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; +import web.model.DataModelForm; import java.io.File; import java.io.IOException; +import java.math.BigInteger; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -21,14 +23,14 @@ import java.util.concurrent.Future; @Service @Model(HuaWeiCloud.class) -public class DataService extends BaseService{ +public class DataService extends BaseService { private ThreadPoolTaskExecutor executor; private boolean isStop; - public void start(Integer threadSize){ - isStop=false; + public void start(Integer threadSize) { + isStop = false; executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(threadSize); executor.setMaxPoolSize(threadSize); @@ -36,13 +38,13 @@ public class DataService extends BaseService{ executor.setWaitForTasksToCompleteOnShutdown(true); executor.initialize(); log.info("开始"); - new Thread(()->{ + new Thread(() -> { getCid(threadSize); }).start(); } - public void stop(){ - if(executor!=null) { + public void stop() { + if (executor != null) { executor.shutdown(); isStop = true; } @@ -63,7 +65,7 @@ public class DataService extends BaseService{ } for (; i <= 15717791; ) { - if(isStop){ + if (isStop) { log.info("停止"); break; } @@ -75,8 +77,8 @@ public class DataService extends BaseService{ cid.add(i++); } do { - Integer c=cid.poll(); - if(c!=null) { + Integer c = cid.poll(); + if (c != null) { try { executor.execute(() -> { save(c); @@ -120,10 +122,55 @@ public class DataService extends BaseService{ }); } - }catch (Exception e){ + } catch (Exception e) { log.error(e); } } + + public Long rowCount(DataModelForm form) { + StringBuffer sql=new StringBuffer(); + if(form.getTypeIds()==null) { + sql.append("select count(*) from data"); + }else{ + union(sql,count,form.getTypeIds()); + } + + if (StringUtils.isNotEmpty(form.getTitle())) { + sql.append(" where title like '%").append(form.getTitle()).append("%'"); + } + log.info(sql); + BigInteger count = getHibernateTemplate().execute(session -> (BigInteger) session.createNativeQuery(sql.toString()).getSingleResult()); + return count.longValue(); + } + + private static final String union = "union all "; + private static final String tableAlise="A"; + private static final String clazz_="1 as clazz_"; + private static final String head = "select "+tableAlise+".*,"+clazz_+" from ("; + private static final String count="select count(*) from ("; + + private void union(StringBuffer sql,String head,List ids){ + sql.append(head); + ids.forEach(id -> sql.append(union).append("select * from data_").append(id).append(" ")); + sql.replace(0, head.length() + union.length(), head); + sql.append(") ").append(tableAlise); + } + + public List find(DataModelForm form,Integer firstResult,Integer maxResults) { + StringBuffer sql = new StringBuffer(); + if (form.getTypeIds() != null && !form.getTypeIds().isEmpty()) { + union(sql,head,form.getTypeIds()); + } else { + sql.append("select *,").append(clazz_).append(" from data"); + } + + if (StringUtils.isNotEmpty(form.getTitle())) { + sql.append(" where title like '%").append(form.getTitle()).append("%'"); + } + log.info(sql); + return getHibernateTemplate().execute(session -> session.createNativeQuery(sql.toString(),DataModel.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList()); + } + } diff --git a/web/src/main/webapp/WEB-INF/jsp/form.jsp b/web/src/main/webapp/WEB-INF/jsp/form.jsp index 0f683da..79a3979 100644 --- a/web/src/main/webapp/WEB-INF/jsp/form.jsp +++ b/web/src/main/webapp/WEB-INF/jsp/form.jsp @@ -98,14 +98,21 @@ }).prependTo("#queryDiv"); }); + + + $('.dropdown-item[data-type=${typeId}]').click(); + + + $('.input-group-prepend').on('click', 'i', function () { $(this).parent().remove(); $("#type" + $(this).data("type")).remove(); + $(".dropdown-item[data-type="+$(this).data("type")+"]").show(); }); $(".btn-secondary").click(function () { - $("#pageForm").attr("action","/data/table?firstResult="+($(this).val()==''?0:$(this).val())+"&maxResults=${datas.pageSize}").submit(); + $("#pageForm").attr("action","/data/table?currentPage="+($(this).val()==''?0:$(this).val())+"&pageSize=${datas.pageSize}").submit(); }); diff --git a/web/src/main/webapp/WEB-INF/jsp/table.jsp b/web/src/main/webapp/WEB-INF/jsp/table.jsp index fd5f9a0..abc1981 100644 --- a/web/src/main/webapp/WEB-INF/jsp/table.jsp +++ b/web/src/main/webapp/WEB-INF/jsp/table.jsp @@ -19,11 +19,11 @@ - - - - - + + + + + diff --git a/web/src/test/java/SpringTest.java b/web/src/test/java/SpringTest.java index e074a73..17c6dcd 100644 --- a/web/src/test/java/SpringTest.java +++ b/web/src/test/java/SpringTest.java @@ -1,10 +1,6 @@ -import db.model.bilibili.DataModel; import db.util.ExportUtil; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.hibernate.Session; -import org.hibernate.criterion.DetachedCriteria; -import org.hibernate.criterion.Projections; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,9 +19,12 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import web.config.AppConfig; import web.config.SpringConfig; +import web.model.DataModelForm; +import web.service.BiliService; import web.service.DataService; import java.io.IOException; +import java.util.ArrayList; //让测试运行于Spring环境 @RunWith(SpringJUnit4ClassRunner.class) @@ -45,6 +44,9 @@ public class SpringTest { @Autowired private DataService dataService; + @Autowired + private BiliService biliService; + @Autowired @Qualifier("Hweb.service.DataService") private HibernateTemplate template1; @@ -75,9 +77,13 @@ public class SpringTest { @Test 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); +// log.info("记录数:"+dataService.rowCount(DetachedCriteria.forClass(DataModel.class))); + +// log.info(dataService.rowCount(null)); + DataModelForm form=new DataModelForm(); + form.setTypeIds(new ArrayList(){{add(32);add(33);}}); + long c=dataService.rowCount(form); + log.info(c); } @Test @@ -86,7 +92,11 @@ public class SpringTest { sql.exportType(); } - + @Test + public void test5(){ + dataService.getHibernateTemplate(); + biliService.getHibernateTemplate(); + } }
cidaidtitlesubtitleauthorcidaidtitlesubtitleauthor