diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..34390be --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM openjdk:8u272-jdk + +RUN apt-get update && apt-get install -y redis-server + +WORKDIR /application +ARG JAR_FILE=./eladmin-system/target/eladmin-system-2.7.jar +COPY ${JAR_FILE} application.jar + +ENTRYPOINT ["java", "-jar", "/application/application.jar"] \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/AppRun.java b/eladmin-system/src/main/java/me/zhengjie/AppRun.java index e79d209..729c123 100644 --- a/eladmin-system/src/main/java/me/zhengjie/AppRun.java +++ b/eladmin-system/src/main/java/me/zhengjie/AppRun.java @@ -15,21 +15,39 @@ */ package me.zhengjie; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import java.util.List; +import java.util.Optional; +import java.util.function.Function; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.ApplicationPidFileWriter; import org.springframework.context.annotation.Bean; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery; import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RestController; +import com.alipay.api.domain.Goods; + import io.swagger.annotations.Api; import lombok.RequiredArgsConstructor; import me.zhengjie.annotation.rest.AnonymousGetMapping; import me.zhengjie.mapper.db1.IStoreMapper; import me.zhengjie.mapper.db2.IStoreMapper2; +import me.zhengjie.modules.goods.repository.GoodsRepository; import me.zhengjie.utils.SpringContextHolder; /** @@ -42,9 +60,10 @@ import me.zhengjie.utils.SpringContextHolder; @RestController @Api(hidden = true) @SpringBootApplication -@EnableTransactionManagement -@EnableJpaAuditing(auditorAwareRef = "auditorAware") +// @EnableTransactionManagement +// @EnableJpaAuditing(auditorAwareRef = "auditorAware") @RequiredArgsConstructor +@Component public class AppRun { public static void main(String[] args) { @@ -73,6 +92,265 @@ public class AppRun { public String index() { System.out.println(iStoreMapper.findByName("店1")); System.out.println(iStoreMapper2.findByName("店1")); + // test(); return "Backend service started successfully"; } + + // public void test() { + // // 设置使用主数据源 + // DynamicDataSource.setCurrentLookupKey("primaryDataSource"); + // // 执行数据库操作 + // System.out.println("query start:"); + // GoodsRepository goodsRepository = new GoodsRepository() { + + // @Override + // public List findAll() { + // // TODO Auto-generated method stub + // Goods goods = new Goods(); + + // } + + // @Override + // public List findAll(Sort sort) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findAll'"); + // } + + // @Override + // public List + // findAllById(Iterable ids) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method + // 'findAllById'"); + // } + + // @Override + // public List + // saveAll(Iterable entities) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'saveAll'"); + // } + + // @Override + // public void flush() { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'flush'"); + // } + + // @Override + // public S saveAndFlush(S + // entity) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method + // 'saveAndFlush'"); + // } + + // @Override + // public List + // saveAllAndFlush(Iterable entities) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method + // 'saveAllAndFlush'"); + // } + + // @Override + // public void deleteAllInBatch(Iterable + // entities) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method + // 'deleteAllInBatch'"); + // } + + // @Override + // public void deleteAllByIdInBatch(Iterable ids) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method + // 'deleteAllByIdInBatch'"); + // } + + // @Override + // public void deleteAllInBatch() { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method + // 'deleteAllInBatch'"); + // } + + // @Override + // public me.zhengjie.modules.goods.domain.Goods getOne(Integer id) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'getOne'"); + // } + + // @Override + // public me.zhengjie.modules.goods.domain.Goods getById(Integer id) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'getById'"); + // } + + // @Override + // public List + // findAll(Example example) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findAll'"); + // } + + // @Override + // public List + // findAll(Example example, Sort sort) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findAll'"); + // } + + // @Override + // public Page findAll(Pageable + // pageable) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findAll'"); + // } + + // @Override + // public S save(S entity) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'save'"); + // } + + // @Override + // public Optional findById(Integer id) + // { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findById'"); + // } + + // @Override + // public boolean existsById(Integer id) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'existsById'"); + // } + + // @Override + // public long count() { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'count'"); + // } + + // @Override + // public void deleteById(Integer id) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'deleteById'"); + // } + + // @Override + // public void delete(me.zhengjie.modules.goods.domain.Goods entity) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'delete'"); + // } + + // @Override + // public void deleteAllById(Iterable ids) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method + // 'deleteAllById'"); + // } + + // @Override + // public void deleteAll(Iterable entities) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'deleteAll'"); + // } + + // @Override + // public void deleteAll() { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'deleteAll'"); + // } + + // @Override + // public Optional + // findOne(Example example) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findOne'"); + // } + + // @Override + // public Page + // findAll(Example example, + // Pageable pageable) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findAll'"); + // } + + // @Override + // public long + // count(Example example) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'count'"); + // } + + // @Override + // public boolean + // exists(Example example) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'exists'"); + // } + + // @Override + // public R + // findBy(Example example, + // Function, R> queryFunction) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findBy'"); + // } + + // @Override + // public Optional findOne( + // Specification spec) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findOne'"); + // } + + // @Override + // public List findAll( + // Specification spec) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findAll'"); + // } + + // @Override + // public Page findAll( + // Specification spec, Pageable + // pageable) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findAll'"); + // } + + // @Override + // public List findAll( + // Specification spec, Sort sort) { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'findAll'"); + // } + + // @Override + // public long count(Specification spec) + // { + // // TODO Auto-generated method stub + // throw new UnsupportedOperationException("Unimplemented method 'count'"); + // } + + // }; + // System.out.println("query end:"); + // // ... + // // 清除数据源上下文 + // DynamicDataSource.clearCurrentLookupKey(); + + // // 设置使用辅数据源 + // DynamicDataSource.setCurrentLookupKey("secondDataSource"); + // // 执行数据库操作 + // System.out.println("query2 start:"); + // System.out.println("query2 end:"); + // // ... + // // 清除数据源上下文 + // DynamicDataSource.clearCurrentLookupKey(); + // } } diff --git a/eladmin-system/src/main/java/me/zhengjie/DynamicDataSource.java b/eladmin-system/src/main/java/me/zhengjie/DynamicDataSource.java new file mode 100644 index 0000000..97faf9f --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/DynamicDataSource.java @@ -0,0 +1,23 @@ +package me.zhengjie; + +import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; + +public class DynamicDataSource extends AbstractRoutingDataSource { + + private static final ThreadLocal contextHolder = new ThreadLocal<>(); + + @Override + protected Object determineCurrentLookupKey() { + System.out.println("get:" + contextHolder.get()); + return contextHolder.get(); + } + + public static void setCurrentLookupKey(String key) { + System.out.println("set:" + key); + contextHolder.set(key); + } + + public static void clearCurrentLookupKey() { + contextHolder.remove(); + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/config/jpa/JPAPrimaryConfig.java b/eladmin-system/src/main/java/me/zhengjie/config/jpa/JPAPrimaryConfig.java.txt similarity index 100% rename from eladmin-system/src/main/java/me/zhengjie/config/jpa/JPAPrimaryConfig.java rename to eladmin-system/src/main/java/me/zhengjie/config/jpa/JPAPrimaryConfig.java.txt diff --git a/eladmin-system/src/main/java/me/zhengjie/config/jpa/JPASecondaryConfig.java b/eladmin-system/src/main/java/me/zhengjie/config/jpa/JPASecondaryConfig.java.txt similarity index 100% rename from eladmin-system/src/main/java/me/zhengjie/config/jpa/JPASecondaryConfig.java rename to eladmin-system/src/main/java/me/zhengjie/config/jpa/JPASecondaryConfig.java.txt diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/quartz/config/JobRunner.java b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/config/JobRunner.java index a5a9c91..f412bd7 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/quartz/config/JobRunner.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/config/JobRunner.java @@ -15,16 +15,18 @@ */ package me.zhengjie.modules.quartz.config; -import lombok.RequiredArgsConstructor; -import me.zhengjie.modules.quartz.domain.QuartzJob; -import me.zhengjie.modules.quartz.repository.QuartzJobRepository; -import me.zhengjie.modules.quartz.utils.QuartzManage; +import java.util.List; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; -import java.util.List; + +import lombok.RequiredArgsConstructor; +import me.zhengjie.modules.quartz.domain.QuartzJob; +import me.zhengjie.modules.quartz.repository.QuartzJobRepository; +import me.zhengjie.modules.quartz.utils.QuartzManage; /** * @author Zheng Jie @@ -48,4 +50,5 @@ public class JobRunner implements ApplicationRunner { quartzJobs.forEach(quartzManage::addJob); log.info("Timing task injection complete"); } + } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/quartz/rest/QuartzJobController.java b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/rest/QuartzJobController.java index e1b5da7..eefaff5 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/quartz/rest/QuartzJobController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/quartz/rest/QuartzJobController.java @@ -15,6 +15,25 @@ */ package me.zhengjie.modules.quartz.rest; +import java.io.IOException; +import java.util.Set; + +import javax.servlet.http.HttpServletResponse; + +import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; @@ -27,15 +46,6 @@ import me.zhengjie.modules.quartz.service.QuartzJobService; import me.zhengjie.modules.quartz.service.dto.JobQueryCriteria; import me.zhengjie.utils.PageResult; import me.zhengjie.utils.SpringContextHolder; -import org.springframework.data.domain.Pageable; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.*; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Set; /** * @author Zheng Jie @@ -54,8 +64,8 @@ public class QuartzJobController { @ApiOperation("查询定时任务") @GetMapping @PreAuthorize("@el.check('timing:list')") - public ResponseEntity> queryQuartzJob(JobQueryCriteria criteria, Pageable pageable){ - return new ResponseEntity<>(quartzJobService.queryAll(criteria,pageable), HttpStatus.OK); + public ResponseEntity> queryQuartzJob(JobQueryCriteria criteria, Pageable pageable) { + return new ResponseEntity<>(quartzJobService.queryAll(criteria, pageable), HttpStatus.OK); } @ApiOperation("导出任务数据") @@ -75,17 +85,17 @@ public class QuartzJobController { @ApiOperation("查询任务执行日志") @GetMapping(value = "/logs") @PreAuthorize("@el.check('timing:list')") - public ResponseEntity> queryQuartzJobLog(JobQueryCriteria criteria, Pageable pageable){ - return new ResponseEntity<>(quartzJobService.queryAllLog(criteria,pageable), HttpStatus.OK); + public ResponseEntity> queryQuartzJobLog(JobQueryCriteria criteria, Pageable pageable) { + return new ResponseEntity<>(quartzJobService.queryAllLog(criteria, pageable), HttpStatus.OK); } @Log("新增定时任务") @ApiOperation("新增定时任务") @PostMapping @PreAuthorize("@el.check('timing:add')") - public ResponseEntity createQuartzJob(@Validated @RequestBody QuartzJob resources){ + public ResponseEntity createQuartzJob(@Validated @RequestBody QuartzJob resources) { if (resources.getId() != null) { - throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID"); + throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID"); } // 验证Bean是不是合法的,合法的定时任务 Bean 需要用 @Service 定义 checkBean(resources.getBeanName()); @@ -97,7 +107,7 @@ public class QuartzJobController { @ApiOperation("修改定时任务") @PutMapping @PreAuthorize("@el.check('timing:edit')") - public ResponseEntity updateQuartzJob(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources){ + public ResponseEntity updateQuartzJob(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources) { // 验证Bean是不是合法的,合法的定时任务 Bean 需要用 @Service 定义 checkBean(resources.getBeanName()); quartzJobService.update(resources); @@ -108,7 +118,7 @@ public class QuartzJobController { @ApiOperation("更改定时任务状态") @PutMapping(value = "/{id}") @PreAuthorize("@el.check('timing:edit')") - public ResponseEntity updateQuartzJobStatus(@PathVariable Long id){ + public ResponseEntity updateQuartzJobStatus(@PathVariable Long id) { quartzJobService.updateIsPause(quartzJobService.findById(id)); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @@ -117,7 +127,7 @@ public class QuartzJobController { @ApiOperation("执行定时任务") @PutMapping(value = "/exec/{id}") @PreAuthorize("@el.check('timing:edit')") - public ResponseEntity executionQuartzJob(@PathVariable Long id){ + public ResponseEntity executionQuartzJob(@PathVariable Long id) { quartzJobService.execution(quartzJobService.findById(id)); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @@ -126,15 +136,15 @@ public class QuartzJobController { @ApiOperation("删除定时任务") @DeleteMapping @PreAuthorize("@el.check('timing:del')") - public ResponseEntity deleteQuartzJob(@RequestBody Set ids){ + public ResponseEntity deleteQuartzJob(@RequestBody Set ids) { quartzJobService.delete(ids); return new ResponseEntity<>(HttpStatus.OK); } - private void checkBean(String beanName){ + private void checkBean(String beanName) { // 避免调用攻击者可以从SpringContextHolder获得控制jdbcTemplate类 // 并使用getDeclaredMethod调用jdbcTemplate的queryForMap函数,执行任意sql命令。 - if(!SpringContextHolder.getAllServiceBeanName().contains(beanName)){ + if (!SpringContextHolder.getAllServiceBeanName().contains(beanName)) { throw new BadRequestException("非法的 Bean,请重新输入!"); } }