liuchao 4 vuotta sitten
commit
1aac575208
  1. 34
      .gitignore
  2. 54
      build.gradle
  3. 1
      settings.gradle
  4. 13
      src/main/java/com/aiprose/querydsl/QuerydslApplication.java
  5. 34
      src/main/java/com/aiprose/querydsl/controller/UserController.java
  6. 26
      src/main/java/com/aiprose/querydsl/entity/IDEntity.java
  7. 20
      src/main/java/com/aiprose/querydsl/entity/User.java
  8. 15
      src/main/java/com/aiprose/querydsl/repository/UserRepository.java
  9. 48
      src/main/java/com/aiprose/querydsl/service/UserService.java
  10. 14
      src/main/resources/application.yml
  11. 13
      src/test/java/com/aiprose/querydsl/QuerydslApplicationTests.java

34
.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/

54
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()
}

1
settings.gradle

@ -0,0 +1 @@
rootProject.name = 'querydsl'

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

34
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<User> getUser(@PathVariable("username") String username){
User user = userService.findByUsername(username);
return Mono.just(user);
}
@GetMapping("user/save/{username}")
public Mono<User> saveUser(@PathVariable("username") String username){
User user = userService.save(username);
return Mono.just(user);
}
}

26
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;
}

20
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;
}

15
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<User, String>,
QuerydslPredicateExecutor<User>{
}

48
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;
}
}

14
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

13
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() {
}
}
Ladataan…
Peruuta
Tallenna