弹幕下载

master
10295 7 years ago
parent a84cded5b1
commit 9fbb9a6e42
  1. 2
      core/src/main/java/core/thrift/ThriftClientDemo.java
  2. 0
      core/src/main/resources/TestQry.thrift
  3. 39
      db/src/main/java/db/form/PageResult.java
  4. 6
      db/src/main/java/db/model/AbstractModel.java
  5. 9
      db/src/main/java/db/model/bilibili/MenuModel.java
  6. 13
      web/src/main/java/web/aop/Advice.java
  7. 1
      web/src/main/java/web/config/AppConfig.java
  8. 4
      web/src/main/java/web/config/SpringConfig.java
  9. 23
      web/src/main/java/web/controller/DataController.java
  10. 19
      web/src/main/java/web/controller/TableController.java
  11. 2
      web/src/main/java/web/model/DataModelForm.java
  12. 33
      web/src/main/java/web/service/BaseService.java
  13. 67
      web/src/main/java/web/service/DataService.java
  14. 9
      web/src/main/webapp/WEB-INF/jsp/form.jsp
  15. 10
      web/src/main/webapp/WEB-INF/jsp/table.jsp
  16. 26
      web/src/test/java/SpringTest.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();
}

@ -19,6 +19,9 @@ public class PageResult<T extends AbstractModel> {
private String errorMsg;
public PageResult() {
}
/**
*
* @param count 总行数
@ -41,38 +44,64 @@ public class PageResult<T extends AbstractModel> {
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<T> getDatas() {
return datas;
}
public Type getType() {
return type;
public void setDatas(List<T> 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<T> datas) {
this.datas = datas;
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
}

@ -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 extends AbstractModel> T cast() throws ClassCastException{
return (T) this;

@ -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() {

@ -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));
}
}

@ -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 {

@ -50,8 +50,4 @@ public class SpringConfig implements WebMvcConfigurer {
converters.add(fastJsonHttpMessageConverter);
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
}
}

@ -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<DataModel, web.model.DataModel,DataService> {
public class DataController extends TableController<DataModel, DataModelForm,DataService> {
private final String regionJson=IOUtils.toString(DataController.class.getResourceAsStream("/region.json"), Charset.forName("UTF-8"));
@ -31,12 +34,12 @@ public class DataController extends TableController<DataModel, web.model.DataMod
@RequestMapping
public String find(HttpSession session,Model model) {
session.setAttribute("regionJson",regionJson);
return super.find(model,new web.model.DataModel(),0,10);
return super.find(model,new DataModelForm(),new PageResult(0,10));
}
@RequestMapping("start/{size}")
public void start(@PathVariable Integer size){
stop();
stop();
service.start(size);
}
@ -51,16 +54,22 @@ public class DataController extends TableController<DataModel, web.model.DataMod
}
@Override
protected DetachedCriteria getDetachedCriteria(web.model.DataModel queryCommand) {
protected DetachedCriteria getDetachedCriteria(DataModelForm 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;
}
@Override
protected List find(DataModelForm command, Integer firstResult, Integer maxResults) {
return service.find(command, firstResult, maxResults);
}
@Override
protected long rowCount(DataModelForm command) {
return service.rowCount(command);
}
}

@ -8,7 +8,6 @@ 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 java.util.List;
@ -17,17 +16,16 @@ public abstract class TableController<T extends AbstractModel,A extends T,E exte
@RequestMapping("table")
public String find(Model model,@ModelAttribute("command") A command, @RequestParam Integer firstResult, @RequestParam Integer maxResults){
public String find(Model model,@ModelAttribute("command") A command,PageResult pageResult){
DBAction action=DBAction.R;
log.info(tableName+ "进行批量" + action.getCh() + "操作请求");
PageResult result;
if(checkAction(action)) {
try {
Long rowCount=service.rowCount(getDetachedCriteria(command));
Long rowCount=rowCount(command);
if(rowCount>0) {
List list = service.find(getDetachedCriteria(command), firstResult*maxResults, maxResults);
result=new PageResult<T>(rowCount, firstResult,list,maxResults , Type.success);
List list = find(command, pageResult.getCurrentPage()*pageResult.getPageSize(), pageResult.getPageSize());
result=new PageResult<T>(rowCount, pageResult.getCurrentPage(),list,pageResult.getPageSize() , Type.success);
}else{
result=new PageResult(Type.fail,"没有记录");
}
@ -47,4 +45,13 @@ public abstract class TableController<T extends AbstractModel,A extends T,E exte
protected boolean checkAction(DBAction action) {
return false;
}
protected List find(A command, Integer firstResult,Integer maxResults){
return service.find(getDetachedCriteria(command), firstResult, maxResults);
}
protected long rowCount(A command){
return service.rowCount(getDetachedCriteria(command));
}
}

@ -2,7 +2,7 @@ package web.model;
import java.util.List;
public class DataModel extends db.model.bilibili.DataModel {
public class DataModelForm extends db.model.bilibili.DataModel {
private List<Integer> typeIds;
public List<Integer> getTypeIds() {

@ -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 extends AbstractModel> 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<getMaxResults()?maxResults:getMaxResults());
}
/**
* 限制分页查询最大显示数
* @return
*/
protected int getMaxResults(){
return 100;
}
public final Long rowCount(DetachedCriteria criteria){
/**
* 根据查询条件统计记录数
* @param criteria
* @return
*/
public Long rowCount(DetachedCriteria criteria){
return getHibernateTemplate().executeWithNativeSession(session -> (Long) criteria.setProjection(Projections.rowCount()).getExecutableCriteria(session).uniqueResult());
}
}

@ -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<Integer> 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());
}
}

@ -98,14 +98,21 @@
}).prependTo("#queryDiv");
});
<c:if test="${!empty command&&!empty command.typeIds}">
<c:forEach items="${command.typeIds}" var="typeId">
$('.dropdown-item[data-type=${typeId}]').click();
</c:forEach>
</c:if>
$('.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();
});

@ -19,11 +19,11 @@
<table class="table">
<thead>
<tr>
<th scope="col">cid</th>
<th scope="col">aid</th>
<th scope="col">title</th>
<th scope="col">subtitle</th>
<th scope="col">author</th>
<th>cid</th>
<th>aid</th>
<th>title</th>
<th>subtitle</th>
<th>author</th>
</tr>
</thead>

@ -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<Integer>(){{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();
}
}

Loading…
Cancel
Save