添加bootstrap后台模板

master
10295 7 years ago
parent b52b93db5a
commit 41d46398c3
  1. 3
      core/src/main/java/core/aop/Advisor.java
  2. 16
      core/src/main/java/core/service/BaseService.java
  3. 5
      db/src/main/java/db/config/HibernateConfig.java
  4. 30
      db/src/main/java/db/config/MyEmptyInterceptor.java
  5. 3
      web/src/main/java/web/config/SpringConfig.java
  6. 2
      web/src/main/java/web/controller/BaseController.java
  7. 28
      web/src/main/java/web/controller/DataController.java
  8. 126
      web/src/main/java/web/service/DataService.java
  9. 78
      web/src/test/java/SpringTest.java

@ -1,6 +1,7 @@
package core.aop; package core.aop;
import core.dao.BaseDao1; import core.dao.BaseDao1;
import core.dao.BaseDao2;
import org.aopalliance.aop.Advice; import org.aopalliance.aop.Advice;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -19,7 +20,7 @@ public class Advisor extends StaticMethodMatcherPointcutAdvisor {
@Override @Override
public boolean matches(Method method, Class<?> aClass) { public boolean matches(Method method, Class<?> aClass) {
return BaseDao1.class.isAssignableFrom(aClass); return BaseDao1.class.isAssignableFrom(aClass)||BaseDao2.class.isAssignableFrom(aClass);
} }
@PostConstruct @PostConstruct

@ -1,6 +1,7 @@
package core.service; package core.service;
import core.dao.BaseDao1; import core.dao.BaseDao1;
import core.dao.BaseDao2;
import core.util.DBAction; import core.util.DBAction;
import db.model.AbstractModel; import db.model.AbstractModel;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -18,7 +19,10 @@ public class BaseService {
protected Logger log=LogManager.getLogger(); protected Logger log=LogManager.getLogger();
@Resource @Resource
private BaseDao1 baseDao; protected BaseDao1 baseDao1;
@Resource
protected BaseDao2 baseDao2;
public<T extends AbstractModel> String curd(DBAction action, T command) { public<T extends AbstractModel> String curd(DBAction action, T command) {
Class<T> tClass = (Class<T>) command.getClass(); Class<T> tClass = (Class<T>) command.getClass();
@ -26,21 +30,21 @@ public class BaseService {
switch (action) { switch (action) {
case C: case C:
case U: case U:
baseDao.saveOrUpdate(command); baseDao1.saveOrUpdate(command);
break; break;
case R: case R:
if(command.primaryKey()!=null) { if(command.primaryKey()!=null) {
T model = baseDao.get(tClass, command.primaryKey()); T model = baseDao1.get(tClass, command.primaryKey());
return model == null ? "找不到主键=" + command.primaryKey() + "" : model.toString(); return model == null ? "找不到主键=" + command.primaryKey() + "" : model.toString();
}else{ }else{
throw new RuntimeException(command.getClass()+".primaryKey()方法返回null"); throw new RuntimeException(command.getClass()+".primaryKey()方法返回null");
} }
case D: case D:
if(command.getCheckeds()==null) { if(command.getCheckeds()==null) {
baseDao.delete(tClass, command.primaryKey()); baseDao1.delete(tClass, command.primaryKey());
}else{ }else{
for (Serializable checked:command.getCheckeds()){ for (Serializable checked:command.getCheckeds()){
baseDao.delete(tClass,checked); baseDao1.delete(tClass,checked);
} }
} }
break; break;
@ -53,6 +57,4 @@ public class BaseService {
} }
} }

@ -6,10 +6,8 @@ import org.apache.logging.log4j.Logger;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -51,8 +49,6 @@ public class HibernateConfig{
private LocalSessionFactoryBean huaWeiCloud = new LocalSessionFactoryBean(); private LocalSessionFactoryBean huaWeiCloud = new LocalSessionFactoryBean();
private MyEmptyInterceptor interceptor =new MyEmptyInterceptor();
private DruidDataSource dataSource(String url,String username,String password){ private DruidDataSource dataSource(String url,String username,String password){
DruidDataSource dataSource=new DruidDataSource(); DruidDataSource dataSource=new DruidDataSource();
dataSource.setUrl(url); dataSource.setUrl(url);
@ -78,7 +74,6 @@ public class HibernateConfig{
bean.setHibernateProperties(properties); bean.setHibernateProperties(properties);
bean.setDataSource(dataSource); bean.setDataSource(dataSource);
bean.setPackagesToScan(packagesToScan); bean.setPackagesToScan(packagesToScan);
bean.setEntityInterceptor(interceptor);
try { try {
bean.afterPropertiesSet(); bean.afterPropertiesSet();
} catch (IOException e) { } catch (IOException e) {

@ -1,30 +0,0 @@
package db.config;
import db.model.DataModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import java.io.Serializable;
public class MyEmptyInterceptor extends EmptyInterceptor {
private Logger log=LogManager.getLogger();
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if(entity instanceof DataModel){
DataModel dataModel= (DataModel) entity;
log.info(dataModel);
}
return true;
}
@Override
public String onPrepareStatement(String sql) {
// log.info(sql);
return super.onPrepareStatement(sql);
}
}

@ -3,7 +3,6 @@ package web.config;
import core.dao.BaseDao1; import core.dao.BaseDao1;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor; import org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -19,7 +18,7 @@ import javax.annotation.Resource;
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
@PropertySource("classpath:config.properties") @PropertySource("classpath:config.properties")
@ComponentScan({"web.controller"}) @ComponentScan({"web.controller","web.service"})
public class SpringConfig implements WebMvcConfigurer { public class SpringConfig implements WebMvcConfigurer {

@ -18,7 +18,7 @@ public class BaseController<T extends AbstractModel>{
protected Logger log=LogManager.getLogger(); protected Logger log=LogManager.getLogger();
@Resource @Resource
protected BaseService baseService; public BaseService baseService;
@ResponseBody @ResponseBody
@RequestMapping(value = "{action}", produces = "application/json;charset=UTF-8") @RequestMapping(value = "{action}", produces = "application/json;charset=UTF-8")

@ -0,0 +1,28 @@
package web.controller;
import db.model.DataModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import web.service.DataService;
import javax.annotation.Resource;
@Controller
@RequestMapping("/data")
public class DataController extends BaseController<DataModel> {
@Resource
private DataService dataService;
@RequestMapping("start/{size}")
public void start(@PathVariable Integer size){
stop();
dataService.start(size);
}
@RequestMapping("stop")
public void stop(){
dataService.stop();
}
}

@ -0,0 +1,126 @@
package web.service;
import core.service.BaseService;
import db.model.CidEntity;
import db.model.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.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Future;
@Service
public class DataService extends BaseService {
private ThreadPoolTaskExecutor executor;
private boolean isStop;
public void start(Integer threadSize){
isStop=false;
executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(threadSize);
executor.setMaxPoolSize(threadSize);
executor.setQueueCapacity(0);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
log.info("开始");
new Thread(()->{
getCid(threadSize);
}).start();
}
public void stop(){
if(executor!=null) {
executor.shutdown();
isStop = true;
}
}
private void getCid(Integer threadSize) {
File file = new File("result");
int i = 0;
try {
String s = FileUtils.readFileToString(file, "UTF-8");
if (StringUtils.isNumeric(s)) {
i = Integer.parseInt(s);
}
} catch (IOException e) {
log.error(e);
}
for (; i <= 15717791; ) {
if(isStop){
log.info("停止");
break;
}
List<Future<Boolean>> tasks = new ArrayList<>();
LinkedList<Integer> cid = new LinkedList<>();
ConcurrentLinkedQueue<Integer> success = new ConcurrentLinkedQueue<>();
for (int j = 0; j < threadSize; j++) {
cid.add(i++);
}
do {
Integer c=cid.poll();
if(c!=null) {
try {
executor.execute(() -> {
save(c);
success.add(c);
});
} catch (Exception e) {
cid.add(c);
}
}
} while (success.size() < threadSize);
tasks.clear();
cid.clear();
success.clear();
try {
FileUtils.writeStringToFile(file, i + "", "UTF-8", false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void save(Integer id) {
try {
DataModel dataModel = baseDao1.get(DataModel.class, id);
CidEntity cidEntity = baseDao1.get(CidEntity.class, id);
if (dataModel != null && cidEntity != null && cidEntity.getTypeid() != null) {
dataModel.setTypeId(cidEntity.getTypeid());
baseDao2.executeWithNativeSession((Session session) -> {
session.beginTransaction();
int i = 1;
String sql = "insert into data_" + cidEntity.getTypeid() + " (aid, author, backup_vid, cache, cover, dispatch, dispatch_servers, dp_done, dp_done_flv, dp_done_hdmp4, dp_done_mp4, duration, files, letv_addr, letv_vid, letv_vu, mid, page, storage, storage_server, subtitle, title, type, type_id, upload, upload_meta, vid, vp, cid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
NativeQuery query = session.createNativeQuery(sql)
.setParameter(i++, dataModel.getAid()).setParameter(i++, dataModel.getAuthor()).setParameter(i++, dataModel.getBackupVid()).setParameter(i++, dataModel.getCache()).setParameter(i++, dataModel.getCover())
.setParameter(i++, dataModel.getDispatch()).setParameter(i++, dataModel.getDispatchServers()).setParameter(i++, dataModel.getDpDone()).setParameter(i++, dataModel.getDpDoneFlv()).setParameter(i++, dataModel.getDpDoneHdmp4()).setParameter(i++, dataModel.getDpDoneMp4())
.setParameter(i++, dataModel.getDuration()).setParameter(i++, dataModel.getFiles()).setParameter(i++, dataModel.getLetvAddr()).setParameter(i++, dataModel.getLetvVid()).setParameter(i++, dataModel.getLetvVu()).setParameter(i++, dataModel.getMid())
.setParameter(i++, dataModel.getPage()).setParameter(i++, dataModel.getStorage()).setParameter(i++, dataModel.getStorageServer()).setParameter(i++, dataModel.getSubtitle()).setParameter(i++, dataModel.getTitle()).setParameter(i++, dataModel.getType())
.setParameter(i++, dataModel.getTypeId()).setParameter(i++, dataModel.getUpload()).setParameter(i++, dataModel.getUploadMeta()).setParameter(i++, dataModel.getVid()).setParameter(i++, dataModel.getVp()).setParameter(i++, dataModel.getCid());
return query.executeUpdate();
});
}
}catch (Exception e){
log.error(e);
}
}
}

@ -1,8 +1,6 @@
import core.dao.BaseDao1; import core.dao.BaseDao1;
import core.dao.BaseDao2;
import db.model.CidEntity; import db.model.CidEntity;
import db.model.DataModel; import db.model.DataModel;
import db.model.TaskEntity;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -13,18 +11,13 @@ import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections; import org.hibernate.criterion.Projections;
import org.hibernate.metamodel.internal.MetamodelImpl;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType; import org.hibernate.tool.schema.TargetType;
import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.dao.DataAccessException;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import web.config.AppConfig; import web.config.AppConfig;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -39,53 +32,14 @@ import java.util.List;
@SpringJUnitWebConfig @SpringJUnitWebConfig
@ContextConfiguration(classes = {AppConfig.class}) @ContextConfiguration(classes = {AppConfig.class})
public class SpringTest { public class SpringTest {
private Logger log=LogManager.getLogger(); private Logger log = LogManager.getLogger();
@Resource @Resource
private BaseDao1 baseDao1; private BaseDao1 baseDao1;
@Resource
private BaseDao2 baseDao2;
@Test
public void factory(){
MetamodelImpl metamodel= (MetamodelImpl) baseDao1.getSessionFactory().getMetamodel();
}
@Test
@Transactional("transactionManager2")
@Rollback(false)
public void test1(){
DataModel dataModel=new DataModel();
dataModel.setCid(333);
TaskEntity taskEntity=new TaskEntity();
taskEntity.setApi("fdsfsdf");
baseDao2.save(dataModel);
}
@Test
@Transactional("transactionManager2")
@Rollback(false)
public void test(){
// for (int id=1;id<10;id++){
int id=103;
DataModel dataModel=baseDao1.get(DataModel.class,id);
CidEntity cidEntity=baseDao1.get(CidEntity.class,id);
if(dataModel!=null&&cidEntity!=null){
dataModel.setTypeId(cidEntity.getTypeid());
try {
log.info(baseDao2.save(dataModel));
}catch (DataAccessException e){
log.error(e);
}
}
// @Test
// } public void exportTableSql() {
}
// @Test
public void exportTableSql(){
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(baseDao1.getSessionFactory().getProperties()).build(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(baseDao1.getSessionFactory().getProperties()).build();
Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(DataModel.class).buildMetadata(); Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(DataModel.class).buildMetadata();
SchemaExport schemaExport = new SchemaExport(); SchemaExport schemaExport = new SchemaExport();
@ -94,13 +48,13 @@ public class SpringTest {
schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata); schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata);
List<Integer> typeid= (List<Integer>) baseDao1.findByCriteria(DetachedCriteria.forClass(CidEntity.class).setProjection(Projections.groupProperty("typeid"))); List<Integer> typeid = (List<Integer>) baseDao1.findByCriteria(DetachedCriteria.forClass(CidEntity.class).setProjection(Projections.groupProperty("typeid")));
List<String> tableNames=new ArrayList<>(); List<String> tableNames = new ArrayList<>();
StringBuffer sb=new StringBuffer(); StringBuffer sb = new StringBuffer();
for(Integer id:typeid){ for (Integer id : typeid) {
String tableName="data_"+id; String tableName = "data_" + id;
sb.append("create table "+tableName+" like data_1;\n"); sb.append("create table " + tableName + " like data_1;\n");
tableNames.add(tableName); tableNames.add(tableName);
} }
@ -135,26 +89,26 @@ public class SpringTest {
" vp integer,\n" + " vp integer,\n" +
" type_id integer,\n" + " type_id integer,\n" +
" primary key (cid)\n" + " primary key (cid)\n" +
" ) engine=MERGE UNION=("+StringUtils.join(tableNames,",")+") insert_method=no;"); " ) engine=MERGE UNION=(" + StringUtils.join(tableNames, ",") + ") insert_method=no;");
try { try {
FileUtils.writeStringToFile(new File("test.sql"),sb.toString(),"utf-8",true); FileUtils.writeStringToFile(new File("test.sql"), sb.toString(), "utf-8", true);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Test // @Test
public void test4() throws IOException { public void test4() throws IOException {
DetachedCriteria criteria=DetachedCriteria.forClass(DataModel.class); DetachedCriteria criteria = DetachedCriteria.forClass(DataModel.class);
List<DataModel> dataEntities= (List<DataModel>) baseDao1.findByCriteria(criteria,10,10); List<DataModel> dataEntities = (List<DataModel>) baseDao1.findByCriteria(criteria, 10, 10);
for(DataModel dataEntity:dataEntities){ for (DataModel dataEntity : dataEntities) {
log.info(dataEntity.getCid()); log.info(dataEntity.getCid());
} }
// 查询总条数 // 查询总条数
Long count=baseDao1.executeWithNativeSession((Session session)-> (Long) DetachedCriteria.forClass(DataModel.class).setProjection(Projections.rowCount()).setProjection(Projections.groupProperty("groupId")).getExecutableCriteria(session).uniqueResult()); Long count = baseDao1.executeWithNativeSession((Session session) -> (Long) DetachedCriteria.forClass(DataModel.class).setProjection(Projections.rowCount()).setProjection(Projections.groupProperty("groupId")).getExecutableCriteria(session).uniqueResult());
} }
} }

Loading…
Cancel
Save