liuchao
4 лет назад
24 изменённых файлов: 1034 добавлений и 19 удалений
@ -0,0 +1,31 @@ |
|||
package com.aiprose.querydsl.config; |
|||
|
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.core.convert.converter.Converter; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.text.ParseException; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
|
|||
@Component |
|||
public class DateConverter implements Converter<String, Date> { |
|||
@Override |
|||
public Date convert(String source) { |
|||
if(StringUtils.isNoneBlank(source)){ |
|||
SimpleDateFormat dateFormat; |
|||
if(source.trim().length()<11){ |
|||
dateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
|||
}else{ |
|||
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|||
} |
|||
dateFormat.setLenient(false); |
|||
try { |
|||
return dateFormat.parse(source); |
|||
} catch (ParseException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
} |
@ -0,0 +1,52 @@ |
|||
package com.aiprose.querydsl.config; |
|||
|
|||
import com.google.common.collect.Lists; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import springfox.documentation.builders.ApiInfoBuilder; |
|||
import springfox.documentation.builders.PathSelectors; |
|||
import springfox.documentation.builders.RequestHandlerSelectors; |
|||
import springfox.documentation.service.*; |
|||
import springfox.documentation.spi.DocumentationType; |
|||
import springfox.documentation.spi.service.contexts.SecurityContext; |
|||
import springfox.documentation.spring.web.plugins.Docket; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Configuration |
|||
public class SwaggerConfiguration { |
|||
@Bean |
|||
public Docket createRestApi() { |
|||
return new Docket(DocumentationType.OAS_30) |
|||
.apiInfo(apiInfo()) |
|||
.select() |
|||
.apis(RequestHandlerSelectors.basePackage("com.aiprose.querydsl.controller")) |
|||
.paths(PathSelectors.any()) |
|||
.build() |
|||
.securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.<SecurityScheme>newArrayList(new ApiKey("token", "token", "header"))); |
|||
} |
|||
|
|||
private ApiInfo apiInfo() { |
|||
return new ApiInfoBuilder() |
|||
.title("querydsl demo") |
|||
.description("springboot mybatis plus框架demo") |
|||
.contact(new Contact("nelson", "https://www.aiprose.com/", "mail_yanpeng@163.com")) |
|||
.version("1.0") |
|||
.build(); |
|||
} |
|||
|
|||
private SecurityContext securityContext() { |
|||
return SecurityContext.builder() |
|||
.securityReferences(defaultAuth()) |
|||
.forPaths(PathSelectors.regex("/.*")) |
|||
.build(); |
|||
} |
|||
|
|||
List<SecurityReference> defaultAuth() { |
|||
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
|||
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
|||
authorizationScopes[0] = authorizationScope; |
|||
return Lists.newArrayList(new SecurityReference("token", authorizationScopes)); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,64 @@ |
|||
package com.aiprose.querydsl.config; |
|||
|
|||
import com.aiprose.querydsl.entity.Response; |
|||
import com.aiprose.utils.JwtUtil; |
|||
import com.alibaba.fastjson.JSON; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.http.HttpHeaders; |
|||
import org.springframework.http.HttpMethod; |
|||
import org.springframework.http.HttpStatus; |
|||
import org.springframework.http.server.reactive.ServerHttpRequest; |
|||
import org.springframework.http.server.reactive.ServerHttpResponse; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.util.StringUtils; |
|||
import org.springframework.web.server.ServerWebExchange; |
|||
import org.springframework.web.server.WebFilter; |
|||
import org.springframework.web.server.WebFilterChain; |
|||
import reactor.core.publisher.Mono; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: liuchao |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/15 |
|||
*/ |
|||
@Component |
|||
public class TokenInterceptor implements WebFilter { |
|||
private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN,token,username,client"; |
|||
|
|||
@Autowired |
|||
private Response resp; |
|||
|
|||
|
|||
@Override |
|||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { |
|||
ServerHttpRequest request = exchange.getRequest(); |
|||
ServerHttpResponse response = exchange.getResponse(); |
|||
if (request.getPath().value().contains("login") || request.getPath().value().contains("swagger") || request.getPath().value().contains("api-docs") || request.getPath().value().contains("regist")) { |
|||
return chain.filter(exchange); |
|||
} |
|||
String token = request.getHeaders().getFirst("token"); |
|||
if (request.getMethod() == HttpMethod.OPTIONS || StringUtils.isEmpty(token)) { |
|||
HttpHeaders headers = response.getHeaders(); |
|||
headers.add("Access-Control-Allow-Origin", "*"); |
|||
headers.add("Access-Control-Allow-Methods", "*"); |
|||
headers.add("Access-Control-Max-Age", "18000L"); |
|||
headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS); |
|||
headers.add("Access-Control-Expose-Headers", "*"); |
|||
headers.add("Access-Control-Allow-Credentials", "true"); |
|||
} |
|||
if (request.getMethod() == HttpMethod.OPTIONS) { |
|||
response.setStatusCode(HttpStatus.OK); |
|||
return Mono.empty(); |
|||
} |
|||
if (StringUtils.isEmpty(token)){ |
|||
return response.writeWith(Mono.just(response.bufferFactory().wrap(JSON.toJSON(resp.failure(HttpStatus.UNAUTHORIZED, "请先登录")).toString().getBytes()))); |
|||
} |
|||
try { |
|||
exchange.getAttributes().put("uid", JwtUtil.getUid(token)); |
|||
} catch (Exception e) { |
|||
return response.writeWith(Mono.just(response.bufferFactory().wrap(JSON.toJSON(resp.failure(HttpStatus.UNAUTHORIZED, "请先登录")).toString().getBytes()))); |
|||
} |
|||
return chain.filter(exchange); |
|||
} |
|||
} |
@ -0,0 +1,27 @@ |
|||
package com.aiprose.querydsl.config; |
|||
|
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.web.reactive.config.CorsRegistry; |
|||
import org.springframework.web.reactive.config.ResourceHandlerRegistry; |
|||
import org.springframework.web.reactive.config.WebFluxConfigurer; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/15 |
|||
*/ |
|||
@Configuration |
|||
public class WebFluxConf implements WebFluxConfigurer { |
|||
@Override |
|||
public void addCorsMappings(CorsRegistry registry) { |
|||
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "OPTIONS", "DELETE").maxAge(3600); |
|||
} |
|||
|
|||
@Override |
|||
public void addResourceHandlers(ResourceHandlerRegistry registry) { |
|||
registry.addResourceHandler("/swagger-ui/**") |
|||
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") |
|||
.resourceChain(false); |
|||
} |
|||
} |
@ -0,0 +1,49 @@ |
|||
package com.aiprose.querydsl.controller; |
|||
|
|||
import com.aiprose.querydsl.entity.Dept; |
|||
import com.aiprose.querydsl.entity.Emp; |
|||
import com.aiprose.querydsl.entity.Response; |
|||
import com.aiprose.querydsl.service.DeptService; |
|||
import com.aiprose.querydsl.util.FixedPageData; |
|||
import com.aiprose.querydsl.vos.DeptTreeVo; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
import reactor.core.publisher.Mono; |
|||
|
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("dept") |
|||
public class DeptController { |
|||
|
|||
@Autowired |
|||
private DeptService service; |
|||
|
|||
|
|||
@Autowired |
|||
private Response resp; |
|||
|
|||
@PostMapping("save") |
|||
public Mono<Response> save(@RequestBody Dept dept){ |
|||
Dept save = service.save(dept); |
|||
return Mono.just(resp.success(save)); |
|||
} |
|||
|
|||
@DeleteMapping("{id}") |
|||
public Mono<Response> delete(@PathVariable("id") Integer id){ |
|||
service.deleteById(id); |
|||
return Mono.just(resp.success()); |
|||
} |
|||
|
|||
@GetMapping("tree") |
|||
public Mono<Response> tree(){ |
|||
List<DeptTreeVo> tree= service.tree(); |
|||
return Mono.just(resp.success(tree)); |
|||
} |
|||
} |
@ -0,0 +1,51 @@ |
|||
package com.aiprose.querydsl.controller; |
|||
|
|||
import com.aiprose.querydsl.entity.Emp; |
|||
import com.aiprose.querydsl.service.EmpService; |
|||
import com.aiprose.querydsl.util.FixedPageData; |
|||
import com.aiprose.querydsl.vos.UserVo; |
|||
import com.aiprose.querydsl.entity.Response; |
|||
import com.aiprose.querydsl.entity.User; |
|||
import com.aiprose.querydsl.service.UserService; |
|||
import com.aiprose.utils.JwtUtil; |
|||
import com.aiprose.utils.MD5Encrypt; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
import reactor.core.publisher.Mono; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
@RestController |
|||
public class EmpController { |
|||
|
|||
@Autowired |
|||
private EmpService service; |
|||
|
|||
@Autowired |
|||
private Response resp; |
|||
|
|||
@GetMapping("list") |
|||
public Mono<Response> list(@RequestParam Integer page, @RequestParam Integer size, Emp emp){ |
|||
FixedPageData pageData = service.list(page,size,emp); |
|||
return Mono.just(resp.page(pageData.getResults(),pageData.getTotal())); |
|||
} |
|||
|
|||
@PostMapping("save") |
|||
public Mono<Response> save(@RequestBody Emp emp){ |
|||
service.save(emp); |
|||
return Mono.just(resp.success()); |
|||
} |
|||
|
|||
@DeleteMapping("{id}") |
|||
public Mono<Response> delete(@PathVariable("id") Integer id){ |
|||
service.deleteById(id); |
|||
return Mono.just(resp.success()); |
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
package com.aiprose.querydsl.entity; |
|||
|
|||
import com.alibaba.fastjson.annotation.JSONField; |
|||
import com.fasterxml.jackson.annotation.JsonFormat; |
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import lombok.Data; |
|||
import org.springframework.format.annotation.DateTimeFormat; |
|||
|
|||
import javax.persistence.Column; |
|||
import javax.persistence.Entity; |
|||
import javax.persistence.Table; |
|||
import java.util.Date; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
@Data |
|||
@Entity |
|||
@Table(name = "tb_dept") |
|||
public class Dept extends IDEntity { |
|||
@Column(length = 10) |
|||
private String name; |
|||
|
|||
@Column(length = 30) |
|||
private String loc; //位置
|
|||
|
|||
@Column(columnDefinition = "INT UNSIGNED") |
|||
private Integer pid; |
|||
|
|||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
|||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
|||
private Date cdate; //创建时间
|
|||
} |
@ -0,0 +1,35 @@ |
|||
package com.aiprose.querydsl.entity; |
|||
|
|||
import com.alibaba.fastjson.annotation.JSONField; |
|||
import com.fasterxml.jackson.annotation.JsonFormat; |
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import lombok.Data; |
|||
import org.springframework.format.annotation.DateTimeFormat; |
|||
|
|||
import javax.persistence.Column; |
|||
import javax.persistence.Entity; |
|||
import javax.persistence.Table; |
|||
import java.util.Date; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
@Data |
|||
@Entity |
|||
@Table(name = "tb_emp") |
|||
public class Emp extends IDEntity { |
|||
@Column(nullable = false, length = 20) |
|||
private String empName; |
|||
|
|||
private Long mobile; |
|||
|
|||
@Column(columnDefinition = "INT UNSIGNED") |
|||
private Integer deptId; |
|||
|
|||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
|||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
|||
private Date joindate; |
|||
} |
@ -0,0 +1,26 @@ |
|||
package com.aiprose.querydsl.entity; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.persistence.GeneratedValue; |
|||
import javax.persistence.GenerationType; |
|||
import javax.persistence.Id; |
|||
import javax.persistence.MappedSuperclass; |
|||
import java.io.Serializable; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
@Data |
|||
@MappedSuperclass |
|||
public class LDEntity implements Serializable { |
|||
/** |
|||
* 主键 |
|||
*/ |
|||
@Id |
|||
@GeneratedValue(strategy = GenerationType.AUTO) |
|||
protected Long id; |
|||
} |
@ -0,0 +1,198 @@ |
|||
package com.aiprose.querydsl.entity; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
import lombok.Getter; |
|||
import org.springframework.context.annotation.Scope; |
|||
import org.springframework.http.HttpStatus; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.ArrayList; |
|||
|
|||
@Component |
|||
@Scope("prototype") |
|||
@SuppressWarnings(value = "all") |
|||
public class Response { |
|||
/** |
|||
* 默认成功响应码 |
|||
*/ |
|||
private static final Integer DEAFAULT_SUCCESS_CODE = HttpStatus.OK.value(); |
|||
/** |
|||
* 默认成功响应信息 |
|||
*/ |
|||
private static final String DEAFAULT_SUCCESS_MSG = "请求/处理成功!"; |
|||
/** |
|||
* 默认失败响应码 |
|||
*/ |
|||
private static final Integer DEAFAULT_FAILURE_CODE = HttpStatus.INTERNAL_SERVER_ERROR.value(); |
|||
/** |
|||
* 默认失败响应信息 |
|||
*/ |
|||
private static final String DEAFAULT_FAILURE_MSG = "请求/处理失败!"; |
|||
|
|||
@Getter |
|||
private Meta meta; |
|||
|
|||
@Getter |
|||
private Object data; |
|||
|
|||
@Getter |
|||
private Long total =0l; |
|||
|
|||
|
|||
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓成功↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ |
|||
|
|||
public Response success() { |
|||
this.meta = new Meta(DEAFAULT_SUCCESS_CODE, DEAFAULT_SUCCESS_MSG); |
|||
this.data = null; |
|||
return this; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 处理成功响应,返回自定义响应码、信息和数据。 |
|||
* |
|||
* @param msg 处理结果信息 |
|||
* @return 响应对象 |
|||
*/ |
|||
public Response success(String msg) { |
|||
this.meta = new Meta(DEAFAULT_SUCCESS_CODE, msg); |
|||
this.data = null; |
|||
return this; |
|||
} |
|||
|
|||
/** |
|||
* 处理成功响应,返回自定义响应码、信息和数据。 |
|||
* |
|||
* @param data 返回数据 |
|||
* @return 响应对象 |
|||
*/ |
|||
public Response success(Object data) { |
|||
this.meta = new Meta(DEAFAULT_SUCCESS_CODE, DEAFAULT_SUCCESS_MSG); |
|||
this.data = data; |
|||
return this; |
|||
} |
|||
|
|||
/** |
|||
* 处理成功响应,返回自定义响应码、信息和数据。 |
|||
* |
|||
* @param msg 处理结果信息 |
|||
* @param data 返回数据 |
|||
* @return 响应对象 |
|||
*/ |
|||
public Response success(String msg, Object data) { |
|||
this.meta = new Meta(DEAFAULT_SUCCESS_CODE, msg); |
|||
this.data = data; |
|||
return this; |
|||
} |
|||
|
|||
/** |
|||
* 分页数据为空 |
|||
* @return |
|||
*/ |
|||
public Response page() { |
|||
this.meta = new Meta(DEAFAULT_SUCCESS_CODE, DEAFAULT_SUCCESS_MSG); |
|||
this.data = new ArrayList<>(); |
|||
this.total = 0l; |
|||
return this; |
|||
} |
|||
|
|||
/** |
|||
* 分页数据和总数量 |
|||
* @return |
|||
*/ |
|||
public Response page(Object data,Long total) { |
|||
this.meta = new Meta(DEAFAULT_SUCCESS_CODE, DEAFAULT_SUCCESS_MSG); |
|||
this.data = data; |
|||
this.total = total; |
|||
return this; |
|||
} |
|||
|
|||
/** |
|||
* 处理成功响应,返回自定义响应码、信息和数据。 |
|||
* |
|||
* @param httpStatus HTTP 响应码 |
|||
* @param msg 处理结果信息 |
|||
* @param data 返回数据 |
|||
* @return 响应对象 |
|||
*/ |
|||
public Response success(HttpStatus httpStatus, String msg, Object data) { |
|||
this.meta = new Meta(httpStatus.value(), msg); |
|||
this.data = data; |
|||
return this; |
|||
} |
|||
|
|||
|
|||
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓失败↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ |
|||
|
|||
/** |
|||
* 处理失败响应,返回自定义响应码、信息和数据。 |
|||
* |
|||
* @param msg 处理结果信息 |
|||
* @return 响应对象 |
|||
*/ |
|||
public Response failure(String msg) { |
|||
this.meta = new Meta(DEAFAULT_FAILURE_CODE, msg); |
|||
this.data = null; |
|||
return this; |
|||
} |
|||
|
|||
/** |
|||
* 处理失败响应,返回自定义响应码、信息和数据。 |
|||
* |
|||
* @param data 返回数据 |
|||
* @return 响应对象 |
|||
*/ |
|||
public Response failure(Object data) { |
|||
this.meta = new Meta(DEAFAULT_FAILURE_CODE, DEAFAULT_FAILURE_MSG); |
|||
this.data = data; |
|||
return this; |
|||
} |
|||
|
|||
/** |
|||
* 处理失败响应,返回自定义响应码、信息和数据。 |
|||
* |
|||
* @param msg 处理结果信息 |
|||
* @param data 返回数据 |
|||
* @return 响应对象 |
|||
*/ |
|||
public Response failure(String msg, Object data) { |
|||
this.meta = new Meta(DEAFAULT_FAILURE_CODE, msg); |
|||
this.data = data; |
|||
return this; |
|||
} |
|||
|
|||
/** |
|||
* 处理失败响应,返回自定义响应码、信息和数据。 |
|||
* |
|||
* @param httpStatus HTTP 响应码 |
|||
* @param msg 处理结果信息 |
|||
* @param data 返回数据 |
|||
* @return 响应对象 |
|||
*/ |
|||
public Response failure(HttpStatus httpStatus, String msg, Object data) { |
|||
this.meta = new Meta(httpStatus.value(), msg); |
|||
this.data = data; |
|||
return this; |
|||
} |
|||
|
|||
public Response failure(HttpStatus httpStatus, String msg) { |
|||
this.meta = new Meta(httpStatus.value(), msg); |
|||
return this; |
|||
} |
|||
|
|||
@Data |
|||
@AllArgsConstructor |
|||
private class Meta { |
|||
|
|||
/** |
|||
* 处理结果代码,与 HTTP 状态响应码对应 |
|||
*/ |
|||
private Integer code; |
|||
|
|||
/** |
|||
* 处理结果信息 |
|||
*/ |
|||
private String msg; |
|||
} |
|||
} |
@ -0,0 +1,15 @@ |
|||
package com.aiprose.querydsl.repository; |
|||
|
|||
import com.aiprose.querydsl.entity.Dept; |
|||
import org.springframework.data.jpa.repository.JpaRepository; |
|||
import org.springframework.data.querydsl.QuerydslPredicateExecutor; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
public interface DeptRepository extends JpaRepository<Dept, Integer>, |
|||
QuerydslPredicateExecutor<Dept> { |
|||
} |
@ -0,0 +1,16 @@ |
|||
package com.aiprose.querydsl.repository; |
|||
|
|||
import com.aiprose.querydsl.entity.Emp; |
|||
import com.aiprose.querydsl.entity.User; |
|||
import org.springframework.data.jpa.repository.JpaRepository; |
|||
import org.springframework.data.querydsl.QuerydslPredicateExecutor; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
public interface EmpRepository extends JpaRepository<Emp, Integer>, |
|||
QuerydslPredicateExecutor<User>{ |
|||
} |
@ -0,0 +1,74 @@ |
|||
package com.aiprose.querydsl.service; |
|||
|
|||
import com.aiprose.base.BasicServiceImpl; |
|||
import com.aiprose.querydsl.entity.Dept; |
|||
import com.aiprose.querydsl.entity.QDept; |
|||
import com.aiprose.querydsl.entity.QUser; |
|||
import com.aiprose.querydsl.entity.User; |
|||
import com.aiprose.querydsl.repository.DeptRepository; |
|||
import com.aiprose.querydsl.repository.UserRepository; |
|||
import com.aiprose.querydsl.util.FixedPageData; |
|||
import com.aiprose.querydsl.vos.DeptTreeVo; |
|||
import com.aiprose.utils.MD5Encrypt; |
|||
import com.querydsl.jpa.impl.JPAQueryFactory; |
|||
import org.springframework.beans.BeanUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.data.jpa.repository.JpaRepository; |
|||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import javax.persistence.EntityManager; |
|||
import javax.persistence.PersistenceContext; |
|||
import java.util.List; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
@Transactional |
|||
@Service |
|||
public class DeptService extends BasicServiceImpl<Dept, Integer> { |
|||
|
|||
@Autowired |
|||
private DeptRepository repository; |
|||
|
|||
@Autowired |
|||
private JPAQueryFactory queryFactory; |
|||
|
|||
@Override |
|||
public JpaRepository<Dept, Integer> getJpaRepository() { |
|||
return repository; |
|||
} |
|||
|
|||
@Override |
|||
public JpaSpecificationExecutor<Dept> getJpaSpecificationExecutor() { |
|||
return null; |
|||
} |
|||
|
|||
public List<DeptTreeVo> tree() { |
|||
QDept qDept = QDept.dept; |
|||
List<DeptTreeVo> collect = queryFactory.selectFrom(qDept).where(qDept.pid.isNull()).fetch().stream().map(x -> { |
|||
DeptTreeVo vo = new DeptTreeVo(); |
|||
BeanUtils.copyProperties(x, vo); |
|||
List<DeptTreeVo> childs = getChilds(qDept, x.getId()); |
|||
vo.setChild(childs); |
|||
return vo; |
|||
}).collect(Collectors.toList()); |
|||
return collect; |
|||
} |
|||
|
|||
private List<DeptTreeVo> getChilds(QDept qDept, Integer id) { |
|||
return queryFactory.selectFrom(qDept).where(qDept.pid.eq(id)).fetch().stream().map(x -> { |
|||
DeptTreeVo vo = new DeptTreeVo(); |
|||
BeanUtils.copyProperties(x, vo); |
|||
List<DeptTreeVo> childs = getChilds(qDept, x.getId()); |
|||
vo.setChild(childs); |
|||
return vo; |
|||
}).collect(Collectors.toList()); |
|||
} |
|||
} |
@ -0,0 +1,71 @@ |
|||
package com.aiprose.querydsl.service; |
|||
|
|||
import com.aiprose.base.BasicServiceImpl; |
|||
import com.aiprose.querydsl.entity.Emp; |
|||
import com.aiprose.querydsl.entity.QEmp; |
|||
import com.aiprose.querydsl.repository.EmpRepository; |
|||
import com.aiprose.querydsl.util.FixedPageData; |
|||
import com.querydsl.core.QueryResults; |
|||
import com.querydsl.core.types.ExpressionUtils; |
|||
import com.querydsl.core.types.Predicate; |
|||
import com.querydsl.jpa.impl.JPAQueryFactory; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.data.jpa.repository.JpaRepository; |
|||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import javax.persistence.EntityManager; |
|||
import javax.persistence.PersistenceContext; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/14 |
|||
*/ |
|||
@Transactional |
|||
@Service |
|||
public class EmpService extends BasicServiceImpl<Emp,Integer> { |
|||
|
|||
@Autowired |
|||
private JPAQueryFactory queryFactory; |
|||
|
|||
@Autowired |
|||
private EmpRepository repository; |
|||
|
|||
@Override |
|||
public JpaRepository<Emp, Integer> getJpaRepository() { |
|||
return repository; |
|||
} |
|||
|
|||
@Override |
|||
public JpaSpecificationExecutor<Emp> getJpaSpecificationExecutor() { |
|||
return null; |
|||
} |
|||
|
|||
public FixedPageData list(Integer page, Integer size, Emp emp) { |
|||
FixedPageData pageData = new FixedPageData(page, size); |
|||
QEmp qEmp = QEmp.emp; |
|||
Integer deptId = emp.getDeptId(); |
|||
String empName = emp.getEmpName(); |
|||
Long mobile = emp.getMobile(); |
|||
Predicate predicate = qEmp.isNotNull(); |
|||
if (StringUtils.isNotBlank(empName)) { |
|||
predicate = ExpressionUtils.and(predicate, qEmp.empName.eq(empName)); |
|||
} |
|||
if (deptId != null) { |
|||
predicate = ExpressionUtils.and(predicate, qEmp.deptId.eq(deptId)); |
|||
} |
|||
if (mobile != null) { |
|||
predicate = ExpressionUtils.and(predicate, qEmp.mobile.eq(mobile)); |
|||
} |
|||
QueryResults<Emp> queryResults = queryFactory.selectFrom(qEmp).where(predicate).offset(pageData.getOffset()) |
|||
.limit(pageData.getSize()).fetchResults(); |
|||
pageData.setResults(queryResults.getResults()); |
|||
pageData.setTotal(queryResults.getTotal()); |
|||
return pageData; |
|||
} |
|||
} |
@ -0,0 +1,167 @@ |
|||
package com.aiprose.querydsl.util; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class FixedPageData<T> { |
|||
protected long total; |
|||
protected long offset; |
|||
protected long size; |
|||
protected long startIndex; |
|||
protected long endIndex; |
|||
protected long showIndex = 2L; |
|||
protected List<T> results = new ArrayList(); |
|||
|
|||
protected FixedPageData() { |
|||
} |
|||
|
|||
public FixedPageData(long page, long size) { |
|||
this.setSize(size); |
|||
this.setOffset(page * size - size); |
|||
} |
|||
|
|||
public boolean isLastPage() { |
|||
return this.getActivePage() >= this.getPages(); |
|||
} |
|||
|
|||
public long getActivePage() { |
|||
if (this.total == 0L) { |
|||
return 1L; |
|||
} else if (this.size == 0L) { |
|||
return 1L; |
|||
} else { |
|||
long activePage = (this.offset + this.size) / this.size; |
|||
return activePage; |
|||
} |
|||
} |
|||
|
|||
public long getPages() { |
|||
if (this.total == 0L) { |
|||
return 1L; |
|||
} else if (this.total < this.size) { |
|||
return 1L; |
|||
} else { |
|||
long pages = this.total / this.size; |
|||
if (this.total % this.size != 0L) { |
|||
++pages; |
|||
} |
|||
|
|||
return pages; |
|||
} |
|||
} |
|||
|
|||
public boolean isFirstPage() { |
|||
return this.getActivePage() == 1L; |
|||
} |
|||
|
|||
public long getAscNo(long index) { |
|||
long n = this.getOffset() + 1L; |
|||
return n + index; |
|||
} |
|||
|
|||
public long getOffset() { |
|||
return this.offset; |
|||
} |
|||
|
|||
public void setOffset(long offset) { |
|||
if (offset < 0L) { |
|||
this.offset = 0L; |
|||
} else if (this.size == 0L) { |
|||
this.offset = 0L; |
|||
} else { |
|||
this.offset = offset - offset % this.size; |
|||
} |
|||
|
|||
} |
|||
|
|||
public long getDescNo(long index) { |
|||
long n = this.getOffset() + 1L; |
|||
n = this.getTotal() + 1L - n; |
|||
return n - index; |
|||
} |
|||
|
|||
public long getTotal() { |
|||
return this.total; |
|||
} |
|||
|
|||
public void setTotal(long total) { |
|||
this.total = total; |
|||
if (this.offset >= total) { |
|||
long extra = total % this.size; |
|||
long lastStart = extra != 0L ? total - extra : total - this.size; |
|||
if (lastStart > 0L) { |
|||
this.offset = lastStart; |
|||
} else { |
|||
this.offset = 0L; |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
public long getSize() { |
|||
return this.size; |
|||
} |
|||
|
|||
public void setSize(long size) { |
|||
if (size <= 0L) { |
|||
this.size = 10L; |
|||
} else { |
|||
this.size = size; |
|||
} |
|||
|
|||
} |
|||
|
|||
public long getStartIndex() { |
|||
if (this.getPages() <= 2L * this.getShowIndex()) { |
|||
this.startIndex = 1L; |
|||
} else { |
|||
this.startIndex = this.getActivePage() - this.getShowIndex(); |
|||
if (this.startIndex < 1L) { |
|||
this.startIndex = 1L; |
|||
} |
|||
} |
|||
|
|||
return this.startIndex; |
|||
} |
|||
|
|||
public void setStartIndex(long startIndex) { |
|||
this.startIndex = startIndex; |
|||
} |
|||
|
|||
public long getShowIndex() { |
|||
return this.showIndex; |
|||
} |
|||
|
|||
public void setShowIndex(long showIndex) { |
|||
this.showIndex = showIndex; |
|||
} |
|||
|
|||
public long getEndIndex() { |
|||
if (this.getPages() <= 2L * this.getShowIndex()) { |
|||
this.endIndex = this.getPages(); |
|||
} else { |
|||
this.endIndex = this.getActivePage() + this.getShowIndex() - 0L; |
|||
if (this.endIndex > this.getPages()) { |
|||
this.endIndex = this.getPages(); |
|||
} |
|||
} |
|||
|
|||
return this.endIndex; |
|||
} |
|||
|
|||
public void setPage(long page) { |
|||
this.setOffset(page * this.size - this.size); |
|||
} |
|||
|
|||
public void setEndIndex(long endIndex) { |
|||
this.endIndex = endIndex; |
|||
} |
|||
|
|||
public List<T> getResults() { |
|||
return this.results; |
|||
} |
|||
|
|||
public void setResults(List<T> results) { |
|||
this.results = results; |
|||
} |
|||
} |
@ -0,0 +1,25 @@ |
|||
package com.aiprose.querydsl.vos; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonFormat; |
|||
import lombok.Data; |
|||
import org.springframework.format.annotation.DateTimeFormat; |
|||
|
|||
import javax.persistence.Column; |
|||
import java.io.Serializable; |
|||
import java.util.Date; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: liuchao |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/15 |
|||
*/ |
|||
@Data |
|||
public class DeptTreeVo implements Serializable { |
|||
private String name; |
|||
private String loc; |
|||
private Integer pid; |
|||
private Date cdate; |
|||
private List<DeptTreeVo> child; |
|||
} |
@ -0,0 +1,19 @@ |
|||
package com.aiprose.querydsl.vos; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import lombok.Data; |
|||
|
|||
import javax.persistence.Column; |
|||
import java.io.Serializable; |
|||
|
|||
/** |
|||
* @version 1.0 |
|||
* @Auther: nelson |
|||
* @company 北京中经网软件有限公司 |
|||
* @Date: 2020/9/15 |
|||
*/ |
|||
@Data |
|||
public class UserVo implements Serializable { |
|||
private String username; |
|||
private String password; |
|||
} |
Загрузка…
Ссылка в новой проблеме