From 1aac575208ff7375da489d2429e433eb1246c21a Mon Sep 17 00:00:00 2001 From: liuchao Date: Mon, 14 Sep 2020 17:02:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=91=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 34 ++++++++++++ build.gradle | 54 +++++++++++++++++++ settings.gradle | 1 + .../aiprose/querydsl/QuerydslApplication.java | 13 +++++ .../querydsl/controller/UserController.java | 34 ++++++++++++ .../com/aiprose/querydsl/entity/IDEntity.java | 26 +++++++++ .../com/aiprose/querydsl/entity/User.java | 20 +++++++ .../querydsl/repository/UserRepository.java | 15 ++++++ .../aiprose/querydsl/service/UserService.java | 48 +++++++++++++++++ src/main/resources/application.yml | 14 +++++ .../querydsl/QuerydslApplicationTests.java | 13 +++++ 11 files changed, 272 insertions(+) create mode 100644 .gitignore create mode 100644 build.gradle create mode 100644 settings.gradle create mode 100644 src/main/java/com/aiprose/querydsl/QuerydslApplication.java create mode 100644 src/main/java/com/aiprose/querydsl/controller/UserController.java create mode 100644 src/main/java/com/aiprose/querydsl/entity/IDEntity.java create mode 100644 src/main/java/com/aiprose/querydsl/entity/User.java create mode 100644 src/main/java/com/aiprose/querydsl/repository/UserRepository.java create mode 100644 src/main/java/com/aiprose/querydsl/service/UserService.java create mode 100644 src/main/resources/application.yml create mode 100644 src/test/java/com/aiprose/querydsl/QuerydslApplicationTests.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84695dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..fc70e51 --- /dev/null +++ b/build.gradle @@ -0,0 +1,54 @@ +buildscript { + ext { + queryDslVersion = '4.3.1' + } + repositories { + maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} + } +} +plugins { + id 'org.springframework.boot' version '2.3.3.RELEASE' + id 'io.spring.dependency-management' version '1.0.10.RELEASE' + id 'java' +} + +group = 'com.aiprose' +version = '0.0.1' +sourceCompatibility = '1.8' + + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} +repositories { + maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } + mavenCentral() +} +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-webflux' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + developmentOnly 'org.springframework.boot:spring-boot-devtools' + + runtimeOnly 'mysql:mysql-connector-java' + + // QueryDSL + implementation("com.querydsl:querydsl-core:${queryDslVersion}") + implementation("com.querydsl:querydsl-jpa:${queryDslVersion}") + + annotationProcessor("com.querydsl:querydsl-apt:${queryDslVersion}:jpa", + "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final", + "javax.annotation:javax.annotation-api:1.3.2","org.projectlombok:lombok") + + + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + + testImplementation('org.springframework.boot:spring-boot-starter-test') { + exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' + } +} +test { + useJUnitPlatform() +} diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..5e39607 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'querydsl' diff --git a/src/main/java/com/aiprose/querydsl/QuerydslApplication.java b/src/main/java/com/aiprose/querydsl/QuerydslApplication.java new file mode 100644 index 0000000..754ec23 --- /dev/null +++ b/src/main/java/com/aiprose/querydsl/QuerydslApplication.java @@ -0,0 +1,13 @@ +package com.aiprose.querydsl; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class QuerydslApplication { + + public static void main(String[] args) { + SpringApplication.run(QuerydslApplication.class, args); + } + +} diff --git a/src/main/java/com/aiprose/querydsl/controller/UserController.java b/src/main/java/com/aiprose/querydsl/controller/UserController.java new file mode 100644 index 0000000..557e2b2 --- /dev/null +++ b/src/main/java/com/aiprose/querydsl/controller/UserController.java @@ -0,0 +1,34 @@ +package com.aiprose.querydsl.controller; + +import com.aiprose.querydsl.entity.User; +import com.aiprose.querydsl.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + +/** + * @version 1.0 + * @Auther: nelson + * @company 北京中经网软件有限公司 + * @Date: 2020/9/14 + */ +@RestController +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping("user/{username}") + public Mono getUser(@PathVariable("username") String username){ + User user = userService.findByUsername(username); + return Mono.just(user); + } + + @GetMapping("user/save/{username}") + public Mono saveUser(@PathVariable("username") String username){ + User user = userService.save(username); + return Mono.just(user); + } +} diff --git a/src/main/java/com/aiprose/querydsl/entity/IDEntity.java b/src/main/java/com/aiprose/querydsl/entity/IDEntity.java new file mode 100644 index 0000000..30c528b --- /dev/null +++ b/src/main/java/com/aiprose/querydsl/entity/IDEntity.java @@ -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 IDEntity implements Serializable { + /** + * 主键 + */ + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + protected Long id; +} diff --git a/src/main/java/com/aiprose/querydsl/entity/User.java b/src/main/java/com/aiprose/querydsl/entity/User.java new file mode 100644 index 0000000..2cddc45 --- /dev/null +++ b/src/main/java/com/aiprose/querydsl/entity/User.java @@ -0,0 +1,20 @@ +package com.aiprose.querydsl.entity; + +import lombok.Data; +import lombok.Getter; + +import javax.persistence.Entity; +import javax.persistence.Table; + +/** + * @version 1.0 + * @Auther: nelson + * @company 北京中经网软件有限公司 + * @Date: 2020/9/14 + */ +@Data +@Entity +@Table(name="tb_user") +public class User extends IDEntity{ + private String username; +} diff --git a/src/main/java/com/aiprose/querydsl/repository/UserRepository.java b/src/main/java/com/aiprose/querydsl/repository/UserRepository.java new file mode 100644 index 0000000..fe20899 --- /dev/null +++ b/src/main/java/com/aiprose/querydsl/repository/UserRepository.java @@ -0,0 +1,15 @@ +package com.aiprose.querydsl.repository; + +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 UserRepository extends JpaRepository, + QuerydslPredicateExecutor{ +} diff --git a/src/main/java/com/aiprose/querydsl/service/UserService.java b/src/main/java/com/aiprose/querydsl/service/UserService.java new file mode 100644 index 0000000..98ff594 --- /dev/null +++ b/src/main/java/com/aiprose/querydsl/service/UserService.java @@ -0,0 +1,48 @@ +package com.aiprose.querydsl.service; + +import com.aiprose.querydsl.entity.QUser; +import com.aiprose.querydsl.entity.User; +import com.aiprose.querydsl.repository.UserRepository; +import com.querydsl.jpa.impl.JPAQueryFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +/** + * @version 1.0 + * @Auther: nelson + * @company 北京中经网软件有限公司 + * @Date: 2020/9/14 + */ +@Service +public class UserService { + + @Autowired + @PersistenceContext + protected EntityManager entityManager; + + @PostConstruct + public void init() { + queryFactory = new JPAQueryFactory(entityManager); + } + + @Autowired + private UserRepository repository; + + protected JPAQueryFactory queryFactory; + + public User findByUsername(String username) { + QUser qUser = QUser.user; + return queryFactory.select(qUser).from(qUser).where(qUser.username.eq(username)).fetchOne(); + } + + public User save(String username) { + User user = new User(); + user.setUsername(username); + repository.saveAndFlush(user); + return user; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..709354a --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,14 @@ +server: + port: 8081 +spring: + jpa: + hibernate: + ddl-auto: update + show-sql: true + generate-ddl: true + database-platform: org.hibernate.dialect.MySQL8Dialect + datasource: + username: root + password: trgis + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://47.98.109.5:3309/querydsl?characterEncoding=utf8&useSSL=false \ No newline at end of file diff --git a/src/test/java/com/aiprose/querydsl/QuerydslApplicationTests.java b/src/test/java/com/aiprose/querydsl/QuerydslApplicationTests.java new file mode 100644 index 0000000..5a227d9 --- /dev/null +++ b/src/test/java/com/aiprose/querydsl/QuerydslApplicationTests.java @@ -0,0 +1,13 @@ +package com.aiprose.querydsl; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class QuerydslApplicationTests { + + @Test + void contextLoads() { + } + +}